-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameObjectReplacer.cs
96 lines (79 loc) · 3.44 KB
/
GameObjectReplacer.cs
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
96
using UnityEngine;
using UnityEditor;
public class GameObjectReplacer : EditorWindow
{
// Prefab to replace selected objects with
private GameObject prefab;
// Options for matching properties from the original object to the prefab
private bool matchPosition = true;
private bool matchRotation = true;
private bool matchScale = true;
// Adds the custom editor window to the Unity menu
[MenuItem("Tools/GameObject Replacer")]
static void OpenWindow()
{
// Open or create the window
GameObjectReplacer window = (GameObjectReplacer)EditorWindow.GetWindow(typeof(GameObjectReplacer));
window.Show();
}
// Draws the UI elements for the custom editor window
private void OnGUI()
{
// Warn user if in Play mode (this tool works only in Edit mode)
if (EditorApplication.isPlaying)
{
EditorGUILayout.HelpBox("GameObject Replacer only works in Edit mode!", MessageType.Warning);
return;
}
// Input field for selecting the prefab
prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);
// Toggles for matching the transform properties
matchPosition = EditorGUILayout.Toggle("Match Position", matchPosition);
matchRotation = EditorGUILayout.Toggle("Match Rotation", matchRotation);
matchScale = EditorGUILayout.Toggle("Match Scale", matchScale);
// Button to execute the replacement operation
if (GUILayout.Button("Replace"))
{
ReplaceSelectedObjects();
}
}
// Replaces the selected game objects with the specified prefab
private void ReplaceSelectedObjects()
{
// Check if a prefab has been assigned
if (prefab == null)
{
Debug.LogError("No prefab selected!");
return;
}
// Record the current selection state for undo operations
Undo.RecordObjects(Selection.gameObjects, "Replace With Prefab");
// Iterate through each selected game object
foreach (GameObject go in Selection.gameObjects)
{
// Instantiate the prefab as a new game object
GameObject newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
// Verify that the prefab was successfully instantiated
if (newObject == null)
{
Debug.LogError("Failed to instantiate prefab");
continue;
}
// Register the newly created object for undo operations
Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefab");
// Match the position of the original object if the option is enabled
if (matchPosition)
newObject.transform.position = go.transform.position;
// Match the rotation of the original object if the option is enabled
if (matchRotation)
newObject.transform.rotation = go.transform.rotation;
// Match the scale of the original object if the option is enabled
if (matchScale)
newObject.transform.localScale = go.transform.localScale;
// Assign the new object to the same parent as the original object
newObject.transform.SetParent(go.transform.parent);
// Destroy the original object and register it for undo operations
Undo.DestroyObjectImmediate(go);
}
}
}