ProjectDDD/Packages/SLUnity/SLUITrigger.cs
2025-07-08 19:46:31 +09:00

102 lines
2.9 KiB
C#

using UnityEngine;
namespace Superlazy.UI
{
public class SLUITrigger : SLUIComponent
{
public Animator animator;
public string bindingValue;
public string bindingAnimation;
public string defaultAnimation;
public bool forceTrigger;
private SLEntity oldValue;
private bool restart;
protected override void Validate()
{
animator = GetComponent<Animator>();
}
protected override void Init()
{
}
public void Update()
{
if (forceTrigger) return;
if (animator.GetCurrentAnimatorStateInfo(0).loop == false && animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1)
{
if (restart)
{
var newValue = SLGame.SessionGet(bindParent.BindPath).Get(bindingValue);
if (newValue)
{
animator.Play(bindingAnimation, 0, 0);
}
else
{
if (string.IsNullOrEmpty(defaultAnimation) == false)
{
animator.Play(defaultAnimation, 0, 0);
}
}
restart = false;
}
}
}
protected override void Enable()
{
SLGame.AddNotify(bindParent.BindPath.CombinePath(bindingValue), OnChange);
oldValue = false;
}
protected override void Disable()
{
SLGame.RemoveNotify(bindParent.BindPath.CombinePath(bindingValue), OnChange);
}
private void OnChange()
{
if (bindParent.Active == false) return;
var newValue = SLGame.SessionGet(bindParent.BindPath).Get(bindingValue);
if (newValue != oldValue)
{
if (newValue)
{
if (forceTrigger || animator.GetCurrentAnimatorStateInfo(0).loop || animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1)
{
animator.Play(bindingAnimation, 0, 0);
}
else
{
restart = true;
}
}
else
{
if (forceTrigger || animator.GetCurrentAnimatorStateInfo(0).loop || animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1)
{
if (string.IsNullOrEmpty(defaultAnimation) == false)
{
animator.Play(defaultAnimation, 0, 0);
}
}
else
{
restart = true;
}
}
oldValue = newValue;
}
}
}
}