summaryrefslogtreecommitdiff
path: root/Assets/Plugins/Editor/AdvancedInspector/FieldEditors/MonoEditor.cs
blob: 920837941e394b5d2353a68da5359ad634bf0d18 (plain)
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
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using UniToolsEditor;

namespace AdvancedInspector
{
    public class MonoEditor : FieldEditor
    {
        public override bool EditDerived
        {
            get { return false; }
        }

        public override Type[] EditedTypes
        {
            get { return new Type[] { typeof(UnityEditor.MonoScript) }; }
        }

        public override void Draw(InspectorField field, GUIStyle style)
        {
            if (field.SerializedProperty == null || field.SerializedProperty.serializedObject == null)
                return;

            Type type = field.SerializedInstances[0].GetType();
            if (typeof(ComponentMonoBehaviour).IsAssignableFrom(type))
                GUILayout.Label(type.Name);
            else
            {
                EditorGUI.BeginChangeCheck();

                field.SerializedProperty.serializedObject.Update();
                EditorGUILayout.PropertyField(field.SerializedProperty, new GUIContent(""));
                field.SerializedProperty.serializedObject.ApplyModifiedProperties();

                if (EditorGUI.EndChangeCheck())
                {
                    if (field.Parent != null)
                        field.Parent.RefreshFields();
                    else
                        AdvancedInspectorControl.Editor.Instances = new object[] { field.SerializedProperty.serializedObject.targetObject };
                }
            }

            DrawObjectSelector(field);
        }

        private void DrawObjectSelector(InspectorField field)
        {
            MonoBehaviour behaviour = field.GetValue() as MonoBehaviour;
            if (behaviour == null)
                return;

            List<Component> components = new List<Component>(behaviour.gameObject.GetComponents(field.BaseType));
            if (components.Count == 1)
                return;

            int index = components.IndexOf(behaviour);
            string[] texts = new string[components.Count];

            for (int i = 0; i < components.Count; i++)
                texts[i] = i.ToString() + " : " + components[i].ToString();

            EditorGUILayout.BeginHorizontal();
            int selection = EditorGUILayout.Popup(index, texts);
            EditorGUILayout.EndHorizontal();

            if (selection == index)
                return;

            field.SetValue(components[selection]);
        }
    }
}