본문 바로가기

정리 카테고리/API

Unity_ animation.IsPlay()

Animation.IsPlaying



string name에는 확인할 애니메이션의 이름을 넣고 메소드를 실행시키면 bool형태로 반환한다.

이를 통해 애니메이션이 동작하는동안 에만 확인하여 작동 시키고자 한다면 이 메소드를 활용하면 된다.




public bool IsPlaying(string name);

Description

Is the animation named name playing?



아래 예제는 때리는 애니메이션이 동작중일때 "때리는중" 이라는 메시지가 Debug.Log를 통해 출력되는 영상이다.

(처음에는 천천히 보여주기위해 손으로 넘기며 두번째에는 그냥 재생 시킨 모습이다.)









1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
    GameObject actionCH;
    Animation ani;
 
    void Start () {
        //케릭터의 정보를 리소스에서 로드
        var characterLoad = Resources.Load("ch_01_01");
        //게임 오브젝트에 Instantiate ->게임오브젝트 생성
        actionCH = Instantiate(characterLoad) as GameObject;
        //애니메이션을 동작시키기 위해 GetComponent
        ani = actionCH.GetComponent<Animation>();
        //코루틴 동작
        StartCoroutine(this.checkAni());
        
    }
    //동작할 코루틴
    IEnumerator checkAni()
    {
        //대기(영상을 찍기위해 잠시의 딜레이
        yield return new WaitForSeconds(1);
        //애니메이션 플레이
        ani.Play("attack_sword_01");
        //애니메이션 동작여부 확인후 while문 동작
        while (ani.IsPlaying("attack_sword_01"))
        {
            //메시지 출력
                Debug.Log("때리는중");
            //한프레임 쉼
            yield return null;
        }        
        
 
    }
}
cs

P.S. yield return null;을 써야 하는 이유에 대해 확인하여 포스팅 하기!!