- Home /
Question by
kevc45 · Feb 07, 2017 at 09:24 PM ·
editor-scriptingserializedproperty
Deleting GameObjects from ReorderableList with proper undo
How can you use ReorderableList to delete GameObjects? Here's what I've got:
using System;
#if(UNITY_EDITOR)
using UnityEditor;
using UnityEditorInternal;
#endif
using UnityEngine;
public class MyScript : MonoBehaviour
{
[HideInInspector]
public Transform[] points;
}
#if(UNITY_EDITOR)
[CustomEditor(typeof(MyScript)), CanEditMultipleObjects]
public class MyScriptEditor : Editor
{
private ReorderableList list;
private void OnEnable()
{
SerializedProperty sp = serializedObject.FindProperty("points");
list = new ReorderableList(serializedObject, sp, true, true, true, true);
list.drawHeaderCallback = (Rect rect) => {
EditorGUI.LabelField(rect, "Points");
};
list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
var element = list.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
EditorGUI.ObjectField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none);
};
list.onAddCallback = (ReorderableList l) =>
{
var index = l.serializedProperty.arraySize;
l.serializedProperty.arraySize++;
l.index = index;
var element = l.serializedProperty.GetArrayElementAtIndex(index);
GameObject point = new GameObject("Point");
Undo.RegisterCreatedObjectUndo(point, "Create point");
point.transform.parent = ((SimplePlatform)target).transform;
element.objectReferenceValue = point.transform;
};
list.onRemoveCallback = (ReorderableList l) =>
{
try
{
//if(EditorUtility.DisplayDialog("Delete", "Are you sure you want to delete this waypoint? This cannot be undone.", "OK", "Cancel"))
{
var element = l.serializedProperty.GetArrayElementAtIndex(l.index);
var go = ((Transform) element.objectReferenceValue).gameObject;
element.DeleteCommand();
Undo.DestroyObjectImmediate(go);
}
}
catch(System.NullReferenceException)
{
}
ReorderableList.defaultBehaviours.DoRemoveButton(l);
};
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
list.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
}
#endif
. However, I lose the reference to the GameObject when redoing. Here's a video.
Comment
not sure everything your trying to do but everytime i have done undo i have used a list
// set up a list in start
List<GameObject> g = new List<GameObject>();
// add undo to the front of the list
g.Insert(0,somedude);
// use or remove an undo
if(g.Count>0){
//do something with g[0]
g.RemoveAt(0);}
Your answer
Follow this Question
Related Questions
Changing isExpanded property from another Editor 0 Answers
Mass component editing through SerializedProperty or Setters 0 Answers
How can I make dynamic gui based off of the variables from a serializable class? 1 Answer
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer
How to get the layer mask value of a SerializedProperty? 1 Answer