35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public class ReplacWithPrefab : EditorWindow
|
|
{
|
|
GameObject prefab;
|
|
string nameFilter = "spotlight_A";
|
|
|
|
[MenuItem("Tools/Replace With Prefab")]
|
|
static void Open() => GetWindow<ReplacWithPrefab>();
|
|
|
|
void OnGUI()
|
|
{
|
|
prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);
|
|
nameFilter = EditorGUILayout.TextField("Name filter", nameFilter);
|
|
|
|
if (GUILayout.Button("Replace") && prefab)
|
|
{
|
|
var all = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
|
|
foreach (var go in all)
|
|
{
|
|
if (!go.name.StartsWith(nameFilter)) continue;
|
|
|
|
var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, go.transform.parent);
|
|
instance.transform.SetPositionAndRotation(go.transform.position, go.transform.rotation);
|
|
instance.transform.localScale = go.transform.localScale;
|
|
instance.name = go.name;
|
|
Undo.RegisterCreatedObjectUndo(instance, "Replace");
|
|
Undo.DestroyObjectImmediate(go);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif |