代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class DT : MonoBehaviour
{
public Action<float> action;
float begin;
float end;
float time;
float nowtime;
Action complete;
public static DT To(Action<float> action, float begin, float end, float time)
{
GameObject dt = new GameObject("DT");
DT dotween = dt.AddComponent<DT>();
dotween.action = action;
dotween.begin = begin;
dotween.end = end;
dotween.time = time;
dotween.nowtime = Time.time;
return dotween;
}
// Update is called once per frame
void Update()
{
if((Time.time - nowtime) < time)
{
float t = Time.time - nowtime;
float p = t / time;
float a = begin * (1 - p) + end * p;
action(a);
}
else
{
action(end);
if(complete != null)
{
complete();
}
Destroy(gameObject);
}
if(time>=5)
{
time = 3;
}
}
public DT OnComplete(Action complete)
{
this.complete = complete;
return this;
}
}
具体使用方式如下
using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
DT.To((a) =>
{
transform.position = new Vector3(transform.position.x, a, transform.position.z);
}, transform.position.y, 3, 5);
}
// Update is called once per frame
void Update()
{
}
}