Created
March 29, 2016 02:49
-
-
Save kimsama/7302b693819946344024 to your computer and use it in GitHub Desktop.
Dynamically creating AnimatorController on http://kikki.hatenablog.com/entry/2015/07/01/210530
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
#if UNITY_EDITOR | |
AnimatorController mecanimController = null ; | |
// Animation Controller 파일을 생성 | |
mecanimController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath ( "Assets / Resources / Mecanims / HeroMecanim.controller" ); | |
// Animation Controller에 애니메이션을 전환시키기위한 조건이되는 매개 변수를 추가 | |
mecanimController.AddParameter ( "RunTrigger" , AnimatorControllerParameterType.Trigger); | |
mecanimController.AddParameter ( "JumpTrigger" , AnimatorControllerParameterType.Trigger); | |
mecanimController.AddParameter ( "SlidingTrigger" , AnimatorControllerParameterType.Trigger); | |
mecanimController.AddParameter ( "DownTrigger" , AnimatorControllerParameterType.Trigger); | |
// root의 State Machine을 취득 | |
var rootStateMachine = mecanimController.layers [ 0 ] .stateMachine; | |
// "Entry State '과'Any State '의 위치 좌표를 변경 | |
rootStateMachine.entryPosition = new Vector3 ( 0f , 0f , 0f ); | |
rootStateMachine.anyStatePosition = new Vector3 ( 0f , 50f , 0f ); | |
// 각 애니메이션 State Machine을 만들어서 배치 | |
var runState = rootStateMachine.AddState ( "runState" , new Vector3 ( 200f , 0f , 0f )); | |
mecanimController.SetStateEffectiveMotion (runState (Motion) Resources.Load <AnimationClip> ( "AnimationClips / hero_run" )); | |
var jumpState = rootStateMachine.AddState ( "jumpState" , new Vector3 ( 200f , 100f , 0f )); | |
mecanimController.SetStateEffectiveMotion (jumpState (Motion) Resources.Load <AnimationClip> ( "AnimationClips / hero_jump" )); | |
var slidingState = rootStateMachine.AddState ( "slidingState" , new Vector3 ( 200f , 200f , 0f )); | |
mecanimController.SetStateEffectiveMotion (slidingState (Motion) Resources.Load <AnimationClip> ( "AnimationClips / hero_sliding" )); | |
var downState = rootStateMachine.AddState ( "downState" , new Vector3 ( 200f , 300f , 0 )); | |
mecanimController.SetStateEffectiveMotion (downState (Motion) Resources.Load <AnimationClip> ( "AnimationClips / hero_down" )); | |
// 애니메이션의 전환 조건을 설정 | |
runState.AddTransition (jumpState) .AddCondition (AnimatorConditionMode.If, 0 , "JumpTrigger" ); | |
runState.AddTransition (slidingState) .AddCondition (AnimatorConditionMode.If, 0 , "SlidingTrigger" ); | |
runState.AddTransition (downState) .AddCondition (AnimatorConditionMode.If, 0 , "DownTrigger" ); | |
jumpState.AddTransition (runState) .AddCondition (AnimatorConditionMode.If, 0 , "RunTrigger" ); | |
jumpState.AddTransition (downState) .AddCondition (AnimatorConditionMode.If, 0 , "DownTrigger" ); | |
slidingState.AddTransition (runState) .AddCondition (AnimatorConditionMode.If, 0 , "RunTrigger" ); | |
slidingState.AddTransition (downState) .AddCondition (AnimatorConditionMode.If, 0 , "DownTrigger" ); | |
downState.AddTransition (runState) .AddCondition (AnimatorConditionMode.If, 0 , "RunTrigger" ); | |
#else | |
// Unity의 Editor 환경이 아닌 경우, Resource 폴더에서 Animation Controller를 취득 | |
RuntimeAnimatorController mecanimController = Resources.Load <RuntimeAnimatorController> ( "Mecanims / HeroMecanim " ); | |
#endif | |
// Animator이 설정되어있는 객체를 가져 | |
this .heroAnimator = gameObject.GetComponent <Animator> (); | |
#if UNITY_EDITOR | |
// 위에서 만든 Animation Controller를 Animator의 runtimeAnimatorController로 설정 | |
this .heroAnimator.runtimeAnimatorController = new AnimatorOverrideController () {runtimeAnimatorController = (RuntimeAnimatorController) mecanimController}; | |
#else | |
// 위에서 얻은 Animation Controller를 Animator의 runtimeAnimatorController 설정 | |
this .heroAnimator.runtimeAnimatorController = new AnimatorOverrideController () {runtimeAnimatorController = mecanimController}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment