Skip to content

Commit 3990321

Browse files
authored
Merge pull request #24 from coryleach/dev
Text mesh effect improvements
2 parents 092c2c5 + 79f573a commit 3990321

14 files changed

Lines changed: 237 additions & 166 deletions

Demo/Demo.unitypackage

2.4 KB
Binary file not shown.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Includes a SRP shader for blurring the background of UI panels.
2222
#### Using UnityPackageManager (for Unity 2019.3 or later)
2323
Open the package manager window (menu: Window > Package Manager)<br/>
2424
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
25-
https://github.com/coryleach/UnityGUI.git#3.0.7<br/>
25+
https://github.com/coryleach/UnityGUI.git#3.0.8<br/>
2626

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

2929
Find the manifest.json file in the Packages folder of your project and edit it to look like this:
3030
```js
3131
{
3232
"dependencies": {
33-
"com.gameframe.gui": "https://github.com/coryleach/UnityGUI.git#3.0.7",
33+
"com.gameframe.gui": "https://github.com/coryleach/UnityGUI.git#3.0.8",
3434
...
3535
},
3636
}

Runtime/Effects/FadeInText.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using Gameframe.GUI.Tween;
2-
using TMPro;
1+
using TMPro;
32
using UnityEngine;
43

54
namespace Gameframe.GUI
65
{
76
[RequireComponent(typeof(TextMeshEffectTMPro))]
87
public class FadeInText : PlayableTextMeshEffect, ITextMeshColorEffect
98
{
10-
[SerializeField]
9+
[SerializeField]
1110
private bool smooth;
12-
11+
1312
protected override void AddToManager(TextMeshEffectTMPro effectManager)
1413
{
1514
effectManager.AddColorEffect(this);
@@ -22,14 +21,14 @@ protected override void RemoveFromManager(TextMeshEffectTMPro effectManager)
2221

2322
public void UpdateColorEffect(TMP_CharacterInfo charInfo, ref EffectData data)
2423
{
25-
var left = EaseFunctions.Ease(easeType,1 - Mathf.Clamp01(data.Index - (progress - 0.5f)));
24+
var left = GetEasedTime(ref data);
2625
var leftAlpha = (byte) Mathf.Round(255 * left);
2726
data.SetAlpha(0,leftAlpha);
2827
data.SetAlpha(1,leftAlpha);
29-
28+
3029
if (smooth)
3130
{
32-
var right = EaseFunctions.Ease(easeType, 1 - Mathf.Clamp01(data.Index - (progress - 1f)));
31+
var right = GetEasedTime(ref data, -delayPerCharacter);
3332
var rightAlpha = (byte) Mathf.Round(255 * right);
3433
data.SetAlpha(2,rightAlpha);
3534
data.SetAlpha(3,rightAlpha);
@@ -42,5 +41,3 @@ public void UpdateColorEffect(TMP_CharacterInfo charInfo, ref EffectData data)
4241
}
4342
}
4443
}
45-
46-

Runtime/Effects/MoveInText.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Gameframe.GUI.Tween;
2-
using TMPro;
1+
using TMPro;
32
using UnityEngine;
43

54
namespace Gameframe.GUI
@@ -9,18 +8,24 @@ public class MoveInText : VertexTextMeshEffect
98
{
109
[SerializeField]
1110
private Vector3 startOffset = Vector3.zero;
12-
11+
1312
[SerializeField]
1413
protected Vector3 endOffset = Vector3.one;
1514

1615
public override void UpdateVertexEffect(TMP_CharacterInfo charInfo, ref EffectData data)
1716
{
18-
var t = Mathf.Clamp01(progress - data.Index);
19-
t = EaseFunctions.Ease(easeType, t);
17+
var easeTime = GetEasedTime(ref data);
2018
var delta = endOffset - startOffset;
21-
data.LocalPosition = startOffset + delta * t;
19+
data.LocalPosition = startOffset + delta * easeTime;
20+
}
21+
22+
protected override void OnValidate()
23+
{
24+
base.OnValidate();
25+
if (characterAnimationDuration < 0.1f)
26+
{
27+
characterAnimationDuration = 0.1f;
28+
}
2229
}
2330
}
2431
}
25-
26-

Runtime/Effects/PlayableTextMeshEffect.cs

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,94 @@
22
using Gameframe.GUI.Tween;
33
using UnityEngine;
44
using UnityEngine.Events;
5+
using UnityEngine.Serialization;
56

67
namespace Gameframe.GUI
78
{
89
public abstract class PlayableTextMeshEffect : MonoBehaviour, IPlayableTextMeshEffect
910
{
1011
[SerializeField]
1112
private TextMeshEffectTMPro effectManager;
12-
13+
1314
[SerializeField]
1415
protected bool playOnEnable;
1516

16-
[SerializeField]
17+
[SerializeField]
1718
protected Easing easeType = Easing.Linear;
1819

19-
[SerializeField]
20-
protected float charactersPerSecond = 15;
21-
public float CharacterPerSecond
20+
public Easing EaseType
21+
{
22+
get => easeType;
23+
set => easeType = value;
24+
}
25+
26+
[SerializeField]
27+
protected AnimationCurve customCurve = AnimationCurve.Linear(0, 0, 1, 1);
28+
29+
public AnimationCurve Curve
30+
{
31+
get => customCurve;
32+
set => customCurve = value;
33+
}
34+
35+
[SerializeField][FormerlySerializedAs("charactersPerSecond")]
36+
protected float speed = 15;
37+
public float Speed
38+
{
39+
get => speed;
40+
set => speed = value;
41+
}
42+
43+
[SerializeField][FormerlySerializedAs("startDelayMultiplier")]
44+
protected float delayPerCharacter = 1f;
45+
46+
public float DelayPerCharacter
47+
{
48+
get => delayPerCharacter;
49+
set => delayPerCharacter = value;
50+
}
51+
52+
[SerializeField]
53+
protected float characterAnimationDuration = 1f;
54+
55+
public float CharacterAnimationDuration
2256
{
23-
get => charactersPerSecond;
24-
set => charactersPerSecond = value;
57+
get => characterAnimationDuration;
58+
set => characterAnimationDuration = value;
2559
}
26-
60+
2761
[SerializeField]
2862
protected UnityEvent onComplete = new UnityEvent();
2963
public UnityEvent OnComplete => onComplete;
30-
31-
protected float progress;
32-
33-
private Coroutine coroutine;
3464

35-
public bool IsPlaying => coroutine != null;
36-
65+
private float _progress;
66+
67+
private Coroutine _coroutine;
68+
69+
public bool IsPlaying => _coroutine != null;
70+
71+
protected float GetEasedTime(ref EffectData data, float offset = 0)
72+
{
73+
var characterStartTime = data.Index * delayPerCharacter;
74+
var rawTime = (_progress + offset - characterStartTime) / characterAnimationDuration;
75+
var easeTime = Ease(Mathf.Clamp01(rawTime));
76+
return easeTime;
77+
}
78+
79+
/// <summary>
80+
/// Value goes from 1 to 0 instead of 0 to 1
81+
/// </summary>
82+
/// <param name="data"></param>
83+
/// <param name="offset"></param>
84+
/// <returns></returns>
85+
protected float GetInverseEaseTime(ref EffectData data, float offset = 0)
86+
{
87+
var characterStartTime = data.Index * delayPerCharacter;
88+
var rawTime = (_progress + offset - characterStartTime) / characterAnimationDuration;
89+
var easeTime = Ease(1 - Mathf.Clamp01(rawTime));
90+
return easeTime;
91+
}
92+
3793
protected void OnEnable()
3894
{
3995
if (playOnEnable)
@@ -49,57 +105,67 @@ protected void OnDisable()
49105

50106
public void Play()
51107
{
52-
if (coroutine != null)
108+
if (_coroutine != null)
53109
{
54110
Finish();
55111
}
56-
coroutine = StartCoroutine(Run());
112+
_coroutine = StartCoroutine(Run());
57113
}
58114

59115
public void Finish()
60116
{
61-
if (coroutine != null)
117+
if (_coroutine != null)
62118
{
63119
RemoveFromManager(effectManager);
64-
StopCoroutine(coroutine);
120+
StopCoroutine(_coroutine);
65121
onComplete.Invoke();
66122
}
67-
coroutine = null;
123+
_coroutine = null;
68124
}
69125

70126
protected abstract void AddToManager(TextMeshEffectTMPro effectManager);
71127
protected abstract void RemoveFromManager(TextMeshEffectTMPro effectManager);
72128

129+
protected float Ease(float t)
130+
{
131+
return easeType == Easing.CustomCurve ? customCurve.Evaluate(t) : EaseFunctions.Ease(easeType, t);
132+
}
133+
73134
private IEnumerator Run()
74135
{
75136
AddToManager(effectManager);
76137
yield return RunAnimation();
77138
Finish();
78139
}
79-
140+
80141
private IEnumerator RunAnimation()
81142
{
82-
int characterCount = effectManager.Text.textInfo.characterCount;
83-
progress = 0;
84-
while (progress < characterCount)
143+
var characterCount = effectManager.Text.textInfo.characterCount;
144+
_progress = 0;
145+
var duration = (delayPerCharacter * characterCount) + characterAnimationDuration;
146+
while (_progress < duration)
85147
{
86-
progress += Time.smoothDeltaTime * charactersPerSecond;
148+
_progress += Time.deltaTime * speed;
87149
yield return null;
88150
}
89151
}
90-
91-
private void OnValidate()
152+
153+
protected virtual void OnValidate()
92154
{
93155
if (effectManager == null)
94156
{
95157
effectManager = GetComponent<TextMeshEffectTMPro>();
96158
}
97159

98-
if (charactersPerSecond < 0)
160+
if (speed < 0)
99161
{
100-
charactersPerSecond = 1;
162+
speed = 1;
163+
}
164+
165+
if (characterAnimationDuration < 0.01f)
166+
{
167+
characterAnimationDuration = 0.01f;
101168
}
102169
}
103170
}
104171
}
105-

Runtime/Effects/ScaleInText.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
using Gameframe.GUI.Tween;
2-
using TMPro;
1+
using TMPro;
32
using UnityEngine;
43

54
namespace Gameframe.GUI
65
{
76
[RequireComponent(typeof(TextMeshEffectTMPro))]
87
public class ScaleInText : VertexTextMeshEffect
98
{
10-
[SerializeField]
11-
private Vector3 startScale = Vector3.zero;
12-
13-
[SerializeField]
14-
protected Vector3 endScale = Vector3.one;
9+
[SerializeField] private Vector3 startScale = Vector3.zero;
10+
11+
[SerializeField] protected Vector3 endScale = Vector3.one;
1512

1613
public override void UpdateVertexEffect(TMP_CharacterInfo charInfo, ref EffectData data)
1714
{
18-
var t = Mathf.Clamp01(progress - data.Index);
19-
t = EaseFunctions.Ease(easeType, t);
15+
var easeTime = GetEasedTime(ref data);
2016
var delta = endScale - startScale;
21-
data.LocalScale = startScale + delta * t;
17+
data.LocalScale = startScale + delta * easeTime;
2218
}
2319
}
2420
}
25-
26-

0 commit comments

Comments
 (0)