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
1
Question by nuverian · Apr 29, 2013 at 07:29 PM · editorserializationeditorwindowcustom editor

EditorWindow: How to Serialize variables after PLay

Hi. So I've searched countless of times for a solution to this problem, but I wasnt really able to find or understand how to work (around?) this. In practise:

I have an EditorWindow which has a variable of an object I want to make GUI of, for example:

 class MyEditorWindow extends EditorWindow{
 
   var targetObject:Foo;
 
   function OnGUI(){
 
      targetObject.myVar = FloatField(targetObject.myVar);
 
      if (GUI.changed)
         EditorUtility.SetDirty(targetObject);
   }
 
   @MenuItem("MyEditor/OpenEditor")
   static function OpenWindow(targetObject:Foo){
 
      var window = GetWindow(MyEditorWindow);
      window.targetObject = targetObject;
      window.Show();
   }
 }

All works good etc. Then I hit Play and everything has been decoupled. I still see my EditorWindow GUI and change the float for example from the control, but it doesnt refer to the targetObject anymore. So anyone know hot to work this?

Thanks, I believe I was prety clear on the issue :-) and hopefully other get help as well.

(PS: Foo is MonoBehaviour)

Comment
Add comment · Show 7
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 Bunny83 · Apr 29, 2013 at 07:35 PM 0
Share

What is Foo? Is it a $$anonymous$$onoBehaviour script? Because "normal" classes aren't serialized on their own. They are serialized along with a $$anonymous$$onoBehaviour or ScriptableObject

avatar image nuverian · Apr 30, 2013 at 02:37 PM 0
Share

Yes its a $$anonymous$$onoBehaviour..Sorry

avatar image nuverian · Apr 30, 2013 at 02:44 PM 0
Share

The issue is not that targetObject (Foo) is no sierializing the changes done from the editor, but rather that the editor window looses the connection with the targetObject as sson as I enter play mode

avatar image Bunny83 · Apr 30, 2013 at 03:25 PM 0
Share

Hmm, no matter what i do i can't reproduce what you describe. What exactly do you mean by "decoupled"? I suggest you add an ObjectField in your EditorWindow. That way you can click on the ObjectField to highlight the object that it holds.

 EditorGUILayout.ObjectField(targetObject, Foo);

Note: this is a "readonly" object field, so you can't change the targetObject variable with it. If you want that (for debugging) use this:

 targetObject = EditorGUILayout.ObjectField(targetObject, Foo) as Foo;

I've tried something similar to your example and it works as expected. Editing assets will permanently change the prefab. Editing scene objects will do the change. Editing during runtime also changes the object but it's of course reverted once you stop playmode since you have just a copy of the scene at runtime and the original scene is deserialized when you re-enter edit mode.

avatar image Bunny83 · Apr 30, 2013 at 03:32 PM 0
Share

$$anonymous$$ay i ask you how you open the window? Because $$anonymous$$enuItems doesn't work on functions with parameters.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ajkolenc · Jan 11, 2017 at 10:26 PM

I know this is kind of old, but I was experiencing the same problem and I found the solution.

Any variables you want to persist after you press play need to be serialized by Unity. Since it doesn't really make sense to have public variables on an EditorWindow, this means prefacing your private variables with the [SerializeField] attribute. This allows Unity to store the value and reload it after you finish playing.

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

16 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

Related Questions

Change Image/Icon Shown for Asset in Project/Asset Window? 0 Answers

Editor Window Serialization not Working as Expected 2 Answers

Editable non-monobehaviour objects 1 Answer

Creating persistent object in Edit mode 1 Answer

OnSerialize event 2 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