aboutsummaryrefslogtreecommitdiff
path: root/Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs
diff options
context:
space:
mode:
authorTom Harley2018-09-03 23:34:21 +0100
committerTom Harley2018-09-03 23:36:01 +0100
commit5440734d820606b29a795c46172ade5d7eba8efb (patch)
tree85867f40afb02cfa2a5028b515ed0a195866b7e9 /Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs
downloadpowerless-5440734d820606b29a795c46172ade5d7eba8efb.tar.gz
powerless-5440734d820606b29a795c46172ade5d7eba8efb.zip

Initial commit

Diffstat (limited to 'Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs')
-rw-r--r--Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs79
1 files changed, 79 insertions, 0 deletions
diff --git a/Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs b/Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs
new file mode 100644
index 0000000..88d37ff
--- /dev/null
+++ b/Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs
@@ -0,0 +1,79 @@
+using UnityEditor;
+using System;
+using System.Collections.Generic;
+using Cinemachine.Utility;
+using System.Linq.Expressions;
+
+namespace Cinemachine.Editor
+{
+ public class BaseEditor<T> : UnityEditor.Editor where T : class
+ {
+ protected T Target { get { return target as T; } }
+
+ protected SerializedProperty FindAndExcludeProperty<TValue>(Expression<Func<T, TValue>> expr)
+ {
+ SerializedProperty p = FindProperty(expr);
+ ExcludeProperty(p.name);
+ return p;
+ }
+
+ protected SerializedProperty FindProperty<TValue>(Expression<Func<T, TValue>> expr)
+ {
+ return serializedObject.FindProperty(FieldPath(expr));
+ }
+
+ protected string FieldPath<TValue>(Expression<Func<T, TValue>> expr)
+ {
+ return ReflectionHelpers.GetFieldPath(expr);
+ }
+
+ protected virtual List<string> GetExcludedPropertiesInInspector()
+ {
+ var excluded = new List<string>() { "m_Script" };
+ if (mAdditionalExcluded != null)
+ excluded.AddRange(mAdditionalExcluded);
+ return excluded;
+ }
+
+ List<string> mAdditionalExcluded;
+ protected void ExcludeProperty(string propertyName)
+ {
+ if (mAdditionalExcluded == null)
+ mAdditionalExcluded = new List<string>();
+ mAdditionalExcluded.Add(propertyName);
+ }
+
+ public override void OnInspectorGUI()
+ {
+ BeginInspector();
+ DrawRemainingPropertiesInInspector();
+ }
+
+ protected virtual void BeginInspector()
+ {
+ mAdditionalExcluded = null;
+ serializedObject.Update();
+ }
+
+ protected virtual void DrawPropertyInInspector(SerializedProperty p)
+ {
+ List<string> 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();
+ }
+ }
+}