VirtualFramework/Assets/Scripts/Actions/AnimationAction.cs

237 lines
6.4 KiB
C#
Raw Normal View History

2025-01-16 18:48:37 +08:00
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using DG.Tweening;
2024-12-30 10:29:48 +08:00
using System.Collections.Generic;
using System;
2024-12-14 18:27:59 +08:00
using UnityEngine;
using QFramework;
2024-12-14 18:27:59 +08:00
namespace QFramework
{
internal class AnimationAction : IAction
{
public System.Action OnFinished
{
get; set;
}
2024-12-14 18:27:59 +08:00
private AnimationAction()
{
}
2024-12-14 18:27:59 +08:00
private static readonly SimpleObjectPool<AnimationAction> mPool =
new SimpleObjectPool<AnimationAction>(() => new AnimationAction(), null, 10);
2024-12-14 18:27:59 +08:00
string path;
string animName;
Animation anim;
string frame;
string speed;
string deviceName;
2024-12-14 18:27:59 +08:00
float totalTime;
GameObject obj;
2024-12-14 18:27:59 +08:00
public static AnimationAction Allocate(string path, Dictionary<string, string> datas, System.Action OnFinished = null)
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.path = path;
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"] : "";
retNode.OnFinished = OnFinished;
return retNode;
}
2024-12-14 18:27:59 +08:00
public ulong ActionID
{
get; set;
}
public ActionStatus Status
{
get; set;
}
TweenerCore<float, float, FloatOptions> animDot;
public void OnStart()
{
if (string.IsNullOrEmpty(deviceName))
{
obj = Utility.FindObj(path);
}
else
2024-12-14 18:27:59 +08:00
{
obj = DeviceController.Instance.GetDeviceObj(deviceName);
2024-12-14 18:27:59 +08:00
}
if (obj != null)
2024-12-14 18:27:59 +08:00
{
if (obj.activeSelf == false)
{
Debug.LogError(obj.name + "<22><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬");
this.Finish();
}
else
{
try
2024-12-18 11:17:08 +08:00
{
anim = obj.GetComponent<Animation>();
2025-01-14 10:16:29 +08:00
if (string.IsNullOrEmpty(frame) == false && frame != "-1")
{
int curFrame = 0;
int.TryParse(frame, out curFrame);
anim.clip = anim[animName].clip;
anim[animName].time = curFrame / anim.clip.frameRate;
anim.Play(animName);
anim[animName].speed = 0;
anim.Sample();
this.Finish();
}
else
{
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; // <20>Ӷ<EFBFBD><D3B6><EFBFBD>ĩβ<C4A9><CEB2>ʼ
// <20><><EFBFBD><EFBFBD><E3B5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
float duration = anim[animName].length / Math.Abs(curSpeed);
animDot = DOTween.To(
() => anim[animName].normalizedTime,
v => anim[animName].normalizedTime = v,
0, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ
duration
);
2025-01-22 10:59:25 +08:00
animDot.onComplete = () =>
{
anim.Stop();
this.Finish(); // <20>ؼ<EFBFBD><D8BC>޸ģ<DEB8><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>
2025-01-22 10:59:25 +08:00
};
totalTime = duration; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
2025-01-22 10:59:25 +08:00
}
else
{
anim[animName].speed = curSpeed;
anim.Play(animName);
totalTime = Math.Abs(anim[animName].length / curSpeed);
// <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (anim[animName].wrapMode == WrapMode.Loop)
2025-01-22 10:59:25 +08:00
{
// ʾ<><CABE><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10<31><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
totalTime = 10f;
2025-01-22 10:59:25 +08:00
}
2025-01-09 15:52:57 +08:00
}
2025-01-22 10:59:25 +08:00
}
2024-12-31 13:16:06 +08:00
}
catch (Exception)
{
anim = obj.GetComponent<Animation>();
Debug.LogError($"{path} <20><><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD> {animName} <20><><EFBFBD><EFBFBD>");
this.Finish();
}
}
}
else
{
Debug.LogError(<>ҵ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>" + path);
this.Finish();
}
}
2025-01-22 10:59:25 +08:00
2024-12-31 13:16:06 +08:00
public void OnExecute(float dt)
{
//if (Status != ActionStatus.NotStart) return;
totalTime -= dt;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD>
if (anim != null && animDot != null && !animDot.IsPlaying())
2024-12-14 18:27:59 +08:00
{
this.Finish();
return;
2024-12-14 18:27:59 +08:00
}
if (anim != null)
2024-12-14 18:27:59 +08:00
{
// <20><>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
if (anim[animName].wrapMode != WrapMode.Loop)
2024-12-14 18:27:59 +08:00
{
if (!anim.isPlaying || totalTime <= 0)
{
this.Finish();
}
}
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>
else
{
if (totalTime <= 0)
{
anim.Stop(animName);
this.Finish();
}
2024-12-14 18:27:59 +08:00
}
}
else
2024-12-14 18:27:59 +08:00
{
this.Finish();
2024-12-14 18:27:59 +08:00
}
}
2024-12-14 18:27:59 +08:00
public void OnFinish()
{
// Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AnimActionfinish");
OnFinished?.Invoke(); // <20><><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD>ص<EFBFBD>
}
2024-12-14 18:27:59 +08:00
public void Reset()
{
Status = ActionStatus.NotStart;
Paused = false;
}
2024-12-14 18:27:59 +08:00
public bool Paused
{
get; set;
}
public void Deinit()
{
if (!Deinited)
2024-12-14 18:27:59 +08:00
{
if (obj != null && anim != null)
2024-12-14 18:27:59 +08:00
{
anim.Stop();
2024-12-14 18:27:59 +08:00
}
animDot?.Kill();
OnFinished = null;
Deinited = true;
obj = null;
mPool.Recycle(this);
2024-12-14 18:27:59 +08:00
}
}
public bool Deinited
{
get; set;
}
}
2024-12-14 18:27:59 +08:00
}