Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to get a variable is highlighted in Inspector from script

Discussion in 'Scripting' started by kINGlu, May 17, 2021.

  1. kINGlu

    kINGlu

    Joined:
    Apr 15, 2013
    Posts:
    8
    Hello everyone, I have a problem that needs to know the status of all the variables in the Inspector.. So is there any way to access it? To judge a variable in the Inspector is selected(focus, blue highlight etc) or not from C# script?
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    The variables you see at the inspector are not really the variables unity is using in runtime.
    Unity works with a serialized copy of your class.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MyMonoBehaviour : MonoBehaviour
    4. {
    5.     public int a = 4;
    6.     public NotSerializableStruct b = default;
    7.     public SerializableStruct c = default;
    8.    
    9.     [SerializeField] int d = 4;
    10.     int e = 4;
    11.    
    12.     void Update()
    13.     {
    14.         int f = 4;
    15.         //...
    16.     }
    17. }
    18.  
    19. public struct NotSerializableStruct
    20. {
    21.     public int value = 4;
    22. }
    23.  
    24. [System.Serializable]
    25. public struct SerializableStruct
    26. {
    27.     public int value = 4;
    28. }
    In this short code, a, c and d are serializables and unity shows it by default at the inspector, while b, e and f are not.

    So if you want to see your variables at the default inspector, they will need to make them serializables.

    Anyway, I'm thinking that what you want to do is to debug the code. The way to debug the code depends on the IDE you are using to create your code, if you tell us what it is we can guide you through the process, but surely you have countless tutorials to do it at the net, as it is a fairly common process while programming and you will use constantly once you figure out how it works.
     
  3. kINGlu

    kINGlu

    Joined:
    Apr 15, 2013
    Posts:
    8
    I know that all the things ars serializables at the inspector, actually I konw what u are talking about and I am now working on a custom Inspector, which all the variables are marked as SerializedProperty
    企业微信截图_1621258581798.png
    Like this picture shows, the variable "Multiline" in code is "private SeriallizedPropety _multiline", and it is now being choosen or focused with blue highlight, so my question is this: can i get this status or is there any API to konw that "Multiline" is being focusd from a script? just like "_multiline.isForcusd" (this is just an example)
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    rubcc95 likes this.
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What do you need it for? Maybe there's another roundabout way of accomplishing what you want.
     
    rubcc95 likes this.
  6. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    I've been messing around for a bit and I've ended with this code that seems to do what you want, however as @GroZZleR said, the should be another way in order to get what you expect, since my code uses reflection to get a private field... which unity has private for some reason. Anyway here it is:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Reflection;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. public class MyMonoBehaviour : MonoBehaviour
    8. {
    9.     [SerializeField] int _value = 5;
    10.     [SerializeField] string _text1 = default, _text2 = default, _text3 = default;
    11. }
    12.  
    13.  
    14.  
    15. [CustomEditor(typeof(MyMonoBehaviour))]
    16. public class MyEditor : Editor
    17. {
    18.     SerializedProperty _value, _text1, _text2;
    19.     int valueId, text1Id, text2Id;
    20.  
    21.     private void OnEnable()
    22.     {
    23.         _value = serializedObject.FindProperty("_value");      
    24.         _text1 = serializedObject.FindProperty("_text1");      
    25.         _text2 = serializedObject.FindProperty("_text2");      
    26.     }
    27.  
    28.     public override void OnInspectorGUI()
    29.     {
    30.         var fieldLinker = new Dictionary<int, SerializedProperty>();
    31.  
    32.         EditorGUILayout.PropertyField(_value);
    33.         Add(_value);
    34.  
    35.         EditorGUILayout.PropertyField(_text1);
    36.         Add(_text1);
    37.  
    38.         EditorGUILayout.PropertyField(_text2);
    39.         Add(_text2);
    40.  
    41.         var selectedId = GUIUtility.keyboardControl;
    42.  
    43.         if(fieldLinker.TryGetValue(selectedId, out var property))
    44.             Debug.Log($"Selected id is {selectedId}, it references to {property.displayName}");
    45.  
    46.         serializedObject.ApplyModifiedProperties();  
    47.      
    48.         void Add(SerializedProperty property)
    49.         {
    50.             var lastControlID = GUIUtilityExtra.LastControlID;
    51.             if (fieldLinker.ContainsKey(lastControlID))
    52.                 return;
    53.  
    54.             fieldLinker.Add(lastControlID, property);
    55.         }
    56.     }  
    57. }
    58.  
    59.  
    60.  
    61. public static class GUIUtilityExtra
    62. {
    63.     static readonly Type EDITOR_UTILITY_TYPE = typeof(EditorGUIUtility);
    64.     static readonly FieldInfo LAST_CONTROL_ID_FIELD;
    65.  
    66.     static GUIUtilityExtra()
    67.     {
    68.         LAST_CONTROL_ID_FIELD = EDITOR_UTILITY_TYPE.GetField("s_LastControlID", BindingFlags.Static | BindingFlags.NonPublic);
    69.     }
    70.  
    71.     public static int LastControlID => (int)LAST_CONTROL_ID_FIELD.GetValue(null);  
    72. }
     
    MGhasemi likes this.
  7. kINGlu

    kINGlu

    Joined:
    Apr 15, 2013
    Posts:
    8
    Thank you! EditorGUIUtility.textFieldHasSelection is not the answer, it is true when there is any textFiled is in focus and my question is not belong to textFiled only...LOL, anyway, I also think it's not given yet, so the only way is to find out in the source code...
     
  8. kINGlu

    kINGlu

    Joined:
    Apr 15, 2013
    Posts:
    8
    Thank you for what u've done, your code seems really working for me. What I want to do is when user selected the variable at the custom Inspector, i can display a describtion of this variable to him.. i'm not using EditorGUILayout.trTextField("variable", "tooltip") because this custom Inspector is running in the Unity Play Mode and i found the tooltip is not working in PlayMode, so i choosed this way... actually i m doing another easy way which add a additianal toggle to show the tooltip, it's also works, but seems not so comfortable for the user