using Core.Utilities; using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using TMPro; using TowerDefense.UI.HUD; using UnityEngine; using UnityEngine.UI; public class TextMoveDoTween : MonoBehaviour { public TextMeshProUGUI bloodText; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } /// /// 当前的 /// /// /// /// public void moveBloodText( float x,float y,string text,bool crit = false ) { // 先设置Text的位置到一个标准位置 Vector3 pos = bloodText.transform.position; pos.x = x; pos.y = y; pos.z = 0; bloodText.transform.position = pos; Vector3 sval = bloodText.transform.localScale; sval.x = 1.0f; sval.y = 1.0f; sval.z = 1.0f; bloodText.transform.localScale = sval; bloodText.text = text; this.TextMove(bloodText,crit ); } /// /// 清空当前的DoTweenMove数据并把数据放到Pool中. /// protected void Remove() { Poolable.TryPool(gameObject); } private void TextMove( Graphic graphic,bool crit = false ) { //获得Text的rectTransform,和颜色,并设置颜色微透明 RectTransform rect = graphic.rectTransform; Color color = graphic.color; graphic.color = new Color(color.r, color.g, color.b, 0); //设置一个DOTween队列 Sequence textMoveSequence = DOTween.Sequence(); System.Random rd = new System.Random(); //设置Text移动和透明度的变化值\ float baseTime = 0.3f; if (crit) baseTime = 0.2f; float scaleUp = 1.8f; if (crit) scaleUp = 2.2f; float scaleVec = Screen.height / 2400f; int basey = rd.Next( (int)(100*scaleVec),(int)(180*scaleVec) ); if( crit ) basey = rd.Next((int)(150 * scaleVec), (int)(210 * scaleVec)); int basex; if (crit) basex = rd.Next(-30, 30); else basex = rd.Next(35, 55); Tweener textMove01 = rect.DOMoveY(rect.position.y + basey, baseTime); Tweener textMovex = rect.DOMoveX(rect.position.x + basex, baseTime); Tweener textMove02 = rect.DOMoveY(rect.position.y + basey + (int)(60*scaleVec), baseTime); Tweener textColor01 = graphic.DOColor(new Color(color.r, color.g, color.b, 1), baseTime); Tweener textColor02 = graphic.DOColor(new Color(color.r, color.g, color.b, 0), baseTime); Tweener textScale = rect.DOScale(scaleUp, 0.25f); //Append 追加一个队列,Join 添加一个队列 //中间间隔一秒 //Append 再追加一个队列,再Join 添加一个队列 textMoveSequence.Append(textMove01); textMoveSequence.Join(textMovex); textMoveSequence.Join(textColor01); // 中断一下. textMoveSequence.AppendInterval(0.3f + (float)rd.NextDouble() * 0.3f); // 向上变大淡出 textMoveSequence.Append(textMove02); textMoveSequence.Join(textColor02); textMoveSequence.Join(textScale); textMoveSequence.AppendCallback(Remove); } }