一、动画效果使用animator实现
二、动画效果可拓展可修改
三、文字效果可修改,也可替换为图片
四、解决问题:1、复杂的文字显示效果;2、方便拓展修改;3、多语言版本不需要大量美术工作;
五、实现方法如下:
1、制作预制体;
2、根据字体样式修改文字大小:
3、代码播放动画:
(1)、预制体导出自动的脚本
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// TextBeatCom控件(导出时更新)
/// </summary>
public partial class TextBeatCom : UIComponentBase
{
public ScrollRect m_scroll;
public Animator m_a;
public Animator m_b;
public Animator m_c;
public Animator m_d;
public Animator m_e;
public Animator m_f;
public Animator m_g;
public Animator m_h;
public Animator m_i;
public Animator m_j;
public Animator m_k;
public Animator m_l;
public Animator m_m;
public Animator m_n;
public Animator m_o;
public Animator m_p;
public Animator m_q;
public Animator m_r;
public Animator m_s;
public Animator m_t;
public Animator m_u;
public Animator m_v;
public Animator m_w;
public Animator m_x;
public Animator m_y;
public Animator m_z;
public Animator m_A;
public Animator m_B;
public Animator m_C;
public Animator m_D;
public Animator m_E;
public Animator m_F;
public Animator m_G;
public Animator m_H;
public Animator m_I;
public Animator m_J;
public Animator m_K;
public Animator m_L;
public Animator m_M;
public Animator m_N;
public Animator m_O;
public Animator m_P;
public Animator m_Q;
public Animator m_R;
public Animator m_S;
public Animator m_T;
public Animator m_U;
public Animator m_V;
public Animator m_W;
public Animator m_X;
public Animator m_Y;
public Animator m_Z;
private void Awake()
{
m_scroll = transform.FindInAllChild("scroll_n").GetComponent<ScrollRect>();
m_a = transform.FindInAllChild("a_n").GetComponent<Animator>();
m_b = transform.FindInAllChild("b_n").GetComponent<Animator>();
m_c = transform.FindInAllChild("c_n").GetComponent<Animator>();
m_d = transform.FindInAllChild("d_n").GetComponent<Animator>();
m_e = transform.FindInAllChild("e_n").GetComponent<Animator>();
m_f = transform.FindInAllChild("f_n").GetComponent<Animator>();
m_g = transform.FindInAllChild("g_n").GetComponent<Animator>();
m_h = transform.FindInAllChild("h_n").GetComponent<Animator>();
m_i = transform.FindInAllChild("i_n").GetComponent<Animator>();
m_j = transform.FindInAllChild("j_n").GetComponent<Animator>();
m_k = transform.FindInAllChild("k_n").GetComponent<Animator>();
m_l = transform.FindInAllChild("l_n").GetComponent<Animator>();
m_m = transform.FindInAllChild("m_n").GetComponent<Animator>();
m_n = transform.FindInAllChild("n_n").GetComponent<Animator>();
m_o = transform.FindInAllChild("o_n").GetComponent<Animator>();
m_p = transform.FindInAllChild("p_n").GetComponent<Animator>();
m_q = transform.FindInAllChild("q_n").GetComponent<Animator>();
m_r = transform.FindInAllChild("r_n").GetComponent<Animator>();
m_s = transform.FindInAllChild("s_n").GetComponent<Animator>();
m_t = transform.FindInAllChild("t_n").GetComponent<Animator>();
m_u = transform.FindInAllChild("u_n").GetComponent<Animator>();
m_v = transform.FindInAllChild("v_n").GetComponent<Animator>();
m_w = transform.FindInAllChild("w_n").GetComponent<Animator>();
m_x = transform.FindInAllChild("x_n").GetComponent<Animator>();
m_y = transform.FindInAllChild("y_n").GetComponent<Animator>();
m_z = transform.FindInAllChild("z_n").GetComponent<Animator>();
m_A = transform.FindInAllChild("A_n").GetComponent<Animator>();
m_B = transform.FindInAllChild("B_n").GetComponent<Animator>();
m_C = transform.FindInAllChild("C_n").GetComponent<Animator>();
m_D = transform.FindInAllChild("D_n").GetComponent<Animator>();
m_E = transform.FindInAllChild("E_n").GetComponent<Animator>();
m_F = transform.FindInAllChild("F_n").GetComponent<Animator>();
m_G = transform.FindInAllChild("G_n").GetComponent<Animator>();
m_H = transform.FindInAllChild("H_n").GetComponent<Animator>();
m_I = transform.FindInAllChild("I_n").GetComponent<Animator>();
m_J = transform.FindInAllChild("J_n").GetComponent<Animator>();
m_K = transform.FindInAllChild("K_n").GetComponent<Animator>();
m_L = transform.FindInAllChild("L_n").GetComponent<Animator>();
m_M = transform.FindInAllChild("M_n").GetComponent<Animator>();
m_N = transform.FindInAllChild("N_n").GetComponent<Animator>();
m_O = transform.FindInAllChild("O_n").GetComponent<Animator>();
m_P = transform.FindInAllChild("P_n").GetComponent<Animator>();
m_Q = transform.FindInAllChild("Q_n").GetComponent<Animator>();
m_R = transform.FindInAllChild("R_n").GetComponent<Animator>();
m_S = transform.FindInAllChild("S_n").GetComponent<Animator>();
m_T = transform.FindInAllChild("T_n").GetComponent<Animator>();
m_U = transform.FindInAllChild("U_n").GetComponent<Animator>();
m_V = transform.FindInAllChild("V_n").GetComponent<Animator>();
m_W = transform.FindInAllChild("W_n").GetComponent<Animator>();
m_X = transform.FindInAllChild("X_n").GetComponent<Animator>();
m_Y = transform.FindInAllChild("Y_n").GetComponent<Animator>();
m_Z = transform.FindInAllChild("Z_n").GetComponent<Animator>();
}
}
(2)、预制体实现效果的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public partial class TextBeatCom
{
List<RectTransform> m_showList = new List<RectTransform>();
List<Animator> m_aniList = new List<Animator>();
string m_text = string.Empty;
public void Init(string text)
{
m_text = text;
ResetContent();
ShowText();
}
void ShowText()
{
CreateTextItem();
StartCoroutine(PlayAnimation());
}
void CreateTextItem()
{
float last = 0, move = 0;
int length = m_text.Length;
for (int index = 0; index < length; index++)
{
var tChar = $"{m_text[index]}_n";
Debug.Log($"char = {tChar}");
var child = m_scroll.content.Find(tChar);
if (child != null)
{
var obj = GameObject.Instantiate(child, m_scroll.content.transform);
if (obj != null)
{
//obj.SetActive(true);
var rt = obj.GetComponent<RectTransform>();
move += rt.rect.width / 2 + last / 2;
rt.anchoredPosition = new Vector2(move, 0);
last = rt.rect.width;
m_showList.Add(rt);
m_aniList.Add(obj.GetComponent<Animator>());
}
}
else
{
move += 50;//空格
}
}
if (m_showList.Count > 0) move += (int)m_showList[0].rect.width >> 1;
move = (int)move >> 1;
for (int index = 0; index < m_showList.Count; index++)
{
var obj = m_showList[index];
var pos = obj.anchoredPosition;
pos.x -= move;
obj.anchoredPosition = pos;
}
}
IEnumerator PlayAnimation()
{
yield return PlayShow();
PlayBeat();
}
IEnumerator PlayShow()
{
int playFinishNum = 0;
for (int i = 0; i < m_aniList.Count; i++)
{
var obj = m_aniList[i];
obj.SetActive(true);
obj.OnPlay("Show").OnComplete(() => { playFinishNum++; });
yield return new WaitForSeconds(0.05f);
}
yield return new WaitUntil(() => playFinishNum >= m_aniList.Count);
}
public void PlayBeat()
{
for (int index = 0; index < m_aniList.Count; index++)
{
var obj = m_aniList[index];
obj.Play("Beat");
}
}
public void OnClear()
{
for (int i = 0; i < m_showList.Count; i++)
{
var tran = m_showList[i];
m_showList.Remove(tran);
i--;
GameObject.Destroy(tran.gameObject);
}
m_showList.Clear();
m_aniList.Clear();
}
void ResetContent()
{
for (int index = 0; index < m_scroll.content.childCount; index++)
{
m_scroll.content.GetChild(index).SetActive(false);
}
}
}
六、可以使用其他动画插件