Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Rickenbaker · Feb 22, 2017 at 06:58 PM · c#serializationcustom editordirty

[Solved] Custom editor resets after Play?

Hello all,

Asking for your help and guidance on this one.

I am making a custom editor to instantiate and destroy multiple objects using two GUI buttons. In edit mode, all is working fine, I can instantiate multiple prefabs, and then destroy them, one by one, from last instantiated to first, but as soon as I press "Play" and the "Stop", I am not able to destroy any previously instantiated prefabs (instantiated before play-mode). I can instantiate new prefabs and then destroy them, but as for those prefabs that were instantiated before I hit play - they stay unaffected.

Now, I am not that much worried that this happens in play mode, but I definitely want to continue from the point before play mode.

Whenever a prefab is instantiated, I add it to a Stack and keep a track of the size. After I hit play - stop, it seems to reset to 0.

After browsing numerous posts about a similar problem and checking Unity's API, I do realize I need to figure out a way to save the custom editor settings and variables by serializing them or making them dirty, I just do not know how to accomplish this. I've tried what was suggested in those posts, but nothing seems to be working in my case (or I'm doing something wrong).

I have a Monobehaviour script and an Editor script. I'll post the snippets for a single type of the object, as other parts are the same.

Here is the first one:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable, ExecuteInEditMode]
 public class ObjectControl: MonoBehaviour {
 
     [SerializeField, HideInInspector]
     public Stack<GameObject> undoStack = new Stack<GameObject>();
     public GameObject instance;
     public int undoStackSize;
     public int objectSelectionIndex = 0;
     
     public void placeObject()
     {
         switch (objectSelectionIndex)
         {
             case 1:
 
                 Debug.Log("Just received a number 1 from the editor");
                 GameObject object_A = Resources.Load("Prefabs/Object_A") as GameObject;
                 instance = Instantiate(object_A, this.transform.position, this.transform.rotation, this.transform);
                              
                 undoStack.Push(instance);
                 undoStackSize = undoStack.Count;
 
                 break;
  
             case 4:
 
                 Debug.Log("Just received a number 4 from the editor, deleting the object");
 
                 if (undoStack.Count > 0)
                 {
                     GameObject objToUndo = undoStack.Pop();
                     DestroyImmediate(objToUndo);
                     undoStackSize--;
                     
                 }
 
                 else
                 {
                     Debug.Log("Stack is empty! Stack size is: " + undoStack.Count);
                 }
 
                 break;
         }
     }

And the Editor script:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using System.Collections.Generic;
 
 
 [CustomEditor(typeof(ObjectControl)), CanEditMultipleObjects]
 public class ObjectControlEditor : Editor
 {
    
     int objectSelectionToolbar = 0;
     int numberOfPossibleUndo;
         
     bool chooseOption = false;
     bool objectSelectionFoldout = false;
       
     public ObjectControl scriptTarget;
 
     public void Awake()
     {
         scriptTarget = (ObjectControl)target;
     }
 
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
 
         GUI.changed = false;
 
         chooseOption = EditorGUILayout.Foldout(chooseOption, "Choose a segment to add:");
 
         if (chooseOption)
         {
 
             EditorGUILayout.BeginVertical();
 
             stationSelectionFoldout = GUILayout.Toggle(stationSelectionFoldout, "" + (stationSelectionFoldout ? "▼ 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 of the station:");
                 GUILayout.Space(5);     //Space after a text box and before a toolbox
 
                 string[] objectSelectionToolbarOptions = new string[] { "Object A", "Object B", "Object C" };
 
                 stationSelectionToolbar = GUILayout.Toolbar(objectSelectionToolbar, objectSelectionToolbarOptions, GUILayout.MinWidth(Screen.width), GUILayout.Height(50));
                 GUILayout.Space(5);
 
                 RollerCoasterBuilder scriptTarget = (RollerCoasterBuilder)target;
                 numberOfPossibleUndo = scriptTarget.undoStackSize;
                 
                 switch (objectSelectionToolbar)
                 {
                     case 0:
 
                         GUILayout.BeginHorizontal();
                            
                         if (GUILayout.Button("Place selected object", GUILayout.Height(30)))
                         {
                             scriptTarget.objectSelectionIndex = 1;
                             scriptTarget.PlaceObject();
                         }
 
                         GUILayout.Space(5);
 
                         if (GUILayout.Button("Undo" + "(" + numberOfPossibleUndo + ")", GUILayout.Height(30)))
                         {
                             scriptTarget.objectSelectionIndex = 4;
                             scriptTarget.PlaceObject();
                         }
                                           
                         GUILayout.EndHorizontal();
 
                         break;
                     }
                }  
          }
       if (GUI.changed)
          {
          EditorUtility.SetDirty(target);
          }

}

Any ideas or suggestions are welcome. Thanks in advance ;)

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jdean300 · Feb 22, 2017 at 07:05 PM

Stacks cannot be serialized. You need to use a List<GameObject>.

Comment
Add comment · Share
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
0

Answer by UnityCoach · Feb 22, 2017 at 07:27 PM

There's already a whole Undo system for Custom Editors that you can use.

Comment
Add comment · Show 2 · Share
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 Rickenbaker · Feb 22, 2017 at 07:49 PM 0
Share

Hi UnityCoach,

I have read through this page a lot of times but I'm still unable to get my head around how to implement this in my code. Could you maybe advise or point to the correct direction, as I am lost here.

Thanks

avatar image UnityCoach Rickenbaker · Feb 22, 2017 at 11:15 PM 1
Share

Sure, in a nutshell, most operations are caught by the undo system, with a few exceptions that you must handle, like adding a component where gameObjectToAddComponentTo.AddComponent<$$anonymous$$yComponent>(); becomes Undo.AddComponent<$$anonymous$$yComponent>(gameObjectToAddComponentTo);. $$anonymous$$ost cases are given in the docs.

Now, the undo system will gather all operations that happened in the context of a click and group them in one. You can still control this further like :

 int undoGroup = Undo.GetCurrentGroup();
 Undo.SetCurrentGroupName ("Add Special Items"); // don't add "Undo" at the beginning or it'll print "Undo Undo"
 Undo.CollapseUndoOperations (undoGroup);

You can also record all object (component) properties

 Undo.RecordObject (_component, "Set Component Properties");
avatar image
0

Answer by Rickenbaker · Feb 23, 2017 at 09:49 AM

Thanks for everyone for their input,

I was finally able to implement Undo/Redo functionality in my Editor script.

I've also decided to ditch the Stack and carry on with removing child objects, one by one, from last to first, as can be seen in this post: http://stackoverflow.com/questions/32402833/simple-way-to-delete-the-last-child-of-a-gameobject/32402959

No more problems after play-mode :)

Have a nice day!

Comment
Add comment · Share
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

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

317 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 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 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 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 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

ScriptableObject created from custom editor lose data on Unity restart 1 Answer

Custom editor, not serializing 0 Answers

SetDirty not working in Custom Editor with Nested Custom Property drawer 1 Answer

How the heck do I save this info for my characters? [Serialization Usage Question] 1 Answer

How Can I Stop my Array Being Cleared on Runtime via Serialisation? 0 Answers


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