[유니티] 코드에서 애니메이션 바꾸기
프리팹은 하나이고 애니메이션만 계속 코드에서 동적으로 바꾸고 싶을 때
아래와 같이 해주면 됩니다.
1. 먼저 해당 프리팹에 animator 컴포넌트를 추가해줍니다.
(기존에 프리팹을 Player라고 만들었고 스크립트로 MyPlayer를 붙여준 상태입니다.)
2. 조건이 있는데 Project창에 Resources폴더를 만드시고(필수) 그 아래 바로 애니메이션을 둬도 되고
저 같은 경우에는 Ani폴더를 하나 더 만들어서 애니메이션을 뒀습니다.
3. 해당 플레이어를 생성하는 부분에서 다음과 같은 코드를 추가해줍니다.
// MyPlayer에 Animator변수를 선언해줍니다.(저는 public으로 했는데 왠만하면 private으로 해주는 편이 좋을거 같습니다.) class MyPlayer { public Animator ani; } // 플레이어를 생성하는 매니저 클래스 부분. playerPrefab은 유니티에서 드래그로 값을 넣어주던지 합니다. public GameObject playerPrefab; // 해당 플레이어 프리팹을 생성합니다. GameObject go = Instantiate(playerPrefab); // 생성한 gameobject에 myplayer컴포넌트(스크립트)를 가져옵니다. MyPlayer player = go.GetComponent<MyPlayer>(); // player에 ani변수에 원하는 애니메이션 파일을 load해줍니다. player.ani.runtimeAnimatorController = (RuntimeAnimatorController)RuntimeAnimatorController.Instantiate( Resources.Load("Ani\\Player_00_Model", typeof(RuntimeAnimatorController))); |
'Programming > Unity' 카테고리의 다른 글
[유니티] Can't add script (0) | 2018.04.06 |
---|---|
[유니티] But the dll is not allowed to be included or could not be found. (0) | 2018.04.05 |
[유니티] 유니티에 UGUI와 메인카메라의 충돌을 같이 하기 (0) | 2018.03.15 |
[유니티] NGUI UIlabel 줄바꿈 적용시키기 (0) | 2018.02.22 |
[유니티 에러]ArgumentException: The Assembly System.Configuration is referenced by System.Data (0) | 2017.07.15 |