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 --- Assets/Packages/Lean/Common/Scripts/LeanHelper.cs | 94 +++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Assets/Packages/Lean/Common/Scripts/LeanHelper.cs (limited to 'Assets/Packages/Lean/Common/Scripts/LeanHelper.cs') diff --git a/Assets/Packages/Lean/Common/Scripts/LeanHelper.cs b/Assets/Packages/Lean/Common/Scripts/LeanHelper.cs new file mode 100644 index 0000000..2a4b0f8 --- /dev/null +++ b/Assets/Packages/Lean/Common/Scripts/LeanHelper.cs @@ -0,0 +1,94 @@ +using System.Collections; +using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif + +namespace Lean.Common +{ + /// This class contains useful methods used in almost all of my code. + public static class LeanHelper + { + public const string HelpUrlPrefix = "http://carloswilkes.github.io/Documentation/"; + + public const string ComponentPathPrefix = "Lean/"; + + /// This gives you the time-independent 't' value for lerp when used for dampening. This returns 1 in edit mode, or if dampening is less than 0. + public static float DampenFactor(float dampening, float elapsed) + { + if (dampening < 0.0f) + { + return 1.0f; + } +#if UNITY_EDITOR + if (Application.isPlaying == false) + { + return 1.0f; + } +#endif + return 1.0f - Mathf.Exp(-dampening * elapsed); + } + + /// This allows you to destroy the target object in game and in edit mode, and it returns null. + public static T Destroy(T o) + where T : Object + { + if (o != null) + { +#if UNITY_EDITOR + if (Application.isPlaying == true) + { + Object.Destroy(o); + } + else + { + Object.DestroyImmediate(o); + } +#else + Object.Destroy(o); +#endif + } + + return null; + } +#if UNITY_EDITOR + /// This gives you the actual object behind a SerializedProperty given to you by a property drawer. + public static T GetObjectFromSerializedProperty(object target, SerializedProperty property) + { + var tokens = property.propertyPath.Replace(".Array.data[", ".[").Split('.'); + + for (var i = 0; i < tokens.Length; i++) + { + var token = tokens[i]; + var type = target.GetType(); + + if (target is IList) + { + var list = (IList)target; + var index = int.Parse(token.Substring(1, token.Length - 2)); + + target = list[index]; + } + else + { + while (type != null) + { + var field = type.GetField(token, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic); + + if (field != null) + { + target = field.GetValue(target); + + break; + } + + type = type.BaseType; + } + } + } + + return (T)target; + } +#endif + } +} \ No newline at end of file -- cgit v1.2.3