Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 J.J. · Sep 12, 2012 at 09:51 PM · editorwindowplaymodeproperty

How to restore EditorWindow property after playmode?

I just started experimenting with a custom EditorWindow and I added a simple toggle button to enable/disable MeshRenderer.enable. Everything works just fine, however, after returning from playmode, the state of the button still persists. I thought everything got serialized and restored when returning to Edit mode? At least that's what happened to the MeshRenderer.enable. How can I restore the state of the toggle button pre-Playmode so everything is in sync?

Here's the sample:

 public class TestEditor : EditorWindow
 {
     private bool EnableButton = true;
 
     void OnGUI()
     {
         EnableButton = EditorGUILayout.Toggle("ToggleMesh", EnableButton);
         dosomething();
     }
 }
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 J.J. · Sep 13, 2012 at 10:40 PM 1
Share

Is there a doc page that explains how the saving/reloading of properties work with respect to playing the scene from within the editor and then returning back to editing mode?

avatar image LightStriker · Sep 18, 2013 at 09:01 PM 0
Share

Sadly, I have the same issue. I searched far and wide and found no solution. Even experts here told me only "Editor" are resistant to this issue, not EditorWindow. The solution I implemented is not what I would consider "clean", but it works. I've added metadata on all GameObject of my level. Each object in a level get a specific unique ID that doesn't change between play states. When the game start or stop, my EditorWindow retrieve it's data from the scene.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Sep 18, 2013 at 09:20 PM

EditorWindows are serialized the same way all other objects are serialized. That means only public variables (or private variables with SerializeField) of a serializable type are "saved" and "restored". Another way is to use OnEnable / OnDisable and EditorPrefs to store information even beyond editor restarts.

Comment
Add comment · Show 3 · 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 LightStriker · Sep 18, 2013 at 09:47 PM 0
Share

The problem is not one of serialization as the objects are properly saved and retrieved. The problem is the data of an object targeted by an EditorWindow on play state changes. For some obvious reason, Unity creates a copy of all the level's GameObject to build its playfield when you start the engine. For this reason, GameObjects may even change ID between play state. If you have a EditorWindow who's targeting a GameObject in your scene, it won't reference the proper data once the play state changes. It's weird because it still reference the proper object, but not the proper data. An example of this, I added some debug line on my EditorWindow, the data of an enum in a ScriptableObject was different in my window vs the same object seen in the Inspector. I knew it was the same ScriptableObject because it was my EditorWindow who called the Inspector! On top, I haven't found any built-in way or method to track a GameObject between changes of play states.

avatar image Bunny83 · Sep 18, 2013 at 09:58 PM 0
Share

Well, it seems you actually don't have the same issue because the question was about data being lost at playmode-change. The editor and the scene runtime are two seperate things. Of course if you reference an asset inside an editor window the reference won't be updated at playmode change since the Editorwindow doesn't belong to the same serialization-set. The inspector works the same way, only the Selection class tracks the selected object, but that's based on the hierarchy item and not a normal reference.

So you simply have to live with the fact that in playmode you work with different objects.

avatar image LightStriker · Sep 18, 2013 at 10:17 PM 1
Share

He was talking about a button binded on a GameObject which state persist between play state changes. I doubt he has problem serializing a $$anonymous$$eshRenderer on a GameObject.

I fully understand the difference of serialization-set, but Unity doesn't offer any easy solution to have an EditorWindow's data update through play state changes. Frankly, would have it been hard to have a method to retrieve a GO between play state?

Or even assume that someone somewhere would wish an EditorWindow to update its reference?

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

11 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

Related Questions

Is it possible to bake navmesh to plane? 0 Answers

Close EditorWindow in Play Mode 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Problem using a data structure created in editor mode, will not serialize? 1 Answer

Editor GUI Foldout header style customization 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