-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoLinker.cs
More file actions
75 lines (68 loc) · 2.75 KB
/
AutoLinker.cs
File metadata and controls
75 lines (68 loc) · 2.75 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
[InitializeOnLoad]
public class AutoLinker
{
static Dictionary<string, GameObject> hierarchyNameToGameObjectMap;
static Dictionary<string, SerializedObject> inspectorFieldNameToSerializedPropertyMap;
static AutoLinker()
{
EditorApplication.delayCall += RunAutoLinker;
}
static void RunAutoLinker()
{
hierarchyNameToGameObjectMap = new Dictionary<string, GameObject>();
inspectorFieldNameToSerializedPropertyMap = new Dictionary<string, SerializedObject>();
SetupHierachyMap();
SetupInspectorMap();
HandleAutoLinking();
}
static void SetupHierachyMap()
{
GameObject[] gameObjects = Object.FindObjectsOfType<GameObject>();
foreach (GameObject gameObject in gameObjects)
{
string key = gameObject.name.ToLower().Replace(" ", "");
hierarchyNameToGameObjectMap.Add(key, gameObject);
}
}
static void SetupInspectorMap()
{
foreach (GameObject gameObject in hierarchyNameToGameObjectMap.Values)
{
Component[] componenents = gameObject.GetComponents<Component>();
foreach (Component component in componenents)
{
if (component == null)
continue;
SerializedObject serializedObject = new SerializedObject(component);
SerializedProperty serializedProperty = serializedObject.GetIterator();
while (serializedProperty.NextVisible(true))
{
string key = serializedProperty.displayName.ToLower().Replace(" ", "");
if (serializedProperty.propertyType == SerializedPropertyType.ObjectReference)
{
if (!inspectorFieldNameToSerializedPropertyMap.ContainsKey(key))
{
inspectorFieldNameToSerializedPropertyMap.Add(key, serializedObject);
}
}
}
}
}
}
static void HandleAutoLinking()
{
foreach (string name in inspectorFieldNameToSerializedPropertyMap.Keys)
{
string key = name.ToLower().Replace(" ", "");
if (hierarchyNameToGameObjectMap.ContainsKey(key))
{
SerializedProperty serializedProperty = inspectorFieldNameToSerializedPropertyMap[key].FindProperty(name);
serializedProperty.objectReferenceValue = hierarchyNameToGameObjectMap[key];
inspectorFieldNameToSerializedPropertyMap[key].ApplyModifiedProperties();
}
}
}
}