Prefabを一括で置き換えたいんじゃい!!
こんなことを考えたことはありませんか?
- すでに設置したPrefabを置き換えたい
- ステージ作ったけど、いくつか障害物にしたい
などなど、色んなシーンで使えそうなEditor拡張を書いてみました。

前提
他にもやりたいことがたくさんある場合、こちらのAssetがめちゃくちゃ便利です。
assetstore.unity.com
作ったもの
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
// Copyright (c) 2021 Hiroyuki Kako // This software is released under the MIT License, see LICENSE. #if UNITY_EDITOR using System.Collections.Generic; using UnityEditor; using UnityEngine; namespace ObjectReplacer { public class ObjectReplacer : EditorWindow { [MenuItem("KEditorExtensions/ObjectReplacer")] private static void Open() { GetWindow<ObjectReplacer>("Replace object in scene."); } static GameObject expectedObject; public GameObject[] targetObjectsInEditor; public GameObject expectedObjectInEditor; private static List<GameObject> _cachedObjects = new List<GameObject>(); [MenuItem("GameObject/KEditorExtensions/ObjectReplacer/DO_REPLACE", false, 20)] public static void DO_REPLACE() { var tmp = Selection.gameObjects; foreach (var obj in tmp) { if (!_cachedObjects.Contains(obj)) { _cachedObjects.Clear(); _cachedObjects = new List<GameObject>(tmp); ReplaceFunction(); break; } } } private void OnGUI() { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Object of destinations to convert."); expectedObjectInEditor = (GameObject)EditorGUILayout.ObjectField(expectedObjectInEditor, typeof(GameObject), true); expectedObject = expectedObjectInEditor; EditorGUILayout.EndHorizontal(); ScriptableObject target = this; SerializedObject so = new SerializedObject(target); SerializedProperty stringsProperty = so.FindProperty("targetObjectsInEditor"); EditorGUILayout.PropertyField(stringsProperty, true); so.ApplyModifiedProperties(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Right click on hierarchy and select DO REPLACE or push button."); EditorGUILayout.EndHorizontal(); if (GUILayout.Button("DO REPLACE")) { _cachedObjects.Clear(); _cachedObjects = new List<GameObject>(targetObjectsInEditor); expectedObject = expectedObjectInEditor; ReplaceFunction(); targetObjectsInEditor = new GameObject[0]; } } private static void ReplaceFunction() { if (expectedObject != null) { foreach (var tmp in _cachedObjects) { GameObject newObj = PrefabUtility.InstantiatePrefab(expectedObject) as GameObject; if (!(newObj is null)) { newObj.transform.position = tmp.transform.position; newObj.transform.eulerAngles = tmp.transform.eulerAngles; newObj.transform.SetParent(tmp.transform.parent); } DestroyImmediate(tmp); } } else { Debug.Log("No target objects are set."); } } } } #endif |
使い方
ProjectのAssets以下、好きなところにおいてください。Editor内が良いでしょうが、どこでも動くぶんには動きます。その場合、ビルド時だけ注意してください。
Gifの通り、2つの使い方ができます。
右クリックから置換
置き換えたいObjectをセット、置き換えられたいObjectを任意の数選択し、右クリック→置換
Windowから置換
置き換えたいObjectをセット、置き換えられたいObjectを任意の数D&Dし、ボタン押下→置換
