VirtualFramework/Assets/Scripts/Actions/AnimationAction.cs

94 lines
2.2 KiB
C#
Raw Normal View History

2024-12-14 18:27:59 +08:00
using System;
using UnityEngine;
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;
2024-12-18 11:17:08 +08:00
bool reset = false;
2024-12-14 18:27:59 +08:00
Animation anim;
2024-12-18 11:17:08 +08:00
public static AnimationAction Allocate(string path, string animName, string reset, 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;
retNode.animName = animName;
2024-12-18 11:17:08 +08:00
bool.TryParse(reset, out retNode.reset);
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 = Utility.FindObj(path);
if (obj != null)
{
anim = obj.GetComponent<Animation>();
anim.Play(animName);
2024-12-18 11:17:08 +08:00
if (reset)
{
2024-12-27 17:25:30 +08:00
ActionKit.DelayFrame(1, () => anim.Stop()).StartGlobal();
2024-12-18 11:17:08 +08:00
}
2024-12-14 18:27:59 +08:00
}
else
{
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; }
}
}