ProjectDDD/Packages/SLUnity/SLUI/UIOnOff/SLUIAnimator.cs

95 lines
2.0 KiB
C#
Raw Normal View History

using UnityEngine;
namespace Superlazy.UI
{
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(SLUIEntity))]
public class SLUIAnimator : SLUIOnOffTransition
{
private bool animPlayed = false;
public string onAnim;
public string offAnim;
private Animator anim;
private int onHash;
private int offHash;
protected override void Validate()
{
anim = GetComponent<Animator>();
}
protected override void Init()
{
if (onAnim != string.Empty)
{
onHash = Animator.StringToHash(onAnim);
}
else
{
onHash = -1;
}
if (offAnim != string.Empty)
{
offHash = Animator.StringToHash(offAnim);
}
else
{
offHash = -1;
}
anim.Update(0.001f);
}
protected override bool OnUpdate(bool forceUpdate = false)
{
if (animPlayed)
{
if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1.0f)
{
return false;
}
else
{
animPlayed = false;
return true;
}
}
int animHash;
if (currentState)
{
animHash = onHash;
}
else
{
animHash = offHash;
}
if (animHash != -1)
{
anim.Play(animHash, 0, 0);
if (forceUpdate)
{
anim.Update(0.001f);
}
animPlayed = true;
return false;
}
else
{
return true;
}
}
protected override void ForceDisable()
{
animPlayed = false;
}
}
}