From 85553832ead1a96f88726cd2b35cb6ff1d8b8cc8 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sat, 24 Aug 2019 15:24:57 -0400 Subject: Attempt number 2 on localization --- .../Lean/Common/Examples/Scripts/LeanMarker.cs | 187 +++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs (limited to 'Assets/Packages/Lean/Common/Examples/Scripts/LeanMarker.cs') 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 + { + protected override void DrawInspector() + { + BeginError(Any(t => t.Target == null)); + Draw("target"); + EndError(); + } + } +} +#endif + +namespace Lean.Common.Examples +{ + /// 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. + [ExecuteInEditMode] + [DisallowMultipleComponent] + [AddComponentMenu("Lean/Common/Lean Marker")] + public class LeanMarker : MonoBehaviour + { + /// This struct can be added to your custom components, allowing you to quickly find and efficiently access a marked GameObject. + public class Reference + 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(); + + if (component != null) + { + instance = component; return; + } + } + else if (marker.target is Component) + { + var component = ((Component)marker.target).GetComponent(); + + if (component != null) + { + instance = component; return; + } + } + } + else + { + var component = marker.gameObject.GetComponent(); + + 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(); + + 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); + } + } + + /// This stores all active an enables LeanMarker instances by their GameObject name. + private static Dictionary instances = new Dictionary(); + + /// The marker is pointing to this Object. + 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 -- cgit v1.2.3