UI特效文字飞

本文介绍了一个使用Unity实现UI动效的方法,包括文本飞入效果的代码逻辑,通过实例化预设对象并应用Tween动画来实现动态显示消息的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



public class XUICwjTest : Xui
{
    Transform tmpLab;
    List<Transform> copyList = new List<Transform>();


    public Color fromColor = new Color(0f, 1f, 0f, 1f);
    public Color toColor = new Color(0f, 86/255f, 0f, 1f);
    public int AddPosY = 50;
    public float waittime = 0.4f;
    public Transform ParentTranform;
    
    public override void OnInit()
    {
        tmpLab = XGetChild<Transform>("Tmp");
        ParentTranform = this.GetComponent<UIPanel>().transform;
    }


    public static void FlyAwayTextList(Transform parentFom, List<string> txtList)
    {
        if (txtList.Count == 0)
            return;


        if (!XUIManager.Inst.IsOpen<XUICwjTest>())
            XUIManager.Inst.OpenUI<XUICwjTest>();


        XUIManager.Inst.CallUIMethod<XUICwjTest>("FlyAway", parentFom, txtList);
    }


    public void FlyAway(Transform parentFom, List<string> txtList)
    {
        ParentTranform = parentFom;
        ClearCopyList();
        StartCoroutine(CoShowMsg(txtList));
    }
     
    IEnumerator CoShowMsg(List<string> txtList)
    {
        for (int i = 0; i < txtList.Count; ++i )
        {
            Transform copyT = (Transform)Instantiate(tmpLab, tmpLab.transform.position, tmpLab.transform.rotation);
            copyT.parent = ParentTranform;
            copyT.localScale = new Vector3(1, 1, 1);
            UILabel lb = copyT.GetComponent<UILabel>();
            lb.text = txtList[i];
            lb.color = fromColor;
            copyT.gameObject.SetActive(true);
            copyList.Add(copyT);


            foreach (UIWidget widget in copyT.GetComponentsInChildren<UIWidget>())
            {
                widget.color = fromColor;
                TweenPosition.Begin(widget.gameObject, waittime, copyT.transform.localPosition + new Vector3(0, AddPosY, 0));
                TweenColor.Begin(widget.gameObject, waittime/2, toColor);
            }


            yield return new WaitForSeconds(waittime);
            GameObject.Destroy(copyT.gameObject);
            copyList.Remove(copyT);
        }
    }
    
    public void ClearCopyList()
    {
        for (int i = 0; i < copyList.Count; ++i )
        {
            if(copyList[i] != null)
                GameObject.Destroy(copyList[i].gameObject);
        }
        
        copyList.Clear();


        StopAllCoroutines();
    }


    public override void OnClose()
    {
        ClearCopyList();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值