diff options
Diffstat (limited to 'Assets/Packages/Lean/Common/Scripts')
4 files changed, 349 insertions, 0 deletions
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 +{ + /// <summary>This class contains useful methods used in almost all of my code.</summary> + public static class LeanHelper + { + public const string HelpUrlPrefix = "http://carloswilkes.github.io/Documentation/"; + + public const string ComponentPathPrefix = "Lean/"; + + /// <summary>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.</summary> + 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); + } + + /// <summary>This allows you to destroy the target object in game and in edit mode, and it returns null.</summary> + public static T Destroy<T>(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 + /// <summary>This gives you the actual object behind a SerializedProperty given to you by a property drawer.</summary> + public static T GetObjectFromSerializedProperty<T>(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 diff --git a/Assets/Packages/Lean/Common/Scripts/LeanHelper.cs.meta b/Assets/Packages/Lean/Common/Scripts/LeanHelper.cs.meta new file mode 100644 index 0000000..0307ed5 --- /dev/null +++ b/Assets/Packages/Lean/Common/Scripts/LeanHelper.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 74ff7c06330793042a1bb0a7fc0accb5 +timeCreated: 1553135089 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Packages/Lean/Common/Scripts/LeanInspector.cs b/Assets/Packages/Lean/Common/Scripts/LeanInspector.cs new file mode 100644 index 0000000..f82c777 --- /dev/null +++ b/Assets/Packages/Lean/Common/Scripts/LeanInspector.cs @@ -0,0 +1,231 @@ +#if UNITY_EDITOR +using UnityEngine; +using UnityEditor; +using System.Linq; +using System.Collections.Generic; + +namespace Lean.Common +{ + /// <summary>This class allows you to quickly make custom inspectors with common features.</summary> + public class LeanInspector<T> : Editor + where T : Object + { + protected T Target; + + protected T[] Targets; + + private static readonly string[] propertyToExclude = new string[] { "m_Script" }; + + private static List<Color> colors = new List<Color>(); + + private static GUIContent customContent = new GUIContent(); + + public static void BeginError(bool error) + { + BeginError(error, Color.red); + } + + public static void BeginError(bool error, Color color) + { + colors.Add(GUI.color); + + GUI.color = error == true ? color : colors[0]; + } + + public static void EndError() + { + var index = colors.Count - 1; + + GUI.color = colors[index]; + + colors.RemoveAt(index); + } + + public static Rect Reserve() + { + var rect = EditorGUILayout.BeginVertical(); + EditorGUILayout.LabelField(GUIContent.none); + EditorGUILayout.EndVertical(); + + return rect; + } + + public override void OnInspectorGUI() + { + colors.Clear(); + + Target = (T)target; + Targets = targets.Select(t => (T)t).ToArray(); + + EditorGUI.BeginChangeCheck(); + { + EditorGUILayout.Separator(); + + serializedObject.Update(); + + DrawInspector(); + + serializedObject.ApplyModifiedProperties(); + + EditorGUILayout.Separator(); + } + if (EditorGUI.EndChangeCheck() == true) + { + GUI.changed = true; Repaint(); + + Dirty(); + } + } + + public virtual void OnSceneGUI() + { + Target = (T)target; + + DrawScene(); + } + + protected void Each(System.Action<T> update, bool dirty = true) + { + if (dirty == true) + { + Undo.RecordObjects(Targets, "Inspector"); + } + + foreach (var t in Targets) + { + update(t); + } + + if (dirty == true) + { + Dirty(); + } + } + + protected bool Any(System.Func<T, bool> check) + { + foreach (var t in Targets) + { + if (check(t) == true) + { + return true; + } + } + + return false; + } + + protected bool All(System.Func<T, bool> check) + { + foreach (var t in Targets) + { + if (check(t) == false) + { + return false; + } + } + + return true; + } + + protected bool DrawExpand(ref bool expand, string propertyPath, string overrideTooltip = null, string overrideText = null) + { + var rect = Reserve(); + var property = serializedObject.FindProperty(propertyPath); + + customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName; + customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip; + + EditorGUI.BeginChangeCheck(); + + EditorGUI.PropertyField(rect, property, customContent, true); + + var changed = EditorGUI.EndChangeCheck(); + + expand = EditorGUI.Foldout(new Rect(rect.position, new Vector2(25.0f, rect.height)), expand, string.Empty); + + return changed; + } + + protected bool DrawMinMax(string propertyPath, float min, float max, string overrideTooltip = null, string overrideText = null) + { + var property = serializedObject.FindProperty(propertyPath); + var value = property.vector2Value; + + customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName; + customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip; + + EditorGUI.BeginChangeCheck(); + + EditorGUILayout.MinMaxSlider(customContent, ref value.x, ref value.y, min, max); + + if (EditorGUI.EndChangeCheck() == true) + { + property.vector2Value = value; + + return true; + } + + return false; + } + + protected bool DrawEulerAngles(string propertyPath, string overrideTooltip = null, string overrideText = null) + { + var property = serializedObject.FindProperty(propertyPath); + var mixed = EditorGUI.showMixedValue; + + customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName; + customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip; + + EditorGUI.BeginChangeCheck(); + + EditorGUI.showMixedValue = property.hasMultipleDifferentValues; + + var oldEulerAngles = property.quaternionValue.eulerAngles; + var newEulerAngles = EditorGUILayout.Vector3Field(customContent, oldEulerAngles); + + if (oldEulerAngles != newEulerAngles) + { + property.quaternionValue = Quaternion.Euler(newEulerAngles); + } + + EditorGUI.showMixedValue = mixed; + + return EditorGUI.EndChangeCheck(); + } + + protected bool Draw(string propertyPath, string overrideTooltip = null, string overrideText = null) + { + var property = serializedObject.FindProperty(propertyPath); + + customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName; + customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip; + + EditorGUI.BeginChangeCheck(); + + EditorGUILayout.PropertyField(property, customContent, true); + + return EditorGUI.EndChangeCheck(); + } + + protected virtual void DrawInspector() + { + DrawPropertiesExcluding(serializedObject, propertyToExclude); + } + + protected virtual void DrawScene() + { + } + + protected void Dirty() + { + for (var i = targets.Length - 1; i >= 0; i--) + { + EditorUtility.SetDirty(targets[i]); + } + + serializedObject.Update(); + } + } +} +#endif
\ No newline at end of file diff --git a/Assets/Packages/Lean/Common/Scripts/LeanInspector.cs.meta b/Assets/Packages/Lean/Common/Scripts/LeanInspector.cs.meta new file mode 100644 index 0000000..7656035 --- /dev/null +++ b/Assets/Packages/Lean/Common/Scripts/LeanInspector.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: c51842c9aba23b548a212740fe66b48c +timeCreated: 1480552189 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |
