VirtualFramework/Assets/Scripts/Actions/AnimationAction.cs

179 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using static OperationController;
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;
string frame;
string speed;
string deviceName;
float totalTime;
GameObject obj;
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;
}
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
{
obj = DeviceController.Instance.GetDeviceObj(deviceName);
}
if (obj != null)
{
if (obj.activeSelf == false)
{
Debug.LogError(obj.name + "<22><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬");
this.Finish();
}
else
{
try
{
anim = obj.GetComponent<Animation>();
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[animName].speed = 0;
anim.Play(animName);
this.Finish();
}
else
{
float curSpeed = 1;
if (string.IsNullOrEmpty(speed) == false)
{
float.TryParse(speed, out curSpeed);
}
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);
}
}
catch (Exception)
{
anim = obj.GetComponent<Animation>();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>Ǿɰ浼<C9B0>µ<EFBFBD>
//animationֻ<6E><D6BB><EFBFBD>þɰ浼<C9B0><E6B5BC>
Debug.LogError($"{path} <20><><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD> {animName} <20><><EFBFBD><EFBFBD>");
}
}
}
else
{
Debug.LogError(<>ҵ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>" + path);
this.Finish();
}
}
public void OnExecute(float dt)
{
totalTime -= Time.deltaTime;
if (anim != null && (anim.isPlaying == false || totalTime <= 0))
{
this.Finish();
}
}
public void OnFinish()
{
}
public void Reset()
{
Status = ActionStatus.NotStart;
Paused = false;
}
public bool Paused { get; set; }
public void Deinit()
{
if (!Deinited)
{
if (obj != null)
{
if (anim!=null)
{
anim.Stop();
}
}
animDot?.Kill();
OnFinished = null;
Deinited = true;
obj = null;
mPool.Recycle(this);
}
}
public bool Deinited { get; set; }
}
}