- Home /
Unity 5.5.1 - Custom editor button Undo query
Hello all,
I am making a custom editor, and one of the functionalities will be to spawn multiple prefabs at a specific positions, as well as to undo the spawn action(s). So far I have been able to spawn a single object when the "Place object" button is clicked, but now I can't get my head around on how to Undo the spawn action with an "Undo" button click. I have browsed through Unity's scripting API, many posts on this forum, as well as other, but the further I read the more confused I get.
The script I have so far is:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(ObjectSpawn))]
public class ObjectSpawnEditor : Editor
{
int objectSelectionToolbar = 0;
bool chooseOption = false;
bool objectSelectionFoldout = false;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
chooseOption = EditorGUILayout.Foldout(chooseOption, "Choose a part to add:");
if (chooseOption)
{
//Check Station selection button
EditorGUILayout.BeginVertical();
objectSelectionFoldout = GUILayout.Toggle(objectSelectionFoldout, "" + (objectSelectionFoldout ? "▼ Object selection ▼" : "► Object selection ◄"), "Button", GUILayout.MaxWidth(Screen.width), GUILayout.Height(25));
if (objectSelectionFoldout)
{
GUILayout.Space(5); //Space before a text box
GUILayout.Box("Select lenght:");
GUILayout.Space(5); //Space after a text box and before a toolbox
string[] stationSelectionToolbarOptions = new string[] { "Object A", "Object B", "ObjectC" };
objectSelectionToolbar = GUILayout.Toolbar(objectSelectionToolbar, stationSelectionToolbarOptions, GUILayout.MinWidth(Screen.width), GUILayout.Height(50));
GUILayout.Space(5);
switch (objectSelectionToolbar)
{
case 0:
GUILayout.BeginHorizontal();
if (GUILayout.Button("Place object", GUILayout.Height(25)))
{
ObjectSpawn myScript = (ObjectSpawn)target;
myScript.AttachObject();
}
if (GUILayout.Button("Undo", GUILayout.Height(25)))
{
}
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.Box("Information:", GUILayout.MaxWidth(Screen.width), GUILayout.Height(50));
GUILayout.Space(5);
break;
}
}
EditorGUILayout.EndVertical();
}
}
}
And the Monobehaviour script to attach the object, as well as to parent the spawned object to the object that has the script attached to:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectSpawn : MonoBehaviour {
public void AttachObject()
{
GameObject Object_A = Resources.Load("Prefab") as GameObject;
Instantiate(Object_A, this.transform.position, this.transform.rotation, this.transform);
}
}
In Unity it is showing as bellow:
"Start Point" is just an empty game object that has the script "ObjectSpawn" attached to.
Could anyone provide any ideas or steps for the solution? Any suggestions are welcome! Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220612111613im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Custom Inspector Undo Functionality when directly changing SerializedProperties 1 Answer
CustomPropertyDrawer undoable properties 1 Answer
How to Undo SharedMaterial set in CustomEditor? 1 Answer
Undo.RegisterFullObjectHierarchyUndo not working 1 Answer
Custom Editor serializing data in prefab instance. 0 Answers