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);
            }
        }
    }
    
}

 2.编辑路径动画代码赋值

 下面是代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class PathAnition : MonoBehaviour
{
    public Transform poss;
    public List<Vector3> listsve3 = new List<Vector3>();
    // Start is called before the first frame update
    void Start()
    {      
        for (int i = 0; i < poss.childCount ; i++)
        {         
            listsve3.Add(poss.GetChild(i).position);
        }     
    }
    /// <summary>
    /// 开始设置动画并播放
    /// </summary>
    /// <param name="ve3">路径点</param>
    /// <param name="time_">动画时长</param>
    public void PlayAniMation(Vector3 [] ve3,float time_)
    {
            //参数一: 路径点 数组
            //参数二: 完成动画需要 多少秒
            //参数三: 路径的弧度是曲线还是直线
            // 参数四:路径模式 Full3D
            //参数五: 路径点的精密度 数值越大越精密
            //参数六: 路径线路的颜色
            //SetOptions(true) 路径从头回到原点
            //SetLookAt(0) 路径物体与路径点保持的夹角 取值范围0-1
            //SetLoops(-1) - 1无限循环 循环的次数
            //SetEase(Ease.Linear) 匀速线性运动

        transform.DOPath(ve3, time_, PathType.Linear, PathMode.Full3D, 100, Color.yellow)
           .SetOptions(true).SetLookAt(0).SetLoops(1).SetEase(Ease.Linear);
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown (KeyCode.P ))
        {
            transform.DOPause();  //暂停
        }
        if (Input.GetKeyDown(KeyCode.M))
        {
            transform.DOPlay(); //播放
        }
        if (Input.GetKeyDown(KeyCode.N))
        {
            transform.DORestart();//重新播放
        }
    }
}
摄像机移动动画代码编辑

 

具体实现根据项目自行扩展

 

posted @ 2021-08-25 11:04  剑起苍穹  阅读(546)  评论(0编辑  收藏  举报
/*鼠标点击特效*/