aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs
blob: 08c84fddedf9486961cd15ed0efbe6c989f00bd6 (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;

namespace Lean.Common.Examples
{
	[CanEditMultipleObjects]
	[CustomEditor(typeof(LeanMarker))]
	public class LeanMarker_Inspector : LeanInspector<LeanMarker>
	{
		protected override void DrawInspector()
		{
			BeginError(Any(t => t.Target == null));
				Draw("target");
			EndError();
		}
	}
}
#endif

namespace Lean.Common.Examples
{
	/// <summary>This component marks the Target object using the current GameObject name.
	/// This allows you to quickly find and access it from anywhere using the LeanMarker.Reference component.</summary>
	[ExecuteInEditMode]
	[DisallowMultipleComponent]
	[AddComponentMenu("Lean/Common/Lean Marker")]
	public class LeanMarker : MonoBehaviour
	{
		/// <summary>This struct can be added to your custom components, allowing you to quickly find and efficiently access a marked GameObject.</summary>
		public class Reference<T>
			where T : Object
		{
			public Reference(string newName)
			{
				if (string.IsNullOrEmpty(newName) == true)
				{
					throw new System.ArgumentException("Cannot reference a null or empty marker!");
				}

				name = newName;
			}

			protected string name;

			protected bool cached;

			protected T instance;

			public T Instance
			{
				get
				{
					if (cached == false)
					{
						Find();
					}

					return instance;
				}
			}

			protected virtual void Build(LeanMarker marker)
			{
				if (typeof(T) == typeof(GameObject))
				{
					if (marker.target != null)
					{
						if (marker.target is GameObject)
						{
							instance = (T)marker.target; return;
						}
						else if (marker.target is Component)
						{
							instance = (T)(Object)((Component)marker.target).gameObject; return;
						}
					}
					else
					{
						instance = (T)(Object)marker.gameObject; return;
					}
				}
				else if (typeof(T).IsSubclassOf(typeof(Component)))
				{
					if (marker.target != null)
					{
						if (marker.target is T)
						{
							instance = (T)marker.target; return;
						}
						else if (marker.target is GameObject)
						{
							var component = ((GameObject)marker.target).GetComponent<T>();

							if (component != null)
							{
								instance = component; return;
							}
						}
						else if (marker.target is Component)
						{
							var component = ((Component)marker.target).GetComponent<T>();

							if (component != null)
							{
								instance = component; return;
							}
						}
					}
					else
					{
						var component = marker.gameObject.GetComponent<T>();

						if (component != null)
						{
							instance = component; return;
						}
					}
				}
				else if (marker.target != null && marker.target is T)
				{
					instance = (T)marker.target; return;
				}

				throw new System.MissingMemberException();
			}

			protected void Find()
			{
				var marker = default(LeanMarker);

				if (instances.TryGetValue(name, out marker) == true)
				{
					Build(marker);

					return;
				}
				else
				{
					var markers = FindObjectsOfType<LeanMarker>();

					for (var i = markers.Length - 1; i >= 0; i--)
					{
						marker = markers[i];

						if (marker.name == name)
						{
							Build(marker);

							return;
						}
					}
				}

				throw new System.NullReferenceException("Failed to find LeanMarker in scene with name: " + name);
			}
		}

		/// <summary>This stores all active an enables LeanMarker instances by their GameObject name.</summary>
		private static Dictionary<string, LeanMarker> instances = new Dictionary<string, LeanMarker>();

		/// <summary>The marker is pointing to this Object.</summary>
		public Object Target { set { target = value; } get { return target; } } [SerializeField] private Object target;

		[System.NonSerialized]
		private string registeredName;

		protected virtual void OnEnable()
		{
			registeredName = name;

			instances.Add(registeredName, this);
		}

		protected virtual void OnDisable()
		{
			instances.Remove(registeredName);
		}
#if UNITY_EDITOR
		protected virtual void Reset()
		{
			target = gameObject;
		}
#endif
	}
}