Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nicmarxp · Apr 20, 2019 at 10:35 PM 0
Share

Did you figure it out?

avatar image toddisarockstar · Apr 21, 2019 at 05:18 AM 0
Share

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);}

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges