aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs
diff options
context:
space:
mode:
authorAndrew Lee <alee14498@gmail.com>2019-08-24 15:24:57 -0400
committerAndrew Lee <alee14498@gmail.com>2019-08-24 15:24:57 -0400
commit85553832ead1a96f88726cd2b35cb6ff1d8b8cc8 (patch)
tree7a2615034462d4296ed09d24038bb4c68107979d /Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs
parente06acf066171670248b0b644c0eb8f6d895e051e (diff)
downloadUnicity-85553832ead1a96f88726cd2b35cb6ff1d8b8cc8.tar.gz
Unicity-85553832ead1a96f88726cd2b35cb6ff1d8b8cc8.tar.bz2
Unicity-85553832ead1a96f88726cd2b35cb6ff1d8b8cc8.zip
Attempt number 2 on localization
Diffstat (limited to 'Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs')
-rw-r--r--Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs187
1 files changed, 187 insertions, 0 deletions
diff --git a/Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs b/Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs
new file mode 100644
index 0000000..08c84fd
--- /dev/null
+++ b/Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs
@@ -0,0 +1,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
+ }
+} \ No newline at end of file