8000 tween tools added · coderDarren/UnityCore@e8a74ba · GitHub
[go: up one dir, main page]

Skip to content

Commit e8a74ba

Browse files
committed
tween tools added
1 parent bc39fb5 commit e8a74ba

10 files changed

+390
-0
lines changed

Tween/ColorTween.cs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Tween {
6+
7+
/// <summary>
8+
/// ColorTween is a MonoBehaviour wrapper class for Color tween objects
9+
/// It is responsible for responding to events from the tween process
10+
/// </summary>
11+
public abstract class ColorTween : MonoBehaviour
12+
{
13+
14+
public Keyframe<Color>[] keys;
15+
public float duration;
16+
public bool wrap;
17+
public bool runOnEnable=true;
18+
19+
protected Tweener<Color> m_Tween;
20+
21+
#region Unity Functions
22+
private void OnEnable() {
23+
if (runOnEnable)
24+
StartTween();
25+
}
26+
27+
private void OnDisable() {
28+
StopTween();
29+
}
30+
#endregion
31+
32+
#region Public Functions
33+
public void StartTween() {
34+
Init();
35+
}
36+
37+
public void StopTween() {
38+
Dispose();
39+
}
40+
#endregion
41+
42+
#region Override Functions
43+
protected virtual void Init() {
44+
m_Tween = new Tweener<Color>(keys, duration, wrap);
45+
if (m_Tween.Loop != null) {
46+
m_Tween.OnSetValue += OnSetValue;
47+
m_Tween.OnMoveValue += OnMoveValue;
48+
StartCoroutine(m_Tween.Loop);
49+
}
50+
}
51+
52+
protected virtual void Dispose() {
53+
if (m_Tween.Loop != null) {
54+
m_Tween.OnSetValue -= OnSetValue;
55+
m_Tween.OnMoveValue -= OnMoveValue;
56+
StopCoroutine(m_Tween.Loop);
57+
}
58+
}
59+
60+
protected abstract void OnSetValue(Color _val);
61+
protected abstract void OnMoveValue(Color _curr, Color _target, float _nTime);
62+
#endregion
63+
}
64+
}

Tween/FloatTween.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Tween {
6+
7+
/// <summary>
8+
/// FloatTween is a MonoBehaviour wrapper class for float tween objects
9+
/// It is responsible for responding to events from the tween process
10+
/// </summary>
11+
public abstract class FloatTween : MonoBehaviour
12+
{
13+
14+
public Keyframe<float>[] keys;
15+
public float duration;
16+
public bool wrap;
17+
18+
protected Tweener<float> m_Tween;
19+
20+
#region Unity Functions
21+
private void OnEnable() {
22+
Init();
23+
}
24+
25+
private void OnDisable() {
26+
Dispose();
27+
}
28+
#endregion
29+
30+
#region Override Functions
31+
protected virtual void Init() {
32+
m_Tween = new Tweener<float>(keys, duration, wrap);
33+
if (m_Tween.Loop != null) {
34+
m_Tween.OnSetValue += OnSetValue;
35+
m_Tween.OnMoveValue += OnMoveValue;
36+
StartCoroutine(m_Tween.Loop);
37+
}
38+
}
39+
40+
protected virtual void Dispose() {
41+
if (m_Tween.Loop != null) {
42+
m_Tween.OnSetValue -= OnSetValue;
43+
m_Tween.OnMoveValue -= OnMoveValue;
44+
StopCoroutine(m_Tween.Loop);
45+
}
46+
}
47+
48+
protected abstract void OnSetValue(float _val);
49+
protected abstract void OnMoveValue(float _curr, float _target, float _nTime);
50+
#endregion
51+
}
52+
}

Tween/ImageColorTween.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+

2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
namespace Tween {
6+
7+
[RequireComponent(typeof(Image))]
8+
public class ImageColorTween : ColorTween
9+
{
10+
private Image m_Image;
11+
12+
#region Override Functions
13+
protected override void Init() {
14+
m_Image = GetComponent<Image>();
15+
base.Init();
16+
}
17+
18+
protected override void OnSetValue(Color _val) {
19+
m_Image.color = _val;
20+
}
21+
22+
protected override void OnMoveValue(Color _curr, Color _target, float _nTime) {
23+
m_Image.color = Color.Lerp(_curr, _target, _nTime);
24+
}
25+
#endregion
26+
27+
}
28+
29+
}

Tween/ImageFillTween.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+

2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
namespace Tween {
6+
7+
[RequireComponent(typeof(Image))]
8+
public class ImageFillTween : FloatTween
9+
{
10+
private Image m_Image;
11+
12+
#region Override Functions
13+
protected override void Init() {
14+
m_Image = GetComponent<Image>();
15+
base.Init();
16+
}
17+
18+
protected override void OnSetValue(float _val) {
19+
m_Image.fillAmount = _val;
20+
}
21+
22+
protected override void OnMoveValue(float _curr, float _target, float _nTime) {
23+
m_Image.fillAmount = Mathf.Lerp(_curr, _target, _nTime);
24+
}
25+
#endregion
26+
27+
}
28+
29+
}

Tween/Keyframe.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+

2+
using UnityEngine;
3+
4+
namespace Tween {
5+
6+
[System.Serializable]
7+
public class Keyframe<T>
8+
{
9+
public T value;
10+
[Range(0,1)]
11+
public float nTime;
12+
}
13+
}

Tween/PositionTween.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
using UnityEngine;
3+
4+
namespace Tween {
5+
6+
public class PositionTween : Vector3Tween
7+
{
8+
#region Override Functions
9+
protected override void OnSetValue(Vector3 _val) {
10+
transform.localPosition = _val;
11+
}
12+
13+
protected override void OnMoveValue(Vector3 _curr, Vector3 _target, float _nTime) {
14+
transform.localPosition = Vector3.Lerp(_curr, _target, _nTime);
15+
}
16+
#endregion
17+
18+
}
19+
20+
}

Tween/RotationTween.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
using UnityEngine;
3+
4+
namespace Tween {
5+
6+
public class RotationTween : Vector3Tween
7+
{
8+
#region Override Functions
9+
protected override void OnSetValue(Vector3 _val) {
10+
transform.localRotation = Quaternion.Euler(_val);
11+
}
12+
13+
protected override void OnMoveValue(Vector3 _curr, Vector3 _target, float _nTime) {
14+
transform.localRotation = Quaternion.Euler(Vector3.Lerp(_curr, _target, _nTime));
15+
}
16+
#endregion
17+
18+
}
19+
20+
}

Tween/ScaleTween.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
using UnityEngine;
3+
4+
namespace Tween {
5+
6+
public class ScaleTween : Vector3Tween
7+
{
8+
#region Override Functions
9+
protected override void OnSetValue(Vector3 _val) {
10+
transform.localScale = _val;
11+
}
12+
13+
protected override void OnMoveValue(Vector3 _curr, Vector3 _target, float _nTime) {
14+
transform.localScale = Vector3.Lerp(_curr, _target, _nTime);
15+
}
16+
#endregion
17+
18+
}
19+
20+
}

Tween/Tweener.cs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Tween {
6+
7+
public class Tweener<T>
8+
{
9+
10+
public delegate void SetValueDelegate(T _val);
11+
public delegate void MoveValueDelegate(T _curr, T _target, float _nTime);
12+
public event SetValueDelegate OnSetValue;
13+
public event MoveValueDelegate OnMoveValue;
14+
15+
private Keyframe<T>[] m_Keys;
16+
private float m_Duration;
17+
private IEnumerator m_Loop;
18+
private int m_Index = -1;
19+
private bool m_Wrap;
20+
21+
public IEnumerator Loop {
22+
get {
23+
return m_Loop;
24+
}
25+
}
26+
27+
#region Constructors
28+
public Tweener(Keyframe<T>[] _keys, float _duration, bool _wrap) {
29+
m_Keys = _keys;
30+
m_Duration = _duration;
31+
m_Wrap = _wrap;
32+
33+
if (m_Keys.Length > 0) {
34+
m_Loop = RunTween();
35+
}
36+
}
37+
#endregion
38+
39+
#region Private Functions
40+
private IEnumerator RunTween() {
41+
var _key = GetNextKey();
42+
var _nextKey = GetNextKey();
43+
float _timer = 0;
44+
float _time = Mathf.Abs(_nextKey.nTime - _key.nTime) * m_Duration;
45+
46+
SetValue(_key.value);
47+
48+
// run until coroutine is stopped
49+
while (true) {
50+
_timer += Time.deltaTime;
51+
52+
MoveValue(_key.value, _nextKey.value, _timer / _time);
53+
54+
if (_timer > _time) {
55+
if (m_Wrap) {
56+
_key = _nextKey;
57+
} else {
58+
_key = GetNextKey();
59+
}
60+
_nextKey = GetNextKey();
61+
_time = Mathf.Abs(_nextKey.nTime - _key.nTime) * m_Duration;
62+
_timer = 0;
63+
}
64+
65+
yield return null;
66+
}
67+
}
68+
69+
private Keyframe<T> GetNextKey() {
70+
m_Index ++;
71+
if (m_Index > m_Keys.Length - 1) {
72+
m_Index = 0;
73+
}
74+
return m_Keys[m_Index];
75+
}
76+
77+
private void SetValue(T _val) {
78+
try {
79+
OnSetValue(_val);
80+
} catch (System.Exception) {}
81+
}
82+
83+
private void MoveValue(T _current, T _target, float _nTime) {
84+
try {
85+
OnMoveValue(_current, _target, _nTime);
86+
} catch (System.Exception) {}
87+
}
88+
#endregion
89+
90+
}
91+
}

Tween/Vector3Tween.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Tween {
6+
7+
/// <summary>
8+
/// Vector3Tween is a MonoBehaviour wrapper class for Vector3 tween objects
9+
/// It is responsible for responding to events from the tween process
10+
/// </summary>
11+
public abstract class Vector3Tween : MonoBehaviour
12+
{
13+
14+
public Keyframe<Vector3>[] keys;
15+
public float duration;
16+
public bool wrap;
17+
18+
protected Tweener<Vector3> m_Tween;
19+
20+
#region Unity Functions
21+
private void OnEnable() {
22+
Init();
23+
}
24+
25+
private void OnDisable() {
26+
Dispose();
27+
}
28+
#endregion
29+
30+
#region Override Functions
31+
protected virtual void Init() {
32+
m_Tween = new Tweener<Vector3>(keys, duration, wrap);
33+
if (m_Tween.Loop != null) {
34+
m_Tween.OnSetValue += OnSetValue;
35+
m_Tween.OnMoveValue += OnMoveValue;
36+
StartCoroutine(m_Tween.Loop);
37+
}
38+
}
39+
40+
protected virtual void Dispose() {
41+
if (m_Tween.Loop != null) {
42+
m_Tween.OnSetValue -= OnSetValue;
43+
m_Tween.OnMoveValue -= OnMoveValue;
44+
StopCoroutine(m_Tween.Loop);
45+
}
46+
}
47+
48+
protected abstract void OnSetValue(Vector3 _val);
49+
protected abstract void OnMoveValue(Vector3 _curr, Vector3 _target, float _nTime);
50+
#endregion
51+
}
52+
}

0 commit comments

Comments
 (0)
0