Skip to content

Commit 625e3c1

Browse files
authored
Merge pull request #16 from coryleach/dev
Tween extensions
2 parents 1584059 + 726b461 commit 625e3c1

File tree

5 files changed

+155
-10
lines changed

5 files changed

+155
-10
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ Includes a SRP shader for blurring the background of UI panels.
2323
#### Using UnityPackageManager (for Unity 2019.3 or later)
2424
Open the package manager window (menu: Window > Package Manager)<br/>
2525
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
26-
https://github.com/coryleach/UnityGUI.git#3.0.1<br/>
26+
https://github.com/coryleach/UnityGUI.git#3.0.2<br/>
2727

2828
#### Using UnityPackageManager (for Unity 2019.1 or later)
2929

3030
Find the manifest.json file in the Packages folder of your project and edit it to look like this:
3131
```js
3232
{
3333
"dependencies": {
34-
"com.gameframe.gui": "https://github.com/coryleach/UnityGUI.git#3.0.1",
34+
"com.gameframe.gui": "https://github.com/coryleach/UnityGUI.git#3.0.2",
3535
...
3636
},
3737
}
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Threading.Tasks;
2+
using UnityEngine;
3+
4+
namespace Gameframe.GUI.Tween
5+
{
6+
public static class TransformTweenExtensions
7+
{
8+
public static async void DoPosX(this Transform transform, float position, float duration, Easing easing = Easing.Linear)
9+
{
10+
await transform.DoPosXAsync(position, duration, easing).ConfigureAwait(false);
11+
}
12+
13+
public static async void DoPosY(this Transform transform, float position, float duration, Easing easing = Easing.Linear)
14+
{
15+
await transform.DoPosYAsync(position, duration, easing).ConfigureAwait(false);
16+
}
17+
18+
public static async void DoPosZ(this Transform transform, float position, float duration, Easing easing = Easing.Linear)
19+
{
20+
await transform.DoPosYAsync(position, duration, easing).ConfigureAwait(false);
21+
}
22+
23+
public static async Task DoPosXAsync(this Transform transform, float position, float duration, Easing easing = Easing.Linear)
24+
{
25+
var start = transform.position.x;
26+
await TweenExtensions.DoTweenAsync(transform.gameObject.GetInstanceID(), duration, (t) =>
27+
{
28+
var pt = transform.position;
29+
pt.x = Mathf.Lerp(start, position, t);
30+
transform.position = pt;
31+
}, easing);
32+
}
33+
34+
public static async Task DoPosYAsync(this Transform transform, float position, float duration, Easing easing = Easing.Linear)
35+
{
36+
var start = transform.position.y;
37+
await TweenExtensions.DoTweenAsync(transform.gameObject.GetInstanceID(), duration, (t) =>
38+
{
39+
var pt = transform.position;
40+
pt.y = Mathf.Lerp(start, position, t);
41+
transform.position = pt;
42+
}, easing);
43+
}
44+
45+
public static async Task DoPosZAsync(this Transform transform, float position, float duration, Easing easing = Easing.Linear)
46+
{
47+
var start = transform.position.z;
48+
await TweenExtensions.DoTweenAsync(transform.gameObject.GetInstanceID(), duration, (t) =>
49+
{
50+
var pt = transform.position;
51+
pt.z = Mathf.Lerp(start, position, t);
52+
transform.position = pt;
53+
}, easing);
54+
}
55+
56+
public static async Task DoMoveAsync(this Transform transform, Vector3 position, float duration, Easing easing = Easing.Linear)
57+
{
58+
var startPos = transform.position;
59+
await TweenExtensions.DoTweenAsync(transform.gameObject.GetInstanceID(), duration, (t) =>
60+
{
61+
var pt = Vector3.Lerp(startPos, position, t);
62+
transform.position = pt;
63+
}, easing);
64+
}
65+
66+
public static async void DoMove(this Transform transform, Vector3 position, float duration, Easing easing = Easing.Linear)
67+
{
68+
await transform.DoMoveAsync(position, duration, easing).ConfigureAwait(false);
69+
}
70+
71+
public static async Task DoLocalMoveAsync(this Transform transform, Vector3 position, float duration, Easing easing = Easing.Linear)
72+
{
73+
var startPos = transform.position;
74+
await TweenExtensions.DoTweenAsync(transform.gameObject.GetInstanceID(), duration, (t) =>
75+
{
76+
var pt = Vector3.Lerp(startPos, position, t);
77+
transform.localPosition = pt;
78+
}, easing);
79+
}
80+
81+
public static async void DoLocalMove(this Transform transform, Vector3 position, float duration, Easing easing = Easing.Linear)
82+
{
83+
await transform.DoLocalMoveAsync(position, duration, easing).ConfigureAwait(false);
84+
}
85+
86+
public static async Task DoPunchPosition(this Transform transform, Vector3 position, float duration, Easing easing = Easing.Linear)
87+
{
88+
var startPos = transform.position;
89+
await TweenExtensions.DoTweenAsync(transform.gameObject.GetInstanceID(), duration, (t) =>
90+
{
91+
t *= 2;
92+
if (t > 1)
93+
{
94+
t = 2 - t;
95+
}
96+
97+
var pt = Vector3.Lerp(startPos, position, t);
98+
transform.localPosition = pt;
99+
}, easing);
100+
}
101+
102+
103+
}
104+
}

Runtime/Tween/TransformTweenExtensions.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Tween/TweenExtensions.cs

+37-7
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,28 @@ public static void DoKillTweens(this Component obj)
4949
{
5050
CancelTweensForId(obj.GetInstanceID());
5151
}
52-
52+
5353
public static async Task DoTweenAsync(int id, float duration, Action<float> action, Easing easeType = Easing.Linear)
5454
{
5555
await DoTweenAsync(id, duration, _cancellationTokenSource.Token, action, easeType).ConfigureAwait(false);
5656
}
5757

58-
public static async Task DoTweenAsync(int id, float duration, CancellationToken cancellationToken,
59-
Action<float> action, Easing easeType = Easing.Linear)
58+
public static async Task DoPunchTweenAsync(int id, float duration, Action<float> action, Easing easeType = Easing.Linear)
59+
{
60+
await DoPunchTweenAsync(id, duration, _cancellationTokenSource.Token, action, easeType).ConfigureAwait(false);
61+
}
62+
63+
public static Task DoTweenAsync(int id, float duration, CancellationToken cancellationToken, Action<float> action, Easing easeType = Easing.Linear)
64+
{
65+
return DoTweenAsyncWithLerp(InverseLerpFloat, id, duration, cancellationToken, action, easeType);
66+
}
67+
68+
public static Task DoPunchTweenAsync(int id, float duration, CancellationToken cancellationToken, Action<float> action, Easing easeType = Easing.Linear)
69+
{
70+
return DoTweenAsyncWithLerp(PunchInverseLerpFloat, id, duration, cancellationToken, action, easeType);
71+
}
72+
73+
public static async Task DoTweenAsyncWithLerp(Func<float,float,float,float> lerpMethod, int id, float duration, CancellationToken cancellationToken, Action<float> action, Easing easeType = Easing.Linear)
6074
{
6175
var instanceCancellationToken = StartTween(id);
6276

@@ -74,20 +88,36 @@ public static async Task DoTweenAsync(int id, float duration, CancellationToken
7488
}
7589

7690
t += Time.deltaTime;
77-
action?.Invoke(ease.Invoke(Mathf.InverseLerp(0, duration, t)));
91+
action?.Invoke(ease.Invoke(lerpMethod(0, duration, t)));
7892
}
7993

8094
//Just exit immediately if we've stopped playing in editor
8195
if (!Application.isPlaying)
8296
{
8397
return;
8498
}
85-
86-
action?.Invoke(ease.Invoke(1));
99+
100+
action?.Invoke(ease.Invoke(lerpMethod(0, duration, duration)));
87101

88102
CompleteTween(id);
89103
}
90104

105+
private static float InverseLerpFloat(float a, float b, float t)
106+
{
107+
return Mathf.InverseLerp(a, b, t);
108+
}
109+
110+
private static float PunchInverseLerpFloat(float a, float b, float t)
111+
{
112+
var r = Mathf.InverseLerp(a, b, t);
113+
r *= 2;
114+
if (r > 1)
115+
{
116+
r = 2 - r;
117+
}
118+
return r;
119+
}
120+
91121
private static CancellationToken StartTween(int id)
92122
{
93123
var tweenData = GetTweenData(id);
@@ -148,4 +178,4 @@ private class TweenData
148178
public int count;
149179
}
150180
}
151-
}
181+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.gameframe.gui",
33
"displayName": "Gameframe.GUI",
4-
"version": "3.0.1",
4+
"version": "3.0.2",
55
"description": "This is a library of GUI helpers for UGUI \r\nIncludes a panel system that implements a navigation stack. \r\nIncludes a scene transition system. \r\nIncludes a SRP shader for blurring the background of UI panels.",
66
"keywords": [],
77
"author": {

0 commit comments

Comments
 (0)