public class Test : MonoBehaviour
{[HideInInspector][Header("数量")][SerializeField]public int num;[Header("分布类型")][SerializeField]public DistributionType distType;[HideInInspector][Header("位置")][SerializeField]public List<Vector3> positions;[Header("test")][SerializeField]List<testType> test;
}[Serializable]
public class testType
{public int id;public string name;
}public enum DistributionType
{None,test1,test2,test3,
}
[CustomEditor(typeof(Test))]
public class TestEditor : Editor {public SerializedObject obj;public SerializedProperty distType;public SerializedProperty positions;public SerializedProperty num;public SerializedProperty test;void OnEnable(){obj = new SerializedObject(target);distType = obj.FindProperty("distType");num = obj.FindProperty("num");positions = obj.FindProperty("positions");test = obj.FindProperty("test");}public override void OnInspectorGUI(){//base.OnInspectorGUI();obj.Update();if (distType.enumValueIndex == 0){EditorGUILayout.PropertyField(positions, true);}else{EditorGUILayout.PropertyField(num);}EditorGUILayout.PropertyField(test,true);obj.ApplyModifiedProperties();}
}
在数据类中(文中是Test类)数组类的字段在开启基类的base.OnInspectorGUI();不能用[HideInInspector],否则会无法显示数组内容。
参考链接:
Unity 自定义inspector界面如何显示数组_360问答 (so.com)
Unity根据条件控制Inspector面板中的属性显示_unity inspect 条件控制-CSDN博客