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 :
using UnityEngine;
public class InputNameAttribute : PropertyAttribute
{
[SerializeField]
public string inputName;
}
-----------------------------------------------
-----------------------------------------------
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(InputNameAttribute))]
public class InputNameDrawer : PropertyDrawer
{
private int index = 0;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
InputNameAttribute inputNameAttribute = attribute as InputNameAttribute;
if (!property.propertyType.Equals(SerializedPropertyType.String))
{
EditorGUI.LabelField(position, label.text, "Input name attribute only work with string type.");
}
else
{
string[] list = GetInputManagerAxisList();
index = EditorGUI.Popup(position, property.displayName, index, list);
property.stringValue = list[index];
}
}
private string[] GetInputManagerAxisList()
{
SerializedObject inputManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
SerializedProperty axis = inputManager.FindProperty("m_Axes");
string[] inputNameList = new string[axis.arraySize];
int i = 0;
foreach (SerializedProperty axe in axis)
{
inputNameList[i++] = axe.FindPropertyRelative("m_Name").stringValue;
}
return inputNameList;
}
}