Files
Main/Assets/GameAssets/Resources/GameUI/Common/UIAnimationMainger/UIAnimationManager.cs

191 lines
6.0 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00

using Thousandto.Core.Base;
using System.Collections.Generic;
using UnityEngine;
namespace Thousandto.Plugins.Common
{
//UI动画管理器
public class UIAnimationManager
{
#region//常量
//默认动画曲线
public static AnimationCurve NormalCurve = null;
#endregion
#region//私有变量
//正在播放的动画列表
private static List<UIAnimationTrans> _showList = new List<UIAnimationTrans>();
private static List<UIAnimationTrans> _hideList = new List<UIAnimationTrans>();
#endregion
#region//构造函数
static UIAnimationManager()
{
NormalCurve = new AnimationCurve();
var key = new Keyframe();
key.time = 0f;
key.value = 0f;
key.inTangent = 2.68888f;
key.outTangent = 2.68888f;
NormalCurve.AddKey(key);
key = new Keyframe();
key.time = 1f;
key.value = 1f;
key.inTangent = 0f;
key.inTangent = 0f;
NormalCurve.AddKey(key);
}
#endregion
#region//公有函数
public static void Update()
{
for (int i = _showList.Count - 1; i >= 0; --i)
{
_showList[i].Update();
if(_showList[i].IsFinish)
{
_showList.RemoveAt(i);
}
}
for (int i = _hideList.Count - 1; i >= 0; --i)
{
_hideList[i].Update();
if (_hideList[i].IsFinish)
{
if(_hideList[i].HandleTrans != null && _hideList[i].HideGo)
{
_hideList[i].HandleTrans.gameObject.SetActive(false);
}
_hideList.RemoveAt(i);
}
}
}
public static void PlayShowAnimation(Transform handleTrans, MyAction finishCallBack, List<UIAnimationBase> animations)
{
if (handleTrans == null || animations == null || animations.Count <= 0)
{
return;
}
StopAnimation(handleTrans);
handleTrans.gameObject.SetActive(true);
var animTrans = new UIAnimationTrans();
animTrans.HandleTrans = handleTrans;
animTrans.FinishCallBack = finishCallBack;
for (int i = 0; i < animations.Count; ++i)
{
animTrans.AddAnim(animations[i]);
}
_showList.Add(animTrans);
}
public static void PlayHideAnimAtion(Transform handleTrans, MyAction finishCallBack, List<UIAnimationBase> animations, bool hideGo)
{
if (handleTrans == null || animations == null || animations.Count <= 0)
return;
StopAnimation(handleTrans);
var animTrans = new UIAnimationTrans();
animTrans.HandleTrans = handleTrans;
animTrans.FinishCallBack = finishCallBack;
animTrans.HideGo = hideGo;
for (int i = 0; i < animations.Count; ++i)
{
animTrans.AddAnim(animations[i]);
}
_hideList.Add(animTrans);
}
//alpha动画,只能用于Play函数的参数
public static UIAlphaAnimation Alpha(float start, float end, float duration = 0.5f, AnimationCurve curve = null)
{
UIAlphaAnimation result = new UIAlphaAnimation();
result.StartValue = start;
result.EndValue = end;
result.Duration = duration;
if (curve == null)
{
curve = NormalCurve;
}
result.AnimationCurve = curve;
return result;
}
//缩放动画只能用于Play函数
public static UIScaleAnimation Scale(Vector3 start, Vector3 end, float duration = 0.5f, AnimationCurve curve = null)
{
UIScaleAnimation result = new UIScaleAnimation();
result.StartValue = start;
result.EndValue = end;
result.Duration = duration;
if (curve == null)
{
curve = NormalCurve;
}
result.AnimationCurve = curve;
return result;
}
//位移动画只能用于Play函数
public static UIPositionAnimation Position(Vector3 start, Vector3 end, Vector3 offset, float duration = 0.5f, AnimationCurve curve = null)
{
UIPositionAnimation result = new UIPositionAnimation();
result.StartValue = start;
result.EndValue = end;
result.OffsetPos = offset;
result.Duration = duration;
if (curve == null)
{
curve = NormalCurve;
}
result.AnimationCurve = curve;
return result;
}
//旋转动画只能用于Play函数
public static UIRotationAnimation Rotaion(float start, float end, float duration = 0.5f, AnimationCurve curve = null)
{
UIRotationAnimation result = new UIRotationAnimation();
result.StartValue = start;
result.EndValue = end;
result.Duration = duration;
if (curve == null)
{
curve = NormalCurve;
}
result.AnimationCurve = curve;
return result;
}
#endregion
#region//私有函数
public static void StopAnimation(Transform handleTrans)
{
for (int i = _showList.Count - 1; i >= 0; --i)
{
if(_showList[i].HandleTrans == handleTrans)
{
_showList.RemoveAt(i);
}
}
for (int i = _hideList.Count - 1; i >= 0; --i)
{
if (_hideList[i].HandleTrans == handleTrans)
{
_hideList.RemoveAt(i);
}
}
}
#endregion
}
}