80 lines
1.9 KiB
C#
80 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class RandomAnimatorHandler : MonoBehaviour
|
|
{
|
|
[SerializeField] private Animator targetAnimator;
|
|
public string handelrName;
|
|
[SerializeField] private Vector2 randomDurationRange;
|
|
[SerializeField] private int randomCount;
|
|
[SerializeField] private bool playOnAwake;
|
|
|
|
private bool _isPlaying;
|
|
private float _curduration;
|
|
|
|
private void Awake()
|
|
{
|
|
if (playOnAwake)
|
|
{
|
|
StartRandomPlay();
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
AnimatorCenter.Instance.RegisterRandomHandler(this);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
AnimatorCenter.Instance.UnregisterRandomHandler(this);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_isPlaying)
|
|
{
|
|
_curduration -= Time.deltaTime;
|
|
if (_curduration <= 0)
|
|
{
|
|
RandomPlay();
|
|
_curduration = RandomDuration();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RandomPlay()
|
|
{
|
|
targetAnimator.ResetTrigger("RandomPlay");
|
|
|
|
// 设置参数
|
|
var index = RandomIndex();
|
|
targetAnimator.SetInteger("RandomIndex", index);
|
|
// 播放
|
|
targetAnimator.SetTrigger("RandomPlay");
|
|
|
|
}
|
|
|
|
public void StartRandomPlay()
|
|
{
|
|
_isPlaying = true;
|
|
_curduration = RandomDuration();
|
|
}
|
|
|
|
public void StopRandomPlay()
|
|
{
|
|
_isPlaying = false;
|
|
}
|
|
|
|
private float RandomDuration()
|
|
{
|
|
return Random.Range(randomDurationRange.x, randomDurationRange.y);
|
|
}
|
|
|
|
private int RandomIndex()
|
|
{
|
|
return Random.Range(0, randomCount);
|
|
}
|
|
}
|
|
} |