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 rodeowild · Jul 27, 2017 at 07:05 PM · save datacustom editorcustom-inspector

Unity Custom Editor - save value

This question has been asked, but the answers I have come across do not work for me, so I will ask it again. I have a class similar to this:

 public class MyClass : MonoBehavior
 {
    public int foo; 
 }

I also have an editor for this class, like so:

 [CustomEditor(typeof(MyClass))]
 public class MyEditor : Editor
 {
   private MyClass myClass;
   public int editInfo;
 
   public override void OnInspectorGUI ()
   {
      DrawDefaultInspector ();
 
      myClass = (MyClass)target;
      editInfo = EditorGUILayout.IntField("Edit Info: ", editInfo);

      if(GUILayout.Button ("edit class"))
      {
         editClass();
      }
   }

   public void editClass()
   {
       myClass.foo += editInfo;
   }
 }

My problem is twofold : Firstly, Upon hitting the play button in the editor, Unity resets the editInfo field in my custom Editor. Secondly, although the changes are kept in my Dummy class (MyClass), if I implement this on a real class (too large to show here, but the functionality is the same), the value reverts.

I have tried Undo.RecordObject() and I have added the SerializeField property to all possible places where it could have any meaning. What am i doing wrong?

Comment
Add comment · Show 1
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 rodeowild · Jul 27, 2017 at 08:05 PM 0
Share

Ok, I figured out why the variable in my real life class was reverting. The problem was that it was a prefab. On play, it reverted to the prefab value (I still don't know why). Applying the changes to the prefab fixed this. It would still be nice to not have to do this. Any suggestions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FortisVenaliter · Jul 27, 2017 at 07:24 PM

Assume that nothing in an Editor class is permanent. It can change from frame to frame. If you assume anything will be persistent, you're setting yourself up for failure.

Also, don't store the myClass as a class-scope variable. It should remain method-scope or use an automated getter property.

Finally, keep the data in the data class. "editInfo" is indeterminate simply because it's in an editor class. As soon as you move it to the source data class, it will be more persistent.

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 rodeowild · Jul 27, 2017 at 08:02 PM 0
Share

I'm not sure what you mean by "nothing in the Editor class is permanent". Do you mean all variables, fields, etc.?

Why should myClass be method-scope as opposed to class-scope? It doesn't seem to make a difference.

The third paragraph avoids the problem, but is inconvenient for other reasons. I don't really want my editor specific variables in the class itself. It seems like a hack. It also clutters the editor - ins$$anonymous$$d of begin able to place my editInfo variable near its associated button, it is wedged in between all my other stuff. Not ideal.

avatar image FortisVenaliter rodeowild · Jul 27, 2017 at 08:14 PM 0
Share

For your first question: yes. Every bit of data not built-in to an editor class is suspect. They may stay between objects, or may get cleared out between updates on the same object, so never assume they are deter$$anonymous$$ate or persistent.

For the second... your code there is technically safe as written. But if you didn't set it every single call (line 11), you may end up with an outdated instance after switching between two instances in the hierarchy selector. And it could cause problems moving forward when you up the complexity, so it's best to keep it local. The reason it should be method-scope is to prevent the 'target' variable from changing without updating the 'myClass' variable. If you kept it method-scope, that wouldn't be possible.

Unfortunately, if it needs to be persistent, it can't be in the editor. If you want a clean structure to work with, add an inner class to your data class called "EditorData" that's serializable. That way the editor-only data is at least grouped together in it's own place and you can set it to null in Start() to prevent it from taking up RA$$anonymous$$ outside of the editor.

avatar image rodeowild FortisVenaliter · Jul 27, 2017 at 08:53 PM 0
Share

Thanks, this makes sense. the internal struct is basically what i did to organize. I thought about scriptable objects (I forget exactly what they are, but I remember they are used for storing persistent data), but it seemed like overkill, even if it would work.

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

69 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

Related Questions

Editor slows down by ForLoop(I guess). 1 Answer

How to Hide/Show List or Array in the inspector based on a variable? 0 Answers

How to add custom ui on top control panel 1 Answer

Generic CustomEditor 0 Answers

Automatically increment an integer field in a bunch of prefab gameobjects in the unity editor. 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