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 DavidD · Aug 10, 2012 at 08:38 PM · editorinspectorfoldout

Unity editor - keeping your foldout from collapsing

Hey there!

I have set up a custom inspector for my player. However, whenever I go in-game all of my foldouts collapse. Is there a way to prevent this?

Here's the foldout code I use in case in matters:

 showCameraInfo = EditorGUILayout.Foldout(showCameraInfo, "Camera settings");
Comment
Add comment · Show 4
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 codingChris · Apr 25, 2013 at 01:44 PM 1
Share

This annoys me too. It doesn't even help if you add [SerializeField] to showCameraInfo. A workaround would be to store showCameraInfo in the target object itself, ins$$anonymous$$d of your custom editor. But that's a little bit ugly since it's pure editor logic and not game related...

avatar image whydoidoit · Apr 25, 2013 at 02:49 PM 0
Share

Or you can store it in EditorPrefs...

avatar image codingChris · Apr 25, 2013 at 05:54 PM 0
Share

Oh cool. Didn't even know about EditorPrefs. Thanks.

avatar image darbotron · May 11, 2014 at 10:57 PM 0
Share

I'd be interested to know if anyone knows of a more elegant way (or ways) to do this for use in a property drawer that's used multiple times on possibly 100s of objects?

I'm assu$$anonymous$$g the problem is being caused by the editor throwing away all the existing property drawers and reinstantiating them from serialised data on play?

1 Reply

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

Answer by vexe · May 13, 2014 at 11:21 PM

So I usually deal with this by putting the editor data in the MonoBehaviour and surrounding it with #if UNITY_EDITOR / stuff / #endif

However, recently I came up with a new idea which I think is better. Like @whydoidoit said, you could use EditorPrefs - but the question is, what key should you use so that the foldout value persists when you, say, reselect your object or trigger an assembly reload?

You have to use a key value that is the same before/after an assembly reload. But what to use? If you're writing an Editor for example, even the 'target' is not the same when an assembly reload happens (a new target is instantiated - so using GetInstanceID is useless)

So what you do, is create an interface:

 public interface IUniquelyIdentifiedObject
 {
    string ID { get; }
 }

And then

 public class Whatever : MonoBehaviour, IUniquelyIdentifiedObject
 {
    [SerializeField]
    private string id;
 
    public string ID { get { if (string.IsNullOrEmpty(id)) id = Guid.NewGuid().ToString(); return id;}
 }

And so now, we have an id value that persists between assembly reloads, so we could use it in our foldouts keys!

 // somewhere in editor code...
 string fKey = key + "My foldout";
 EditorPrefs.SetBool(fKey, EditorGUILayout.Foldout(EditorPrefs.GetBool(fKey), "Foldout"));

where key is:

 private string key
 {
    get
    {
       var unique = target as IUniquelyIdentifiedObject;
       return unique == null ? target.GetHashCode().ToString() : unique.ID;
    }
 }

I don't like to use EditorPrefs though. I have a BetterPrefs class which just uses a dictionary of ints-bools and strings-bools.

This method handles foldout values on different target instances well, because different targets means different IDs, which means independent foldout values. The values persist because the ID persists.

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

13 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

Related Questions

How to get variables of the script show in unity editor/inspector? 2 Answers

A node in a childnode? 1 Answer

Inspector window transform component showing ONLY local cordinates 1 Answer

Unity Editor Inspector creating gameObjects in world 2 Answers

Custom Editor foldout doesn't unfold when clicking on name? 3 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