Last active
December 23, 2020 08:22
-
-
Save tsubaki/019c471bd746fad017cc to your computer and use it in GitHub Desktop.
アニメーションの終了を待つ奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Sample : MonoBehaviour | |
{ | |
private static readonly int hashAttack = Animator.StringToHash("Attack"); | |
private static readonly int hashIdle = Animator.StringToHash("Idle"); | |
[SerializeField] | |
UnityEngine.UI.Text text; | |
IEnumerator AttackAnimationFlow() | |
{ | |
text.text = "攻撃中"; | |
var animator = GetComponent<Animator> (); | |
animator.Play (hashAttack); | |
yield return null; // ステートの反映に1フレームいる。解せぬ | |
yield return new WaitForAnimation (animator, 0); | |
text.text = "待機中"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WaitForAnimation : CustomYieldInstruction | |
{ | |
Animator m_animator; | |
int m_lastStateHash = 0; | |
int m_layerNo = 0; | |
public WaitAnimation(Animator animator, int layerNo) | |
{ | |
Init(animator, layerNo, animator.GetCurrentAnimatorStateInfo(layerNo).nameHash); | |
} | |
void Init(Animator animator, int layerNo, int hash) | |
{ | |
m_layerNo = layerNo; | |
m_animator = animator; | |
m_lastStateHash = hash; | |
} | |
public override bool keepWaiting { | |
get { | |
var currentAnimatorState = m_animator.GetCurrentAnimatorStateInfo (m_layerNo); | |
return currentAnimatorState.fullPathHash == m_lastStateHash && | |
(currentAnimatorState.normalizedTime < 1 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
みすってますよ