Files
Main/Assets/GameAssets/Resources/GameUI/Common/MyTweenAnim/MyTweenPosition.cs
2025-01-25 04:38:09 +08:00

330 lines
9.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Thousandto.Core.Base;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Thousandto.Plugins.Common
{
/// <summary>
/// 自定义的tween动画通过update来驱动不作为组件
/// </summary>
public class MyTweenPosition : MyTween
{
private GameObject _targetGO;
public UnityEngine.GameObject TargetGO
{
get { return _targetGO; }
set { _targetGO = value; }
}
private Vector3 _startPos;
public UnityEngine.Vector3 StartPos
{
get { return _startPos; }
set { _startPos = value; }
}
private Vector3 _endPos;
public UnityEngine.Vector3 EndPos
{
get { return _endPos; }
set { _endPos = value; }
}
private float _duration;
public float Duration
{
get { return _duration; }
set { _duration = value; }
}
private float _deltaTime;
public float DeltaTime
{
get { return _deltaTime; }
set { _deltaTime = value; }
}
private Vector3 _speed;
public Vector3 Speed
{
get { return _speed; }
set { _speed = value; }
}
public MyAction Callback;
public MyAction<MyTween, object> Callback2;
private object _param;
public Vector3 GetCurrentPos()
{
return TargetGO.transform.localPosition;
}
public static MyTweenPosition Create(GameObject go, Vector3 endPos, float duration, MyAction action = null)
{
return Create(go, go.transform.localPosition, endPos, duration, action);
}
public static MyTweenPosition Create(GameObject go, Vector3 startPos, Vector3 endPos, float duration, MyAction action = null)
{
MyTweenPosition myTwen = new MyTweenPosition();
MyTween.Add(myTwen);
myTwen.TargetGO = go;
myTwen.EndPos = endPos;
myTwen.StartPos = startPos;
myTwen.Duration = duration <= 0 ? 0.01f : duration;
myTwen.Enable = true;
myTwen.Callback = action;
Vector3 distance = endPos - startPos;
myTwen.Speed = distance / duration;
myTwen.DeltaTime = 0;
return myTwen;
}
public static MyTweenPosition Create(GameObject go, Vector3 startPos, Vector3 endPos, float duration, MyAction<MyTween, object> action, object param)
{
MyTweenPosition myTwen = new MyTweenPosition();
MyTween.Add(myTwen);
myTwen.TargetGO = go;
myTwen.EndPos = endPos;
myTwen.StartPos = startPos;
myTwen.Duration = duration <= 0 ? 0.01f : duration;
myTwen.Enable = true;
myTwen.Callback2 = action;
myTwen._param = param;
Vector3 distance = endPos - startPos;
myTwen.Speed = distance / duration;
myTwen.DeltaTime = 0;
return myTwen;
}
public override void OnUpdate()
{
base.OnUpdate();
if (!Enable)
{
return;
}
if(TargetGO == null)
{
Enable = false;
return;
}
if (Enable)
{
_deltaTime += Time.deltaTime;
TargetGO.transform.localPosition = StartPos + Speed * _deltaTime;
}
if (_deltaTime >= Duration || _stop)
{
Enable = false;
_deltaTime = 0;
TargetGO.transform.localPosition = EndPos;
if (Callback != null)
{
Callback();
}
if(Callback2 != null)
{
Callback2(this, _param);
}
Callback = null;
}
}
}
public class MyTweenUISlider : MyTween
{
private UISlider _uISlider = null;
public UISlider UISlider
{
get { return _uISlider; }
set { _uISlider = value; }
}
private int _curLevel = 0;
public int CurLevel
{
get { return _curLevel; }
set { _curLevel = value; }
}
private int _maxLeven = 0;
public int MaxLeven
{
get { return _maxLeven; }
set { _maxLeven = value; }
}
private float _duration;
public float Duration
{
get { return _duration; }
set {
_duration = value;
}
}
private float _speed;
public float Speed
{
get { return _speed; }
set { _speed = value; }
}
private float _deltaTime;
public float DeltaTime
{
get { return _deltaTime; }
set { _deltaTime = value; }
}
public int OldLevel = 0;
public float BginValue = 0;
public float EndValue = 0;
public float AllValue = 0;
public MyAction Callback = null;
public MyAction<int> EvertFullCallbcak = null;
public static MyTweenUISlider Creat(UISlider slider,int curlevel,int maxlevel,float endvalue, float duration, MyAction action = null)
{
MyTweenUISlider myTwen = new MyTweenUISlider();
MyTween.Add(myTwen);
myTwen.UISlider = slider;
myTwen.CurLevel = curlevel;
myTwen.OldLevel = curlevel;
myTwen.MaxLeven = maxlevel;
myTwen.BginValue = slider.value;
myTwen.EndValue = endvalue;
myTwen.Duration = duration;
myTwen.Speed = 1 / duration;
myTwen.AllValue = maxlevel == curlevel ? endvalue : (maxlevel - curlevel + endvalue);
myTwen.Callback = action;
myTwen.Enable = true;
myTwen.DeltaTime = 0;
return myTwen;
}
public static MyTweenUISlider CreatEvertfull(UISlider slider, int curlevel, int maxlevel, float endvalue, float duration, MyAction<int> action = null)
{
MyTweenUISlider myTwen = new MyTweenUISlider();
MyTween.Add(myTwen);
myTwen.UISlider = slider;
myTwen.CurLevel = curlevel;
myTwen.OldLevel = curlevel;
myTwen.MaxLeven = maxlevel;
myTwen.BginValue = slider.value;
myTwen.EndValue = endvalue;
myTwen.Duration = duration;
myTwen.Speed = 1 / duration;
myTwen.AllValue = maxlevel == curlevel ? endvalue : ( maxlevel - curlevel + endvalue);
myTwen.EvertFullCallbcak = action;
myTwen.Enable = true;
myTwen.DeltaTime = 0;
return myTwen;
}
public void StopUISlider()
{
Enable = false;
if (Callback != null)
{
Callback();
}
}
public override void OnUpdate()
{
base.OnUpdate();
if (!Enable)
{
return;
}
DeltaTime += Time.deltaTime;
var value = Mathf.Lerp(BginValue, AllValue,DeltaTime * Speed);
if (value % 1 < UISlider.value && UISlider.value!= 1)
{
UISlider.value = 1;
}
else
{
UISlider.value = value % 1;
}
if ((int)value > 1)
{
var level = Mathf.Lerp(OldLevel, MaxLeven, DeltaTime * Speed);
if (CurLevel != (int)level)
{
CurLevel = (int)level;
if (EvertFullCallbcak != null)
{
EvertFullCallbcak(CurLevel);
}
}
}
if (value == AllValue)
{
Enable = false;
DeltaTime = 0;
UISlider.value = EndValue;
if (Callback != null)
{
Callback();
}
}
}
}
public abstract class MyTween
{
protected bool Enable = false;
protected bool _stop = false;
private static List<MyTween> _tweenList;
public static void Add(MyTween tween)
{
if (_tweenList == null)
{
_tweenList = new List<MyTween>();
}
//每次放最前面
_tweenList.Insert(0, tween);
}
public static void Update()
{
if (_tweenList == null || _tweenList.Count == 0)
{
return;
}
for (int i = _tweenList.Count - 1; i >= 0; --i)
{
if (!_tweenList[i].Enable)
{
_tweenList.RemoveAt(i);
}
else
{
_tweenList[i].OnUpdate();
}
}
}
public virtual void OnUpdate() { }
public void Stop()
{
_stop = true;
}
}
}