Unity中暂停、继续播放、杀死、正放、倒放Dotween动画

此案例是控制material(材质球)的alpha(透明度)值,对有MeshRenderer的Model外观有不停的渐隐渐显效果

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


public class Demo01 : MonoBehaviour
{
    
    
    /// <summary>
    /// Model的MeshRenderer
    /// </summary>
    public MeshRenderer meshRenderer;

    private void Start()
    {
    
    
        Color color= meshRenderer.material.color;
        color.a = 120f / 255f;

        MyPingPong(meshRenderer.material.color,color);
    }

    private void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.Q))  
        {
    
    
            DOTween.Pause("move");        //只暂停ID为move的动画,可以继续播放
        }
        if (Input.GetKeyDown(KeyCode.W))  
        {
    
    
            DOTween.Play("move");       //继续播放ID为move
        }
        if (Input.GetKeyDown(KeyCode.E))  
        {
    
    
            DOTween.Kill("move");         //杀死ID为move补间动画,不能再播放
        }
        if (Input.GetKeyDown(KeyCode.R))    
        {
    
    
            DOTween.PlayForward("move");      //正放ID为move动画
        }
        if (Input.GetKeyDown(KeyCode.T))
        {
    
    
            DOTween.PlayBackwards("move");      //倒放ID为move动画,倒放不会循环
        }
    }

    /// <summary>
    /// 控制Model渐隐渐显效果
    /// </summary>
    private void MyPingPong(Color a, Color b)
    {
    
    
        DOTween.To(()=>meshRenderer.material.color,x=>meshRenderer.material.color=x,b,0.5f).OnComplete(()=>MyPingPong(b,a)).SetId<Tween>("move");
    }
}

猜你喜欢

转载自blog.csdn.net/jianjianshini/article/details/117781310