VirtualFramework/Assets/Scripts/Actions/AnimationAction.cs

157 lines
4.6 KiB
C#
Raw Normal View History

2025-01-14 10:16:29 +08:00
using DG.Tweening;
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;
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;
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; }
public void OnStart()
{
GameObject obj = null;
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>״̬");
}
2024-12-31 13:16:06 +08:00
try
2024-12-18 11:17:08 +08:00
{
2024-12-31 13:16:06 +08:00
anim = obj.GetComponent<Animation>();
if (string.IsNullOrEmpty(frame) == false && frame != "-1")
2024-12-30 10:29:48 +08:00
{
2024-12-31 13:16:06 +08:00
int curFrame = 0;
int.TryParse(frame, out curFrame);
2025-01-02 10:19:41 +08:00
anim.clip = anim[animName].clip;
2024-12-31 13:16:06 +08:00
anim[animName].time = curFrame / anim.clip.frameRate;
anim[animName].speed = 0;
anim.Play(animName);
this.Finish();
2024-12-30 10:29:48 +08:00
}
2024-12-31 13:16:06 +08:00
else
{
float curSpeed = 1;
if (string.IsNullOrEmpty(speed) == false)
{
float.TryParse(speed, out curSpeed);
}
2025-01-14 10:16:29 +08:00
if (curSpeed < 0)
{
anim.Play(animName);
anim[animName].normalizedTime = 1;
DOTween.To(() => anim[animName].normalizedTime, v => anim[animName].normalizedTime = v, 0, anim[animName].length / Math.Abs(curSpeed)).onComplete = () =>
{
anim.Stop();
};
}
else
2025-01-09 15:52:57 +08:00
{
2025-01-14 10:16:29 +08:00
anim[animName].speed = curSpeed;
anim.Play(animName);
if (anim[animName].wrapMode == WrapMode.Loop)
{
this.Finish();
}
2025-01-09 15:52:57 +08:00
}
2025-01-14 10:16:29 +08:00
2024-12-31 13:16:06 +08:00
}
}
catch (Exception)
{
Debug.LogError($"{path} <20><><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD> {animName} <20><><EFBFBD><EFBFBD>");
2024-12-18 11:17:08 +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();
}
}
public void OnExecute(float dt)
{
if (anim != null && anim.isPlaying == false)
{
this.Finish();
}
}
public void OnFinish()
{
}
public void Reset()
{
Status = ActionStatus.NotStart;
Paused = false;
}
public bool Paused { get; set; }
public void Deinit()
{
if (!Deinited)
{
OnFinished = null;
Deinited = true;
mPool.Recycle(this);
}
}
public bool Deinited { get; set; }
}
}