VirtualFramework/Assets/Scripts/Actions/AnimationAction.cs

178 lines
5.5 KiB
C#
Raw Normal View History

2025-01-14 10:16:29 +08:00
using DG.Tweening;
2025-01-16 18:48:37 +08:00
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
2024-12-14 18:27:59 +08:00
using System;
2024-12-30 10:29:48 +08:00
using System.Collections.Generic;
2024-12-14 18:27:59 +08:00
using UnityEngine;
2024-12-30 10:29:48 +08:00
using UnityEngine.Rendering.Universal;
2025-01-16 18:48:37 +08:00
using static OperationController;
2024-12-14 18:27:59 +08:00
namespace QFramework
{
internal class AnimationAction : IAction
{
public System.Action OnFinished { get; set; }
private AnimationAction()
{
}
private static readonly SimpleObjectPool<AnimationAction> mPool =
new SimpleObjectPool<AnimationAction>(() => new AnimationAction(), null, 10);
string path;
string animName;
Animation anim;
2024-12-30 10:29:48 +08:00
string frame;
string speed;
string deviceName;
float totalTime;
2025-01-22 10:33:42 +08:00
GameObject obj;
2024-12-30 10:29:48 +08:00
public static AnimationAction Allocate(string path, Dictionary<string, string> datas, System.Action OnFinished = null)
2024-12-14 18:27:59 +08:00
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
2024-12-18 11:17:08 +08:00
retNode.Reset();
2024-12-14 18:27:59 +08:00
retNode.path = path;
2024-12-30 10:29:48 +08:00
retNode.animName = datas.ContainsKey("animName") ? datas["animName"] : "";
retNode.frame = datas.ContainsKey("frame") ? datas["frame"] : "";
retNode.speed = datas.ContainsKey("speed") ? datas["speed"] : "";
retNode.deviceName = datas.ContainsKey("deviceName") ? datas["deviceName"] : "";
2024-12-14 18:27:59 +08:00
retNode.OnFinished = OnFinished;
return retNode;
}
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
2025-01-16 18:48:37 +08:00
TweenerCore<float, float, FloatOptions> animDot;
2024-12-14 18:27:59 +08:00
public void OnStart()
{
if (string.IsNullOrEmpty(deviceName))
{
obj = Utility.FindObj(path);
}
else
{
obj = DeviceController.Instance.GetDeviceObj(deviceName);
}
2024-12-14 18:27:59 +08:00
if (obj != null)
{
2025-01-08 14:56:51 +08:00
if (obj.activeSelf == false)
{
Debug.LogError(obj.name + "<22><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬");
2025-01-22 10:59:25 +08:00
this.Finish();
2025-01-08 14:56:51 +08:00
}
2025-01-22 10:59:25 +08:00
else
2024-12-18 11:17:08 +08:00
{
2025-01-22 10:59:25 +08:00
try
2024-12-31 13:16:06 +08:00
{
2025-01-22 10:59:25 +08:00
anim = obj.GetComponent<Animation>();
2025-01-14 10:16:29 +08:00
2025-01-22 10:59:25 +08:00
if (string.IsNullOrEmpty(frame) == false && frame != "-1")
2025-01-14 10:16:29 +08:00
{
2025-01-22 10:59:25 +08:00
int curFrame = 0;
int.TryParse(frame, out curFrame);
anim.clip = anim[animName].clip;
anim[animName].time = curFrame / anim.clip.frameRate;
2025-01-14 10:16:29 +08:00
anim.Play(animName);
2025-03-05 15:06:06 +08:00
anim[animName].speed = 0;
anim.Sample();
2025-01-22 10:59:25 +08:00
this.Finish();
2025-01-14 10:16:29 +08:00
}
else
2025-01-09 15:52:57 +08:00
{
2025-01-22 10:59:25 +08:00
float curSpeed = 1;
if (string.IsNullOrEmpty(speed) == false)
2025-01-14 10:16:29 +08:00
{
2025-01-22 10:59:25 +08:00
float.TryParse(speed, out curSpeed);
2025-01-14 10:16:29 +08:00
}
2025-01-22 10:59:25 +08:00
if (curSpeed < 0)
{
anim.Play(animName);
anim[animName].normalizedTime = 1;
animDot = DOTween.To(() => anim[animName].normalizedTime, v => anim[animName].normalizedTime = v, 0, anim[animName].length / Math.Abs(curSpeed));
animDot.onComplete = () =>
{
anim.Stop();
};
}
else
{
anim[animName].speed = curSpeed;
anim.Play(animName);
if (anim[animName].wrapMode == WrapMode.Loop)
{
this.Finish();
}
}
totalTime = Math.Abs(anim[animName].length / curSpeed);
2025-01-09 15:52:57 +08:00
}
2025-01-22 10:59:25 +08:00
}
catch (Exception)
{
anim = obj.GetComponent<Animation>();
Debug.LogError($"{path} <20><><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD> {animName} <20><><EFBFBD><EFBFBD>");
2024-12-31 13:16:06 +08:00
}
}
2025-01-22 10:59:25 +08:00
2024-12-31 13:16:06 +08:00
2024-12-14 18:27:59 +08:00
}
else
{
2024-12-31 13:16:06 +08:00
Debug.LogError(<>ҵ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>" + path);
2024-12-14 18:27:59 +08:00
this.Finish();
}
}
2025-01-16 18:48:37 +08:00
2024-12-14 18:27:59 +08:00
public void OnExecute(float dt)
{
totalTime -= Time.deltaTime;
if (anim != null && (anim.isPlaying == false || totalTime <= 0))
2024-12-14 18:27:59 +08:00
{
this.Finish();
}
}
public void OnFinish()
{
}
public void Reset()
{
Status = ActionStatus.NotStart;
Paused = false;
}
public bool Paused { get; set; }
public void Deinit()
{
if (!Deinited)
{
2025-01-22 10:33:42 +08:00
if (obj != null)
{
2025-01-22 11:03:42 +08:00
if (anim!=null)
{
anim.Stop();
}
2025-01-22 10:33:42 +08:00
}
2025-01-16 18:48:37 +08:00
animDot?.Kill();
2024-12-14 18:27:59 +08:00
OnFinished = null;
Deinited = true;
2025-01-22 10:33:42 +08:00
obj = null;
2024-12-14 18:27:59 +08:00
mPool.Recycle(this);
}
}
public bool Deinited { get; set; }
}
}