using UnityEditor; using System; using System.Collections.Generic; using Cinemachine.Utility; using System.Linq.Expressions; namespace Cinemachine.Editor { public class BaseEditor : UnityEditor.Editor where T : class { protected T Target { get { return target as T; } } protected SerializedProperty FindAndExcludeProperty(Expression> expr) { SerializedProperty p = FindProperty(expr); ExcludeProperty(p.name); return p; } protected SerializedProperty FindProperty(Expression> expr) { return serializedObject.FindProperty(FieldPath(expr)); } protected string FieldPath(Expression> expr) { return ReflectionHelpers.GetFieldPath(expr); } protected virtual List GetExcludedPropertiesInInspector() { var excluded = new List() { "m_Script" }; if (mAdditionalExcluded != null) excluded.AddRange(mAdditionalExcluded); return excluded; } List mAdditionalExcluded; protected void ExcludeProperty(string propertyName) { if (mAdditionalExcluded == null) mAdditionalExcluded = new List(); mAdditionalExcluded.Add(propertyName); } public override void OnInspectorGUI() { BeginInspector(); DrawRemainingPropertiesInInspector(); } protected virtual void BeginInspector() { mAdditionalExcluded = null; serializedObject.Update(); } protected virtual void DrawPropertyInInspector(SerializedProperty p) { List excluded = GetExcludedPropertiesInInspector(); if (!excluded.Contains(p.name)) { EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(p); if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties(); ExcludeProperty(p.name); } } protected void DrawRemainingPropertiesInInspector() { EditorGUI.BeginChangeCheck(); DrawPropertiesExcluding(serializedObject, GetExcludedPropertiesInInspector().ToArray()); if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties(); } } }