分享一下unity animation controll方面的内容,大家可以学习交流一下,步骤和内容都很详细。

Unity2D的制作流程

1、拿到美术给的帧动画

2、打开Animation windows 手动创建动画文件

3、创建AnimationController 手动连线

4、创建Prefab文件。

这也太麻烦了。全都手动来美术每次给你好几十个动画资源那岂不是要累死程序员了。所以我们不能手动,必须自动。

如下图所示,先看看我生成出来的结果。

我们的目标是Raw文件夹下放所有美术提供的帧动画,每个文件夹就是一组帧动画,文件夹名子就是动画的名子,代码如下所示。


  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7.  
  8. public class BuildAnimation : Editor 
  9. {
  10.  
  11.     //生成出的Prefab的路径
  12. private static string PrefabPath = "Assets/Resources/Prefabs";
  13. //生成出的AnimationController的路径
  14. private static string AnimationControllerPath = "Assets/AnimationController";
  15. //生成出的Animation的路径
  16. private static string AnimationPath = "Assets/Animation";
  17.     //美术给的原始图片路径
  18. private static string ImagePath = Application.dataPath +"/Raw";
  19.  
  20. [MenuItem("Build/BuildAnimaiton")]
  21. static void BuildAniamtion() 
  22. {
  23. DirectoryInfo raw = new DirectoryInfo (ImagePath); 
  24. foreach (DirectoryInfo dictorys in raw.GetDirectories()) 
  25. {
  26. List<AnimationClip> clips = new List<AnimationClip>();
  27. foreach (DirectoryInfo dictoryAnimations in dictorys.GetDirectories()) 
  28. {
  29. //每个文件夹就是一组帧动画,这里把每个文件夹下的所有图片生成出一个动画文件
  30. clips.Add(BuildAnimationClip(dictoryAnimations));
  31. }
  32. //把所有的动画文件生成在一个AnimationController里
  33. AnimatorController controller = BuildAnimationController(clips,dictorys.Name);
  34. //最后生成程序用的Prefab文件
  35. BuildPrefab(dictorys,controller);
  36. }
  37.  
  38.  
  39. static AnimationClip BuildAnimationClip(DirectoryInfo dictorys)
  40. {
  41. string animationName = dictorys.Name;
  42. //查找所有图片,因为我找的测试动画是.jpg 
  43. FileInfo []images  = dictorys.GetFiles("*.jpg");
  44. AnimationClip clip = new AnimationClip();
  45. AnimationUtility.SetAnimationType(clip,ModelImporterAnimationType.Generic);
  46. EditorCurveBinding curveBinding = new EditorCurveBinding();
  47. curveBinding.type = typeof(SpriteRenderer);
  48. curveBinding.path="";
  49. curveBinding.propertyName = "m_Sprite";
  50. ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[images.Length];
  51. //动画长度是按秒为单位,1/10就表示1秒切10张图片,根据项目的情况可以自己调节
  52. float frameTime = 1/10f;
  53. for(int i =0; i< images.Length; i++){
  54. Sprite sprite = Resources.LoadAssetAtPath<Sprite>(DataPathToAssetPath(images[i].FullName));
  55. keyFrames[i] =   new ObjectReferenceKeyframe ();
  56. keyFrames[i].time = frameTime *i;
  57. keyFrames[i].value = sprite;
  58. }
  59. //动画帧率,30比较合适
  60. clip.frameRate = 30;
  61.  
  62.         //有些动画我希望天生它就动画循环
  63. if(animationName.IndexOf("idle") >=0 )
  64. {
  65. //设置idle文件为循环动画
  66. SerializedObject serializedClip = new SerializedObject(clip);
  67. AnimationClipSettings clipSettings = new AnimationClipSettings(serializedClip.FindProperty("m_AnimationClipSettings"));
  68. clipSettings.loopTime = true;
  69. serializedClip.ApplyModifiedProperties();
  70. }
  71. string parentName = System.IO.Directory.GetParent(dictorys.FullName).Name;
  72. System.IO.Directory.CreateDirectory(AnimationPath +"/"+parentName);
  73. AnimationUtility.SetObjectReferenceCurve(clip,curveBinding,keyFrames);
  74. AssetDatabase.CreateAsset(clip,AnimationPath +"/"+parentName +"/" +animationName+".anim");
  75. AssetDatabase.SaveAssets();
  76. return clip;
  77. }
  78.  
  79. static AnimatorController BuildAnimationController(List<AnimationClip> clips ,string name)
  80. {
  81. AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(AnimationControllerPath +"/"+name+".controller");
  82. AnimatorControllerLayer layer = animatorController.GetLayer(0);
  83. UnityEditorInternal.StateMachine sm = layer.stateMachine;
  84. foreach(AnimationClip newClip in clips)
  85. {
  86. State  state = sm.AddState(newClip.name);
  87. state.SetAnimationClip(newClip,layer);
  88. Transition trans = sm.AddAnyStateTransition(state);
  89. trans.RemoveCondition(0);
  90. }
  91. AssetDatabase.SaveAssets();
  92. return animatorController;
  93. }
  94.  
  95. static void BuildPrefab(DirectoryInfo dictorys,AnimatorController animatorCountorller)
  96. {
  97. //生成Prefab 添加一张预览用的Sprite
  98. FileInfo images  = dictorys.GetDirectories()[0].GetFiles("*.jpg")[0];
  99. GameObject go = new GameObject();
  100. go.name = dictorys.Name;
  101. SpriteRenderer spriteRender =go.AddComponent<SpriteRenderer>();
  102. spriteRender.sprite = Resources.LoadAssetAtPath<Sprite>(DataPathToAssetPath(images.FullName));
  103. Animator animator = go.AddComponent<Animator>();
  104. animator.runtimeAnimatorController = animatorCountorller;
  105. PrefabUtility.CreatePrefab(PrefabPath+"/"+go.name+".prefab",go);
  106. DestroyImmediate(go);
  107. }
  108.  
  109.  
  110. public static string DataPathToAssetPath(string path)
  111. {
  112. if (Application.platform == RuntimePlatform.WindowsEditor)
  113. return path.Substring(path.IndexOf("Assets\\"));
  114. else
  115. return path.Substring(path.IndexOf("Assets/"));
  116. }
  117.  
  118.  
  119. class AnimationClipSettings
  120. {
  121. SerializedProperty m_Property;

  122. private SerializedProperty Get (string property) { return m_Property.FindPropertyRelative(property); }

  123. public AnimationClipSettings(SerializedProperty prop) { m_Property = prop; }

  124. public float startTime   { get { return Get("m_StartTime").floatValue; } set { Get("m_StartTime").floatValue = value; } }
  125. public float stopTime { get { return Get("m_StopTime").floatValue; }  set { Get("m_StopTime").floatValue = value; } }
  126. public float orientationOffsetY { get { return Get("m_OrientationOffsetY").floatValue; } set { Get("m_OrientationOffsetY").floatValue = value; } }
  127. public float level { get { return Get("m_Level").floatValue; } set { Get("m_Level").floatValue = value; } }
  128. public float cycleOffset { get { return Get("m_CycleOffset").floatValue; } set { Get("m_CycleOffset").floatValue = value; } }

  129. public bool loopTime { get { return Get("m_LoopTime").boolValue; } set { Get("m_LoopTime").boolValue = value; } }
  130. public bool loopBlend { get { return Get("m_LoopBlend").boolValue; } set { Get("m_LoopBlend").boolValue = value; } }
  131. public bool loopBlendOrientation { get { return Get("m_LoopBlendOrientation").boolValue; } set { Get("m_LoopBlendOrientation").boolValue = value; } }
  132. public bool loopBlendPositionY { get { return Get("m_LoopBlendPositionY").boolValue; } set { Get("m_LoopBlendPositionY").boolValue = value; } }
  133. public bool loopBlendPositionXZ { get { return Get("m_LoopBlendPositionXZ").boolValue; } set { Get("m_LoopBlendPositionXZ").boolValue = value; } }
  134. public bool keepOriginalOrientation { get { return Get("m_KeepOriginalOrientation").boolValue; } set { Get("m_KeepOriginalOrientation").boolValue = value; } }
  135. public bool keepOriginalPositionY { get { return Get("m_KeepOriginalPositionY").boolValue; } set { Get("m_KeepOriginalPositionY").boolValue = value; } }
  136. public bool keepOriginalPositionXZ { get { return Get("m_KeepOriginalPositionXZ").boolValue; } set { Get("m_KeepOriginalPositionXZ").boolValue = value; } }
  137. public bool heightFromFeet { get { return Get("m_HeightFromFeet").boolValue; } set { Get("m_HeightFromFeet").boolValue = value; } }
  138. public bool mirror { get { return Get("m_Mirror").boolValue; } set { Get("m_Mirror").boolValue = value; } }
  139. }
  140.  
  141. }
因为新版的动画系统Unity没有提供直接的API来设置动画的循环状态,所以我们只能通过写文件的形式来修改动画的天生属性。需要用到自己写封装的类 AnimationClipSettings 具体方法请看上面的代码。

有了自动生成动画的代码,就不怕美术一次给你多少组图片,或者更新了多少组图片都能很快的生成出来。

随便写一条测试脚本,来测试一下播放动画。
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NewBehaviourScript : MonoBehaviour {
  5.  
  6. Animator animator ;

  7. void Start () 
  8. {
  9. animator = GetComponent<Animator>();
  10. }
  11.  
  12.  
  13. void OnGUI()
  14. {
  15. if(GUILayout.Button("idle"))
  16. {
  17. animator.Play("idle");
  18.  
  19. }
  20. }
  21. }
好了,到这里关于unity animation controll方面的内容就结束了,动画播放的很正常的。