89 lines
2.4 KiB
C#
89 lines
2.4 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace BlueWater
|
|||
|
{
|
|||
|
public class AnimationController : MonoBehaviour
|
|||
|
{
|
|||
|
// Variables
|
|||
|
#region Variables
|
|||
|
|
|||
|
// Components
|
|||
|
private Animator _animator;
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
// Initialize methods
|
|||
|
#region Initialize Methdos
|
|||
|
|
|||
|
public void InitializeComponents(Animator animator)
|
|||
|
{
|
|||
|
_animator = animator;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
// Methods
|
|||
|
#region Methods
|
|||
|
|
|||
|
public void SetAnimationParameter(string parameter, bool value)
|
|||
|
{
|
|||
|
_animator.SetBool(parameter, value);
|
|||
|
}
|
|||
|
|
|||
|
public void SetAnimationParameter(string parameter, int value)
|
|||
|
{
|
|||
|
_animator.SetInteger(parameter, value);
|
|||
|
}
|
|||
|
|
|||
|
public void SetAnimationParameter(string parameter, float value)
|
|||
|
{
|
|||
|
_animator.SetFloat(parameter, value);
|
|||
|
}
|
|||
|
|
|||
|
public void SetAnimationTrigger(string parameter)
|
|||
|
{
|
|||
|
_animator.SetTrigger(parameter);
|
|||
|
}
|
|||
|
|
|||
|
public bool IsComparingCurrentAnimation(string animationName, int animatorLayer = 0)
|
|||
|
{
|
|||
|
return _animator.GetCurrentAnimatorStateInfo(animatorLayer).IsName(animationName);
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerator WaitForAnimationToRun(string animationName, Action<bool> onSuccess, float timeout = 2f)
|
|||
|
{
|
|||
|
var elapsedTime = 0f;
|
|||
|
while (!IsComparingCurrentAnimation(animationName))
|
|||
|
{
|
|||
|
elapsedTime += Time.deltaTime;
|
|||
|
yield return null;
|
|||
|
if (elapsedTime <= timeout) continue;
|
|||
|
|
|||
|
Debug.Log("Timeout waiting for animation state: " + animationName);
|
|||
|
onSuccess?.Invoke(false);
|
|||
|
}
|
|||
|
|
|||
|
onSuccess?.Invoke(true);
|
|||
|
}
|
|||
|
|
|||
|
public void SetCurrentAnimationSpeed(float targetDuration, int animatorLayer = 0)
|
|||
|
{
|
|||
|
var animationLength = _animator.GetCurrentAnimatorStateInfo(animatorLayer).length;
|
|||
|
_animator.speed = animationLength / targetDuration;
|
|||
|
}
|
|||
|
|
|||
|
public float GetCurrentAnimationNormalizedTime(int animatorLayer = 0)
|
|||
|
{
|
|||
|
return _animator.GetCurrentAnimatorStateInfo(animatorLayer).normalizedTime;
|
|||
|
}
|
|||
|
|
|||
|
public void ResetAnimationSpeed()
|
|||
|
{
|
|||
|
_animator.speed = 1f;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|