-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlAttribute.cs
More file actions
61 lines (51 loc) · 2 KB
/
ControlAttribute.cs
File metadata and controls
61 lines (51 loc) · 2 KB
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
using UnityEngine;
[System.AttributeUsageAttribute(System.AttributeTargets.Field)]
public class ControlAttribute : System.Attribute
{
public string Name;
public string FullPath;
public static void Process(MonoBehaviour obj)
{
var type = obj.GetType();
var fileds = type.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
foreach(var filed in fileds)
{
var attribute = filed.GetCustomAttributes(typeof(ControlAttribute), true);
if (attribute.Length == 0)
continue;
var controlAttribute = attribute[0] as ControlAttribute;
if (controlAttribute == null)
continue;
if (!string.IsNullOrEmpty(controlAttribute.Name))
{
var control = GetControl(obj.gameObject, controlAttribute.Name, filed.FieldType);
filed.SetValue(obj, control);
continue;
}
if (!string.IsNullOrEmpty(controlAttribute.FullPath))
{
var control = GetControlByFullPath(obj.gameObject, controlAttribute.FullPath, filed.FieldType);
filed.SetValue(obj, control);
continue;
}
}
}
public static Object GetControl(GameObject gameObject, string name, System.Type type)
{
var go = gameObject.GetChildByName(name);
if (go == null)
return null;
if (type == typeof(GameObject))
return go;
return go.GetComponent(type);
}
public static Object GetControlByFullPath(GameObject gameObject, string name, System.Type type)
{
var go = gameObject.transform.FindChild(name);
if (go == null)
return null;
if (type == typeof(GameObject))
return go.gameObject;
return go.GetComponent(type);
}
}