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
2
Question by vexe · Feb 01, 2014 at 11:17 AM · serializeoninspectorguifoldoutcustominspector

Serialize a variable in a custom inspector?

It seems like mocking stuff with [SerializeField] in an Editor class won't serialize the thing after an assembly reload. I have a simple boolean to represent a fold status for my editor - I want to serialize it. i.e. let's say I unfolded in editor mode, went to play mode and see that it's still unfolded.

The way I used to do it is have the boolean stored in the target MonoBehaviour - (the type whom I'm making the editor for) - but that's just not clean, why does that MB have to store something, only its editor needs? it doesn't make sense.

Here's what my editor looks like:

alt text

If knowing any code would help you, let me know.

Thanks.

fsm.png (19.6 kB)
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

1 Reply

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

Answer by gfoot · Feb 01, 2014 at 11:36 AM

You need to store the data somewhere persistent. It works in the MonoBehaviour because Unity serializes it and recreates it after the code reload. Other than that, you could use EditorPrefs to store your editor-only data, but if you want to store this attribute per instance (so the expanded/collapsed state is stored separately for different edited objects) then it will get tricky.

Comment
Add comment · Show 4 · 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 vexe · Feb 01, 2014 at 11:51 AM 0
Share
  • thanks for your answer! - I've never used EditorPrefs before so this might be a good chance. But why is it tricky to save individual folds? if it's just a bool then I think it should as easy as saving the main fold. I'll give it a shot.

avatar image gfoot · Feb 01, 2014 at 02:32 PM 1
Share

EditorPrefs stores data as key-value pairs. You need a way to create keys that are somehow linked to the $$anonymous$$onoBehaviour being edited. I'm not sure the best way to do that - maybe you can use GetInstanceID().

The other thing you may or may not want is these prefs persist forever - they're stored in the registry - so when you close the editor then open it again later on, it will remember the folds. I don't know whether that suits your needs or not.

I guess another place you could store this data is within a ScriptableObject instance in the Assets folder. Your ScriptableObject could contain a list of $$anonymous$$onoBehaviour instance IDs along with any editor data you want to store linked to that $$anonymous$$onoBehaviour. You would need to make sure the ScriptableObject is correctly serialised of course.

avatar image vexe · Feb 01, 2014 at 02:50 PM 0
Share

Thanks I kind of took Bunny's approach of using conditional compilation http://answers.unity3d.com/questions/216395/editorguilayoutfoldout-no-way-to-remember-state.html

avatar image vexe · Feb 01, 2014 at 02:56 PM 0
Share

I don't really care if the values are saved between editor sessions. I considered just saving the values manually to a file, and loading them back... however, I prefer conditional compilation ins$$anonymous$$d.

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

19 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

Related Questions

Assign script into another script in Inspector 2 Answers

Classic EditorGUILayout.foldout problems 1 Answer

Need to serialize a Monobehaviour Class 2 Answers

usage of FieldInfo.SetValue with SerializeField 2 Answers

Unity C# Deserialization dealing with extra properties 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