- Home /
Question by
EMOTION-THEORY · Jun 27, 2015 at 02:08 AM ·
c#editorinspector
How To Register Undo multiple steps (Editor)
I have this function in an Editor class that is meant to grab the selected objects in the Inspector and then parent them to a new GameObject.
I basically want to know how to register the entire operation for Undo. I'm getting complications around the instantiation, and parenting, and what not.
Here's the code:
static void AddParent()
{
Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel |
SelectionMode.OnlyUserModifiable);
string name;
if (Selection.activeGameObject)
name = Selection.activeGameObject.name;
else
name = "GameObject";
GameObject newParent = new GameObject(name);
if (transforms == null || transforms.Length == 0)
{
Selection.activeGameObject = newParent;
return;
}
Transform originalParent = transforms [0].parent;
if (originalParent)
newParent.transform.SetParent(originalParent, false);
foreach (Transform transform in transforms)
{
transform.parent = newParent.transform;
}
Selection.activeGameObject = newParent;
}
Comment