CapersProject/Assets/02.Scripts/BlueWater/Character/AnimationController.cs

132 lines
3.4 KiB
C#
Raw Normal View History

2024-06-03 18:26:03 +00:00
using System;
using System.Collections;
using Sirenix.OdinInspector;
2024-06-03 18:26:03 +00:00
using UnityEngine;
namespace BlueWater
{
public class AnimationController : MonoBehaviour
{
// Variables
#region Variables
// Components
[SerializeField]
2024-06-03 18:26:03 +00:00
private Animator _animator;
#endregion
// Unity events
#region Unity events
private void Awake()
{
InitializeComponents();
}
2024-06-03 18:26:03 +00:00
#endregion
// Initialize methods
#region Initialize Methdos
[Button("컴포넌트 초기화")]
private void InitializeComponents()
2024-06-03 18:26:03 +00:00
{
_animator = GetComponentInChildren<Animator>();
2024-06-03 18:26:03 +00:00
}
#endregion
// Methods
#region Methods
2024-10-28 04:24:04 +00:00
public void PlayAnimation(string stateName, int layer = 0)
{
_animator.Play(stateName, 0);
}
2024-06-03 18:26:03 +00:00
public void SetAnimationParameter(string parameter, bool value)
{
if (!_animator) return;
2024-06-03 18:26:03 +00:00
_animator.SetBool(parameter, value);
}
public void SetAnimationParameter(string parameter, int value)
{
if (!_animator) return;
2024-06-03 18:26:03 +00:00
_animator.SetInteger(parameter, value);
}
public void SetAnimationParameter(string parameter, float value)
{
if (!_animator) return;
2024-06-03 18:26:03 +00:00
_animator.SetFloat(parameter, value);
}
public void SetAnimationTrigger(string parameter)
{
if (!_animator) return;
2024-06-03 18:26:03 +00:00
_animator.SetTrigger(parameter);
}
public bool IsComparingCurrentAnimation(string animationName, int animatorLayer = 0)
{
if (!_animator) return false;
2024-06-03 18:26:03 +00:00
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)
{
if (!_animator) return;
2024-06-03 18:26:03 +00:00
var animationLength = _animator.GetCurrentAnimatorStateInfo(animatorLayer).length;
_animator.speed = animationLength / targetDuration;
}
public float GetCurrentAnimationNormalizedTime(int animatorLayer = 0)
{
if (!_animator) return 0f;
2024-06-03 18:26:03 +00:00
return _animator.GetCurrentAnimatorStateInfo(animatorLayer).normalizedTime;
}
public float GetCurrentAnimationLength(int animatorLayer = 0)
{
if (!_animator) return 0f;
return _animator.GetCurrentAnimatorStateInfo(animatorLayer).length;
}
2024-06-03 18:26:03 +00:00
public void ResetAnimationSpeed()
{
if (!_animator) return;
2024-10-28 04:24:04 +00:00
2024-06-03 18:26:03 +00:00
_animator.speed = 1f;
}
2024-10-28 04:24:04 +00:00
2024-06-03 18:26:03 +00:00
#endregion
}
}