当前位置:   article > 正文

Spine工具(根据Spine生成的Animator自动生成状态机)_spine转animator

spine转animator

由于反复操作,状态机连线太麻烦,操作重复,所以写了个工具一键制作:

代码

  1. //=====================================================
  2. // - FileName: SpineAnimatorCreator.cs
  3. // - Author: #AuthorName#
  4. // - CreateTime: #CreateTime#
  5. // - Email: #AuthorEmail#
  6. // - Description:
  7. // - (C) Copyright 2019, webeye,Inc.
  8. // - All Rights Reserved.
  9. //======================================================
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. using Qarth;
  16. using UnityEditor;
  17. using UnityEditor.Animations;
  18. using System.IO;
  19. using Object = UnityEngine.Object;
  20. namespace GameWish.Game
  21. {
  22. public class SpineAnimatorCreator
  23. {
  24. private static string[] animNames = new string[] { "idle", "attack", "hit", "death", "walk", "appear" };
  25. [MenuItem("Assets/SpineAnimTool/AutoBuildAnimator(请选中AnimatorController和他下面的所有clips)")]
  26. private static void CreateAnimator()
  27. {
  28. AnimatorController animator = Selection.activeObject as AnimatorController;
  29. if (animator == null)
  30. {
  31. Log.e("选中物体没有状态机!!!");
  32. return;
  33. }
  34. int len = animator.parameters.Length;
  35. if (len > 0)
  36. {
  37. for (int i = 0; i < len; i++)
  38. {
  39. animator.RemoveParameter(animator.parameters[i]);
  40. }
  41. }
  42. if (animator.layers.Length > 0)
  43. {
  44. animator.RemoveLayer(0);
  45. }
  46. animator.AddLayer("Base Layer");
  47. animator.AddParameter("State", AnimatorControllerParameterType.Int);
  48. Object[] tar = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(animator));
  49. for (int i = 0; i < tar.Length; i++)
  50. {
  51. AnimationClip animationClip = new AnimationClip();
  52. animationClip = tar[i] as AnimationClip;
  53. if (animationClip != null)
  54. {
  55. if (animationClip.name.Contains("idle") || animationClip.name.Contains("walk") || animationClip.name.Contains("run"))
  56. {
  57. AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(animationClip);
  58. clipSettings.loopTime = true;
  59. AnimationUtility.SetAnimationClipSettings(animationClip, clipSettings);
  60. }
  61. animator.AddMotion(animationClip, 0);
  62. }
  63. }
  64. AnimationClip[] animationClips = animator.animationClips;
  65. AnimatorStateMachine animatorStateMachine = animator.layers[0].stateMachine;
  66. ChildAnimatorState[] cas = animatorStateMachine.states;
  67. int stateValue = 0;
  68. foreach (var item in cas)
  69. {
  70. if (item.state.name.Contains("idle"))
  71. {
  72. animatorStateMachine.defaultState = item.state;
  73. }
  74. AnimatorStateTransition atb = animatorStateMachine.AddAnyStateTransition(item.state);
  75. atb.hasExitTime = true;
  76. atb.exitTime = 0;
  77. atb.hasFixedDuration = false;
  78. atb.duration = 0;
  79. atb.canTransitionToSelf = false;
  80. atb.hasExitTime = false;
  81. for (int i = 0; i < animNames.Length; i++)
  82. {
  83. if (item.state.name.Contains(animNames[i]))
  84. {
  85. stateValue = i;
  86. }
  87. }
  88. atb.AddCondition(AnimatorConditionMode.Equals, stateValue, "State");
  89. }
  90. AssetDatabase.SaveAssets();
  91. AssetDatabase.Refresh();
  92. }
  93. }
  94. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/117875
推荐阅读
相关标签
  

闽ICP备14008679号