Last active
June 8, 2017 18:57
-
-
Save muzudho/6e98ca253b051da298afbb1a2fabef3b to your computer and use it in GitHub Desktop.
Unityのアニメーター・コントローラーを使ってモーションの状態遷移をプログラムから独立させようぜ ref: http://qiita.com/muzudho1/items/85cd11ba0c7b1b7d496d
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 UnityEditor; | |
using UnityEngine; |
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; // 追加 | |
public class Banana1 : MonoBehaviour { | |
[MenuItem("Bye bye Earth!/Rocket1/Space2/Moon3")] | |
static void GoToTheMoon() | |
{ | |
Debug.Log("Moon is great!"); | |
} | |
} |
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
Project | |
| | |
+-- ゲームオブジェクト | |
| | |
+-- スプライト・レンダラー | |
| | | |
| +-- スプライト | |
| ~~~~~~~~~ | |
| | |
+-- アニメーター | |
Assets/Resources/Sprites | |
| | |
+-- スプライト | |
~~~~~~~~~ | |
| | |
+-- タイル状にスライスした画像の1枚1枚 | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
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
Project | |
| | |
+-- ゲームオブジェクト | |
| | |
+-- スプライト・レンダラー | |
| | |
+-- アニメーター | |
Assets/Resources/AnimationClips ※別にこのフォルダーでなくてもいい | |
| | |
+-- アニメーション | |
~~~~~~~~~~~~~ | |
Assets/Resources/Sprites | |
| | |
+-- スプライト | |
| | |
+-- タイル状にスライスした画像の1枚1枚 |
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
Project | |
| | |
+-- ゲームオブジェクト | |
| | |
+-- スプライト・レンダラー | |
| | | |
| +-- タイル状にスライスした画像の1枚1枚 | |
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| | |
+-- アニメーター | |
Assets/Resources/AnimationClipsい | |
| | |
+-- アニメーション | |
| | |
+-- タイル状にスライスした画像の1枚1枚 | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
Assets/Resources/Sprites | |
| | |
+-- スプライト | |
| | |
+-- タイル状にスライスした画像の1枚1枚 | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
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
// (1)サンプルのコントローラー(ファイル)を見に行く☆ | |
var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/Mecanim/StateMachineTransitions.controller"); | |
// (2)アニメーター・ビューの[parameters] に C#スクリプトから 4つ追加する。 | |
controller.AddParameter("TransitionNow", AnimatorControllerParameterType.Trigger); | |
controller.AddParameter("Reset", AnimatorControllerParameterType.Trigger); | |
controller.AddParameter("GotoB1", AnimatorControllerParameterType.Trigger); | |
controller.AddParameter("GotoC", AnimatorControllerParameterType.Trigger); | |
// (3)ステートマシン(六角形のやつ)を3つ追加する。 | |
var rootStateMachine = controller.layers[0].stateMachine; | |
var smA = rootStateMachine.AddStateMachine("smA"); // stateMachineA | |
var smB = rootStateMachine.AddStateMachine("smB"); // stateMachineB | |
var smC = smB.AddStateMachine("smC"); // stateMachineC |
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
// (4)ステート(長方形のやつ)を5つ追加する。 | |
var stateA1 = smA.AddState("stateA1"); // (5)(6) | |
var stateB1 = smB.AddState("stateB1"); // (7) | |
var stateB2 = smB.AddState("stateB2"); // (7) | |
smC.AddState("stateC1"); // (8) | |
var stateC2 = smC.AddState("stateC2"); // (8) don’t add an entry transition, should entry to state by default |
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
// 以下、トランジション(白い矢印の線)を追加する。 | |
// (5)stateA1 から Exit へのトランジション | |
var exitTransition = stateA1.AddExitTransition(); // stateA1 は Exit につなげる。 | |
exitTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow"); // 条件にトリガーを追加する | |
exitTransition.duration = 0; // duration は 0 に。 | |
// (6)(これは分からない) stateA1 と Any State がつながっていないようだが? | |
var resetTransition = smA.AddAnyStateTransition(stateA1); | |
resetTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "Reset"); | |
resetTransition.duration = 0; |
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
// (7)stateB1 は Entry につなげる。 | |
var transitionB1 = smB.AddEntryTransition(stateB1); | |
transitionB1.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "GotoB1"); // 条件にトリガーを追加する。 | |
// (8)stateB2 も Entry につなげる。 | |
smB.AddEntryTransition(stateB2); | |
// (9)stateC2 は Entry につなげる方法ではなく、デフォルトとして設定する。 | |
smC.defaultState = stateC2; | |
// (10)stateC2 は Exit につなげる。 | |
var exitTransitionC2 = stateC2.AddExitTransition(); | |
exitTransitionC2.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow"); // 条件にトリガーを追加する。 | |
exitTransitionC2.duration = 0; |
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
Project | |
| | |
+-- ゲームオブジェクト | |
~~~~~~~~~~~~~~~~ | |
| | |
+-- スプライト・レンダラー | |
| | |
+-- アニメーター |
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
Project | |
| | |
+-- ゲームオブジェクト | |
| | |
+-- スプライト・レンダラー | |
| | |
+-- アニメーター | |
Assets/Resources/Sprites | |
| | |
+-- スプライト | |
~~~~~~~~~ |
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
Project | |
| | |
+-- ゲームオブジェクト | |
| | |
+-- スプライト・レンダラー | |
| | |
+-- アニメーター | |
Assets/Resources/Sprites ※別にこのフォルダーでなくてもいい | |
| | |
+-- スプライト | |
| | |
+-- タイル状にスライスした画像の1枚1枚 | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
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
Project | |
| | |
+-- ゲームオブジェクト | |
| | |
+-- スプライト・レンダラー | |
| ~~~~~~~~~~~~~~~~~~~~ | |
| | |
+-- アニメーター | |
Assets/Resources/Sprites ※別にこのフォルダーでなくてもいい | |
| | |
+-- スプライト | |
| | |
+-- タイル状にスライスした画像の1枚1枚 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment