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
0
Question by gardian06 · Apr 04, 2013 at 03:40 PM · custom-inspectorbehavior

Custom Inspector fields reset when focus is lost

I am attempting to create a custom editor for one of my scripts, and part of the functionality is that I have a ClassObjects that require a reference name to a datum from a component on a GameObject. this is done in such a way that I only need the datum in order to access what I need from the component.

in the ClassObjectEditor I am storing the reference object for the datum. I can place the GameObject reference, and obtain the datum in the component, but when I lose focus on the GameObject holding the ClassObject, and then return focus the Inspector reads that the GameObject reference is null(none), but when I look at the Debug Inspector it says the datum is still there.

steps to recreate:

  1. create a ClassA

  2. in ClassA hold a variable (example string str)

  3. create an editor for ClassA (ClassAEditor)

  4. in ClassAEditor hold a GameObject

  5. when a GameObject is placed in a ObjectField get the GameObject.name

  6. place the GameObject.name into ClassA.str

  7. lose focus on the GameObject holding ClassA then return focus (the GameObject field should read null(none)

  8. switch to Debug Inspector

is this intended behavior?

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
Best Answer

Answer by gardian06 · Apr 15, 2013 at 02:54 PM

it would seem that the member variables of a custom inspector are shared across all instances of that script to be inspected, so what this means is if I have a ScriptX, and then I have a ScriptXEditor and ScriptX is applied to 5 different objects. there are 5 instances of ScriptX, but there is still only 1 instance of ScriptXEditor; therefore whenever focus is lost on an object with an instance of the ScriptX all of the member variables of ScriptXEditor will be reset to their default values.

this seems to be intended behavior for what seems to be as the values, and references held by a custom inspector are only really meant to be used in the editor and by minimizing the amount of thing that needs to be stored in memory while in the editor decreases performance demands from O(n) to O(1) per member per script instance and considering that the editor is already more demanding then say a release build.

also it seems to be expected that for most things that will be stored as members in a custom inspector should either be calculated, or only for setup purposes. if a value to be viewable in the custom inspector can not be calculated it must be stored in the script the editor script references.

 //DummyScript
 using UnityEngine;
 using System.Collections;
 
 public class DummyScript : MonoBehaviour {
     public int maintain = 5;
 }


 //DummyScriptEditor
 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(DummyScript))]
 public class DummyScriptEditor : Editor{
     DummyScript dummy;
     public int dummyMember = 10;
     
     public void OnEnable(){
         dummy = (DummyScript)target;
     }
     
     public override void OnInspectorGUI(){
         dummy.maintain = EditorGUILayout.IntField("maintain", dummy.maintain);
         dummyMember = EditorGUILayout.IntField("Editor Member", dummyMember);
         
         if(GUILayout.Button("Apply member to maintain")){
             dummy.maintain = dummyMember;
         }
         
         if(GUILayout.Button("Apply maintain to member")){
             dummyMember = dummy.maintain;
         }
         
         // all other editor code goes before this
         if(GUI.changed){ EditorUtility.SetDirty(dummy);}
     }
 }

to test

  • create a scene, and place any number of empty GameObjects in the scene, and attach the DummyScript to each.

  • if you modify maintain when focus is lost it the new value will stay.

  • if you modify Editor Member when focus is lost the value will reset to default

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
avatar image
0

Answer by zoeyschlemper · Nov 03, 2020 at 06:12 PM

One thing that no one talks about is the core function of an Inspector. Inspectors just display data contained in the asset being inspected. So each time the Inspector gains focus and calls OnInspectorGUI, you have to grab a reference to the instance of the object being inspected and fill in your custom fields with data from it. Otherwise, even if you successfully edit the asset through your Inspector, the default values will always be shown and NOT the current values on the asset.


Then, when the editing session finishes, you do what gardian06 says, which is to tell Unity that the asset has changed since last time, and that data needs to persist after the session ends.


These two things are the core functionality of a custom Inspector.

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

11 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

Related Questions

Adapt a behavior tree graph structure to quest system 0 Answers

Custom editor few derived types 1 Answer

Custom Inspector Array Elements Not Updating 1 Answer

Enumerations in custom inspector C# 2 Answers

Adding a component in editor mode by selecting an Enum. 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