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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Property drawer / attribute can't being serialized

Discussion in 'Scripting' started by unity_R84Ylyu35MIx_g, Apr 7, 2020.

  1. unity_R84Ylyu35MIx_g

    unity_R84Ylyu35MIx_g

    Joined:
    Nov 9, 2019
    Posts:
    2
    Hello everyone !

    I'm trying to code a property attribute [ InputeName ] that allow to get all Project Settings input axes name into a dropdown list in the inspector to choose the value of componant field like the picture below showing it.



    It work perfectly except one major problem... when I enter play mode or change scene, dropdown list are reset to the first element of the list ( horizontal). I think it's because something isn't serialized but I don't know what and why. I'm totally new at editor coding and after research on the net I found people with the same problem as me but the solution provided don't work for me. This is mu code :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class InputNameAttribute : PropertyAttribute
    4. {
    5.     [SerializeField]
    6.     public string inputName;
    7.  
    8. }
    9.  
    10. -----------------------------------------------
    11. -----------------------------------------------
    12.  
    13. using UnityEditor;
    14. using UnityEngine;
    15.  
    16. [CustomPropertyDrawer(typeof(InputNameAttribute))]
    17. public class InputNameDrawer : PropertyDrawer
    18. {
    19.     private int index = 0;
    20.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    21.     {
    22.         InputNameAttribute inputNameAttribute = attribute as InputNameAttribute;
    23.         if (!property.propertyType.Equals(SerializedPropertyType.String))
    24.         {
    25.             EditorGUI.LabelField(position, label.text, "Input name attribute only work with string type.");
    26.         }
    27.         else
    28.         {
    29.             string[] list = GetInputManagerAxisList();
    30.             index  = EditorGUI.Popup(position, property.displayName, index, list);
    31.             property.stringValue = list[index];
    32.         }
    33.  
    34.     }
    35.  
    36.     private string[] GetInputManagerAxisList()
    37.     {
    38.         SerializedObject inputManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
    39.         SerializedProperty axis = inputManager.FindProperty("m_Axes");
    40.         string[] inputNameList = new string[axis.arraySize];
    41.         int i = 0;
    42.         foreach (SerializedProperty axe in axis)
    43.         {
    44.             inputNameList[i++] = axe.FindPropertyRelative("m_Name").stringValue;
    45.         }
    46.  
    47.         return inputNameList;
    48.     }
    49. }
     
  2. kasztelan

    kasztelan

    Joined:
    Nov 3, 2014
    Posts:
    11
    Code (CSharp):
    1. private int index = 0;
    You're resetting the index every time you instantiate the property drawer. Instead of that you need to find the index that should be selected and pass that to Popup.

    Code (CSharp):
    1. int index = Array.IndexOf(list, property.displayName);