blob: 2a4b0f85de4e4306d5704e7efbc8761e84b46f3d (
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
|
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
}
}
|