|
| 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 | +} |
0 commit comments