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 kdubb · May 23, 2011 at 12:10 AM · togglecustom-inspectorstate

How to manage toggle (and other controls) state in a custom inspector?

I have a custom inspector with toggles for a number of items to make it more manageable:

alt text

Each "Item #" can be toggled (simulating the Unity array inspector). The toggles are only part of the editor itself and exist only to make the editor more easy to digest. Unfortunately since there is no boolean in my object representing the toggle state it means that all the toggles reset as I switch between objects in Unity.

I could solve this issue by putting a variable for the toggle state into my object but this seems wasteful and unnecessary.

I know that Unity has some method of handling this without intruding into the object itself because the default inspector toggles work correctly with no "extra" state variables in the object.

How can I get Unity to remember the toggle state?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · May 23, 2011 at 01:24 AM

Just add a boolean array to your inspector. When you size your array you have to size this array as well. You could make the array a static variable, so the toggled items keep their state when selecting different objects.

OnEnable is fired when you select one of your objects. You should check and update the array here. I would use a List instead of an array ;) just because it's easier to resize.

Comment
Add comment · Show 2 · 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 kdubb · May 23, 2011 at 01:31 AM 0
Share

I was basically doing that but I was using start. It seems that the entire inspector is re-created each time you select an object. So this won't actually work. I have almost worked around that by storing my inspector state variables in a static dictionary keyed on the object I have selected. Unfortunately this works until I hit "Play" then it loses all state again (I guess the objects get new identities).

avatar image Bunny83 · May 24, 2011 at 04:28 PM 0
Share

Why do you want to store the states per object? wouldn't it be enough to create one static list? Of course if you have toggled an item and you select another object the same items(index-based) will be toggled. If you really want the toggle state per object, add the state variable to your object. You can wrap the variable in #if UNITY_EDITOR .... #endif so it won't go into your build.

avatar image
0

Answer by Adam-Buckner · Aug 26, 2011 at 08:11 PM

My solution was similar to Bunny83's. I have an array for the boolean flags for each element, but I save the flags to editor prefs. This code is first pass, and needs some polish, but goes something like this:

 public MyType thisTarget;
 public bool[] displaysExpanded = new bool[0];
 
 void OnEnable () {
     thisTarget = target as MyType;
     displaysExpanded = new bool[thisTarget.myArrayOrList.Length];
     for (int i = 0; i < displaysExpanded.Length; i++) {
         displaysExpanded[i] = EditorPrefs.GetBool ("FoldFlag"+i);
     }
 }
 
 void OnDisable () {
     for (int i = 0; i < displaysExpanded.Length; i++) {
         EditorPrefs.SetBool ("FoldFlag"+i, displaysExpanded[i]);
     }
 }

If you wanted to save this per game object, you could have the editor prefs key be something like "FoldFlag"+target.gameobject.name+i (or whatever is legal).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Custom Editor for GameObject Class look like Unity's Custom Inspector for GameObject? 1 Answer

Editor randomly unparents game object after InstantiatePrefab() & SetParent() in OnInspectorGUI() 0 Answers

Change gameview ratio / resolution using a custom inspector? 1 Answer

problems writing an enum range attribute 1 Answer

Error "Can't load custom inspector ... because the inspected type is null." for custom editor in a dll 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