Unity DoTweening動態添加動畫


1.簡單記錄一下,不喜勿噴

首先下載DoTween插件 

直接上代碼,代碼不多,好幾個動畫放一起,一起播放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class Systean_BtnAni : MonoBehaviour
{

    public Transform[] tuian;
    private Vector3 H_2_pointx;
    public Button btn;
    public Sprite hig_sprite,nhig_sprite;
    public Sequence se;
    private Vector3 H_3_yidong;
    // Start is called before the first frame update
    void Start()
    {
        H_2_pointx = tuian[5].localPosition;
        H_3_yidong = new Vector3 (-878.2f,271.6f,0);
        btn.onClick.AddListener(delegate {
            TweenCallback completeCallBack = SanJiao_XiaoShi;
            TweenCallback completeCallBack1 = Xianchu;
            TweenCallback completeCallBack2 = Dianchu;
            //動畫的隊列 
            se = DOTween.Sequence();
            //創建的動畫
            Tween tween = tuian[1].DOScale(0, 0.2f);
            Tween tween1 = tuian[5].GetComponent<RectTransform>().DOLocalMove(H_3_yidong, 1f);
            Tween tween2 = tuian[4].DOScale(0.6f, 0.3f).SetLoops(2, LoopType.Yoyo);
            Tween tween3 = tuian[6].GetComponent<Image>().DOFillAmount(1, 0.3f);
            Tween tween4 = tuian[7].DOScale(1.5f, 0.5f).SetLoops(2, LoopType.Yoyo);
            //把動畫添加到隊列里面
            se.Append(tween);
            //插入動畫
            se.AppendCallback(completeCallBack);
            se.Append(tween1);
            se.Append(tween2);
            //插入動畫
            se.AppendCallback(completeCallBack1);
            se.Append(tween3);
            //插入動畫
            se.AppendCallback(completeCallBack2);
            se.Append(tween4);
            //播放隊列里面的動畫  按順序播放
            se.Play();
            btn.GetComponent<Image>().sprite = hig_sprite;
        });
    }
    void SanJiao_XiaoShi()
    {
        tuian[5].gameObject.SetActive(true);
        tuian[4].gameObject.SetActive(true);
        tuian[0].gameObject.SetActive(false);
    }
    void Xianchu()
    {
        tuian[6].gameObject.SetActive(true);
    }
    void Dianchu()
    {
        tuian[7].gameObject.SetActive(true);
    }  
    public void NoDisAble_()
    {
        //動畫停止
        se.Kill();
        btn.GetComponent<Image>().sprite = nhig_sprite;
        tuian[1].localScale = new Vector3(1, 1, 1);
        tuian[5].localPosition = H_2_pointx;
        for (int i = 0; i < tuian.Length ; i++)
        {
            if (i<=3)
            {
                tuian[i].gameObject.SetActive(true);
            }
            else
            {
                tuian[i].gameObject.SetActive(false);
            }
        }
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM