-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHasUIElementInfo.cs
127 lines (96 loc) · 3.47 KB
/
HasUIElementInfo.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#region Header
/* ============================================
* Author : strix
* Initial Creation Date : 2020-06-19
* Summary :
* Template : New Behaviour For ReSharper
============================================ */
#endregion Header
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Object = System.Object;
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine.UI;
#endif
namespace UIFramework
{
[Serializable]
public class UIElementInfo
{
public enum EType
{
Button,
Toggle,
}
[UIHasEnumSelector]
public string strEnumName;
public MonoBehaviour pMono;
public EType eType;
public UIElementInfo(string strEnumName, MonoBehaviour pMono, EType eType)
{
this.strEnumName = strEnumName;
this.pMono = pMono;
this.eType = eType;
}
public override string ToString()
{
return strEnumName;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(UIElementInfo))]
public class UIElementInfoDrawer : PropertyDrawer
{
readonly List<UIElementInfo> _listEnum = new List<UIElementInfo>();
// ========================================================================== //
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Object pObjectOwner = property.serializedObject?.targetObject;
if (pObjectOwner == null)
return;
Component pComponent = pObjectOwner as Component;
if (pComponent == null)
return;
_listEnum.Clear();
MonoBehaviour[] arrMono = pComponent.GetComponents<MonoBehaviour>();
foreach (var pMono in arrMono)
{
if (pMono == null)
continue;
Button[] arrButton = pMono.GetComponentsInChildren<Button>(true);
_listEnum.AddRange(arrButton.Select(pButton => new UIElementInfo(pButton.name, pMono, UIElementInfo.EType.Button)));
}
if (_listEnum.Count == 0)
return;
EditorGUI.BeginProperty(position, label, property);
SerializedProperty pProperty_strEnumName = property.FindPropertyRelative(nameof(UIElementInfo.strEnumName));
int iIndex = CalculateIndex(pProperty_strEnumName);
iIndex = EditorGUI.Popup(position, label.text, iIndex, _listEnum.Select(p => p.strEnumName).ToArray());
SerializedProperty pProperty_pMono = property.FindPropertyRelative(nameof(UIElementInfo.pMono));
pProperty_strEnumName.stringValue = _listEnum[iIndex].strEnumName;
pProperty_pMono.objectReferenceValue = _listEnum[iIndex].pMono;
EditorGUI.EndProperty();
}
// ========================================================================== //
private int CalculateIndex(SerializedProperty property)
{
string strValue = property.stringValue;
if (string.IsNullOrEmpty(strValue))
return 0;
int iIndex = 0;
for (int i = 1; i < _listEnum.Count; i++)
{
if (_listEnum[i].strEnumName == strValue)
{
iIndex = i;
break;
}
}
return iIndex;
}
}
#endif
}