Advertisement
LittleAngel

TestWindow by M.S.

Feb 19th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4.  
  5. public class TestWindow : EditorWindow {
  6.  
  7. public GameObject target;
  8. public AnimationClip idleAnim;
  9. public AnimationClip walkAnim;
  10. public AnimationClip runAnim;
  11.  
  12. // Use this for initialization
  13. void Start () {
  14.  
  15. }
  16.  
  17. [MenuItem ("Window/Animation Helper")]
  18. static void OpenWindow () {
  19. // Get existing open window or if none, make a new one:
  20. EditorWindow.GetWindow(typeof(TestWindow));
  21.  
  22. }
  23.  
  24. void OnGUI()
  25. {
  26. target = EditorGUILayout.ObjectField("Target Object", target, typeof(GameObject), true) as GameObject;
  27. idleAnim = EditorGUILayout.ObjectField("Idle", idleAnim, typeof(AnimationClip), true) as AnimationClip;
  28. walkAnim = EditorGUILayout.ObjectField("Walk", walkAnim, typeof(AnimationClip), true) as AnimationClip;
  29. runAnim = EditorGUILayout.ObjectField("Run", runAnim, typeof(AnimationClip), true) as AnimationClip;
  30.  
  31. if (GUILayout.Button("Create"))
  32. {
  33. UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/" + target.name + ".controller");
  34.  
  35. // Adds 2 parameters
  36. controller.AddParameter("Speed", UnityEngine.AnimatorControllerParameterType.Float);
  37.  
  38. //Add states
  39. UnityEditor.Animations.AnimatorState idleState = controller.layers[0].stateMachine.AddState("Idle");
  40. idleState.motion = idleAnim;
  41.  
  42. // Blend Tree creation
  43. UnityEditor.Animations.BlendTree blendTree;
  44. UnityEditor.Animations.AnimatorState moveState = controller.CreateBlendTreeInController("Move", out blendTree);
  45.  
  46. // BlendTree setup
  47. blendTree.blendType = UnityEditor.Animations.BlendTreeType.Simple1D;
  48. blendTree.blendParameter = "Speed";
  49. blendTree.AddChild(walkAnim);
  50. blendTree.AddChild(runAnim);
  51.  
  52. UnityEditor.Animations.AnimatorStateTransition leaveIdle = idleState.AddTransition(moveState);
  53. UnityEditor.Animations.AnimatorStateTransition leaveMove = moveState.AddTransition(idleState);
  54.  
  55. leaveIdle.AddCondition(UnityEditor.Animations.AnimatorConditionMode.Less, 0.01f, "Speed");
  56. leaveMove.AddCondition(UnityEditor.Animations.AnimatorConditionMode.Greater, 0.01f, "Speed");
  57.  
  58. target.GetComponent<Animator>().runtimeAnimatorController = controller;
  59. }
  60.  
  61. }
  62.  
  63. // Update is called once per frame
  64. void Update () {
  65.  
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement