-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionMethods.cs
109 lines (102 loc) · 4.33 KB
/
ExtensionMethods.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using static WESL.Helper;
namespace WESL
{
public static class ExtensionMethods
{
/// <summary>
/// Resets transform to identity
/// </summary>
/// <param name="t">Unity transform</param>
public static void ResetTransform(this Transform t)
{
t.position = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
}
/// <summary>
/// Move's transform from current position to location in world space
/// </summary>
/// <param name="t">Unity transform</param>
/// <param name="location">Location in world space to move to</param>
/// <param name="time">Time needed to move to position</param>
/// <returns></returns>
public static IEnumerator MoveTransform (this Transform t, Vector3 location, float time)
{
Vector3 start = t.position;
Timer timer = new Timer(time);
while (!timer.IsDone())
{
t.position = Vector3.Lerp(start, location, timer.GetPercentage());
yield return new WaitForEndOfFrame();
}
yield return new WaitForEndOfFrame();
}
//colour extensions
/// <summary>
/// Change the alpha value of the Color by tint.
/// </summary>
/// <param name="color">Unity color</param>
/// <param name="tint">change alpha by -1f to 1f</param>
public static void tintAlpha(this Color color, float tint)
{
color = new Color(color.r, color.g, color.b, Mathf.Clamp(color.a + tint, 0, 1));
}
/// <summary>
/// Returns this Color with the alpha tinted by tint.
/// </summary>
/// <param name="color">Unity color</param>
/// <param name="tint">change alpha by -1f to 1f</param>
/// <returns>this Color tinted by tint</returns>
public static Color withTintAlpha (this Color color, float tint)
{
return new Color(color.r, color.g, color.b, Mathf.Clamp(color.a + tint, 0, 1));
}
/// <summary>
/// Returns this Color with the alpha set to alpha.
/// </summary>
/// <param name="color">Unity color</param>
/// <param name="alpha">set alpha from 0 to 1f</param>
/// <returns>this Color with alpha set to alpha</returns>
public static Color withAlpha (this Color color, float alpha)
{
Mathf.Clamp(alpha, 0, 1);
return new Color(color.r, color.g, color.b, alpha);
}
/// <summary>
/// Returns this Color with alpha interpolated between start and end at percentage.
/// </summary>
/// <param name="color"></param>
/// <param name="start">start alpha value from 0 to 1</param>
/// <param name="end">end alpha value from 0 to 1</param>
/// <param name="percentage">evaluation percentage from 0 to 1</param>
/// <returns>this Color with alpha interpolated between start and end at percentage</returns>
public static Color LerpAlpha (this Color color, float start, float end, float percentage)
{
Mathf.Clamp(start, 0, 1);
Mathf.Clamp(end, 0, 1);
Mathf.Clamp(percentage, 0, 1);
return new Color(color.r, color.g, color.b, Mathf.Lerp(start, end, percentage));
}
/// <summary>
/// Returns this Color with alpha interpolated between start and end at the timer's percentage.
/// </summary>
/// <param name="color"></param>
/// <param name="start">start alpha value from 0 to 1</param>
/// <param name="end">end alpha value from 0 to 1</param>
/// <param name="percentage">timer to evaluate Lerp against</param>
/// <returns>this Color with alpha interpolated between start and end at the timer's percentage</returns>
public static Color LerpAlpha(this Color color, float start, float end, Timer timer)
{
Mathf.Clamp(start, 0, 1);
Mathf.Clamp(end, 0, 1);
return new Color(color.r, color.g, color.b, Mathf.Lerp(start, end, timer.GetPercentage()));
}
}
}