Skip to content

Commit a897f45

Browse files
committed
Add text animation.
1 parent fa400d8 commit a897f45

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* $File: JCS_TextAnimation.cs $
3+
* $Date: 2019-07-18 14:34:29 $
4+
* $Revision: $
5+
* $Creator: Jen-Chieh Shen $
6+
* $Notice: See LICENSE.txt for modification and distribution information
7+
* Copyright © 2019 by Shen, Jen-Chieh $
8+
*/
9+
10+
/* NOTE: If you are using `TextMesh Pro` uncomment this line.
11+
*/
12+
#define TMP_PRO
13+
14+
using System.Collections;
15+
using System.Collections.Generic;
16+
using UnityEngine;
17+
using UnityEngine.UI;
18+
19+
#if TMP_PRO
20+
using TMPro;
21+
#endif
22+
23+
namespace JCSUnity
24+
{
25+
/// <summary>
26+
/// Text animation that will display text accordingly.
27+
/// </summary>
28+
public class JCS_TextAnimation
29+
: MonoBehaviour
30+
{
31+
/* Variables */
32+
33+
[Header("** Chec Variables (JCS_TextAnimation) **")]
34+
35+
[Tooltip("Frame this animation is currently displayed.")]
36+
[SerializeField]
37+
private int mCurrentFrame = 0;
38+
39+
40+
[Header("** Initialize Variables (JCS_TextAnimation) **")]
41+
42+
[Tooltip("Target text renderer.")]
43+
[SerializeField]
44+
private Text mTextContainer = null;
45+
46+
#if TMP_PRO
47+
[Tooltip("Target text renderer.")]
48+
[SerializeField]
49+
private TextMeshPro mTextMesh = null;
50+
#endif
51+
52+
53+
[Header("** Runtime Variables (JCS_TextAnimation) **")]
54+
55+
[Tooltip("Timer active or not active.")]
56+
[SerializeField]
57+
private bool mActive = true;
58+
59+
[Tooltip("Hold All text animation's frame.")]
60+
[TextArea]
61+
public List<string> textFrame = null;
62+
63+
[Tooltip("Seconds per frame.")]
64+
[SerializeField] [Range(0.0f, 30.0f)]
65+
private float mSPF = 0.5f;
66+
67+
// Base timer to display frame.
68+
private float mFrameTimer = 0.0f;
69+
70+
71+
/* Setter/Getter */
72+
73+
public bool Active { get { return this.mActive; } set { this.mActive = value; } }
74+
public Text TextContainer { get { return this.mTextContainer; } set { this.mTextContainer = value; } }
75+
#if TMP_PRO
76+
public TextMeshPro TextMesh { get { return this.mTextMesh; } set { this.mTextMesh = value; } }
77+
#endif
78+
public int CurrentFrame { get { return this.mCurrentFrame; } }
79+
public float SPF { get { return this.mSPF; } set { this.mSPF = value; } }
80+
81+
82+
/* Functions */
83+
84+
private void Awake()
85+
{
86+
// Initialize the first frame.
87+
UpdateTextFrame();
88+
}
89+
90+
private void Update()
91+
{
92+
if (!mActive)
93+
return;
94+
95+
DoTextAnimation();
96+
}
97+
98+
/// <summary>
99+
/// Update the frame text.
100+
/// </summary>
101+
public void UpdateTextFrame()
102+
{
103+
UpdateTextFrame(this.mCurrentFrame);
104+
}
105+
106+
/// <summary>
107+
/// Update the frame text.
108+
/// </summary>
109+
/// <param name="frameIndex"> Frame index to displayed. </param>
110+
public void UpdateTextFrame(int frameIndex)
111+
{
112+
this.mCurrentFrame = frameIndex;
113+
114+
/* Ensure in display range. */
115+
if (this.mCurrentFrame >= textFrame.Count)
116+
this.mCurrentFrame = textFrame.Count - 1;
117+
else if (this.mCurrentFrame < 0)
118+
this.mCurrentFrame = 0;
119+
120+
if (mTextContainer)
121+
mTextContainer.text = textFrame[this.mCurrentFrame];
122+
if (mTextMesh)
123+
mTextMesh.text = textFrame[this.mCurrentFrame];
124+
}
125+
126+
/// <summary>
127+
/// Do the actual text animation here.
128+
/// </summary>
129+
private void DoTextAnimation()
130+
{
131+
mFrameTimer += Time.deltaTime;
132+
133+
if (mFrameTimer < mSPF)
134+
return;
135+
136+
++this.mCurrentFrame;
137+
138+
if (this.mCurrentFrame >= textFrame.Count)
139+
this.mCurrentFrame = 0;
140+
141+
UpdateTextFrame();
142+
143+
mFrameTimer = 0.0f; // Reset timer.
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)