aboutsummaryrefslogtreecommitdiff
path: root/Assets/Cinemachine/Base/Editor/Utility/BaseEditor.cs
blob: 88d37ff8c35ccde7f92cefd36b072b8873df2974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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();
        }
    }
}