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 Gerrard · May 01, 2015 at 05:05 PM · inspectoroninspectorgui

OnInspectorGui : how to get selected field ?

Hello !

I would like to know if it's possible to get the selected field in OnInspectorGui.

On the first image, it would be the foldout "TriggerZone" and on the second image "TestTriggerZone2".

Why I want to get this selected field ? To allow to remove it when key is down rather than add a "remove" button.

Thanks !

image 1 image 2

selected-1.jpg (22.3 kB)
selected-2.jpg (31.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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DoTA_KAMIKADzE · May 01, 2015 at 05:06 PM

I've had a hard time understanding what exactly you want, so I'll try to give broad answer. I'll provide an example setup of YourEditor:

     [CustomEditor(typeof(YourType))]
     public class YourEditor : Editor
     {
         private YourType _yourCtrl = null;
         void OnEnable()
         {
             _yourCtrl = (YourType)target;
         }
         public override void OnInspectorGUI()
         {
             //blablabla
             _yourCtrl.yourObjectInYourScript = EditorGUILayout.ObjectField(_yourCtrl.yourObjectInYourScript, typeof(SomeType), false) as SomeType;
             //blablabla
         }
     }

As far as I understood you either want to set it to None (as example for ObjectField) or to remove it from displaying.

For the first one you'll need to either set your object to null:

 _yourCtrl.yourObjectInYourScript = null;//anywhere in Editor or the script itself

or to explicitly set it null:

 EditorGUILayout.ObjectField(null, typeof(SomeType), false) as SomeType;

If you just don't want to show that field then add some boolean either in script or youreditor (for example let it be showMyField) and change it to false/true on click on condition or on whatever else you want, and then:

 if (showMyField) EditorGUILayout.ObjectField(null, typeof(SomeType), false) as SomeType;
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 zeman97 · Jan 07, 2016 at 07:55 AM

I'm not sure exactly what you meant, but the title is "How to get selected field?" which I'm going to take to mean "How to get the field that is currently selected in the editor?"

You could use EditorGUIUtility.hotControl which returns the ControlID of the last field the left mouse was clicked on.

You could essentially save the value of hotControl as it updates and compare that to the ID's of the controls you are wanting to check.

 int currentControlID = 0;
 int myFieldControlID = /*Whatever the ID of the field is*/;
 
 if(EditorGUIUtility.hotControl != 0)
     currentControlID = EditorGUIUtility.hotControl;
 
 if(currentControlID == myFieldControlID)
     // Create your popup here.
 
 

As far as getting the ControlID of the fields you want, I would just suggest making a LabelField that displays EditorGUIUtility.hotControl and what what the value is whenever you click on the field you want.

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 uromastix87 · Sep 28, 2017 at 04:26 PM

Here you go. I just encountered this problem. Thanks to zeman97 I was able to get started! using UnityEditor; using UnityEngine; using System.Collections;

 [CustomEditor (typeof(BoxColliderHelper))]
 [CanEditMultipleObjects]
 public class BoxColliderHelperEditor : Editor
 {
   int val1FieldControlValue, val2FieldControlValue, val3FieldControlValue, selectedField;
   float val1 = 0f, val2 = 0f, val3 = 0f;
 
   public override void OnInspectorGUI ()
   {
     val1FieldControlValue = EditorGUIUtility.GetControlID (new GUIContent ("value01"), FocusType.Passive);
     val1 = EditorGUILayout.FloatField (new GUIContent ("value01"), val1);
     val2FieldControlValue = EditorGUIUtility.GetControlID (new GUIContent ("value02"), FocusType.Passive);
     val2 = EditorGUILayout.FloatField ("value02", val2);
     val3FieldControlValue = EditorGUIUtility.GetControlID (new GUIContent ("value03"), FocusType.Passive);
     val3 = EditorGUILayout.FloatField ("value03", val3);
     //get current selected control
     if (EditorGUIUtility.hotControl != 0)
       selectedField = EditorGUIUtility.hotControl - 1;

     //display values
     EditorGUILayout.LabelField (selectedField.ToString ());
     EditorGUILayout.LabelField ("01's label " + val1FieldControlValue.ToString ());
     EditorGUILayout.LabelField ("02's label " + val2FieldControlValue.ToString ());
     EditorGUILayout.LabelField ("03's label " + val3FieldControlValue.ToString ());
   }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a CustomEditor in the inspector and also seeing normal inspector fields 1 Answer

How do you force a custom inspector to redraw? 7 Answers

Callback when custom PropertyAttribute changes 0 Answers

OnInspectorGUI Custom Drawing? 1 Answer

Custom editor desperation: instance of custom class 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