1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| using UnityEngine; using System.Collections; using UnityEditor; using System; using UnityEditor.Animations; using System.IO; using System.Xml; using System.Text.RegularExpressions; using System.Xml.Serialization; using System.Collections.Generic; using System.Linq;
//[CustomEditor(typeof(EditorTools))] public class EditorTools : MonoBehaviour { #region 创建动画控制器
/// <summary> /// 记录上一个state,用于自状态机中 /// </summary> static AnimatorState lastAnimatorState = null; static string ParameterName; // 动画片段 static AnimationClip die; static AnimationClip jump; static AnimationClip BackLeap; static AnimationClip ForwardLeap; /// <summary> /// base layer AnimatorStateMachine /// </summary> static AnimatorStateMachine mainASM; static int stateHeight = 100; static int stateWidth = 220;
/// <summary> /// 根据配置文件创建特技组 /// </summary> [MenuItem ("Tools/CreateAnimatorState")] static void CreateAnimatorState () { // 获取动画片段 List<object> allAssets = new List<object> (AssetDatabase.LoadAllAssetsAtPath ("Assets/Charactors/player2.FBX")); var animationClips = allAssets.Where (o => o.GetType () == typeof(AnimationClip)).ToList (); foreach (var item in animationClips) { AnimationClip x = item as AnimationClip; switch (x.name) { case "die": die = x; break; case "jump": jump = x; break; case "BackLeap": BackLeap = x; break; case "ForwardLeap": ForwardLeap = x; break; default: break; } }
// 当每个动画是一个单独的FBX文件中时可以用下面的方法来获取 //die = AssetDatabase.LoadAssetAtPath ("Assets/Charactors/player2.FBX", typeof(AnimationClip)) as AnimationClip;
// 获取状态机 AnimatorController animatorController = AssetDatabase.LoadAssetAtPath ("Assets/AnimatorController/demo.controller", typeof(AnimatorController)) as AnimatorController; AnimatorControllerLayer layer = animatorController.layers [0]; mainASM = layer.stateMachine;
// 获取当前所有的参数 AnimatorControllerParameter[] paras = animatorController.parameters; List<AnimatorControllerParameter> listParas = new List<AnimatorControllerParameter> (paras);
// 删除指定的参数 var acps = listParas.Where (p => p.name.Contains ("GroupParameter")).ToArray (); foreach (AnimatorControllerParameter item in acps) { animatorController.RemoveParameter (item); }
// 删除指定的子状态机 ChildAnimatorStateMachine[] childASM = mainASM.stateMachines; List<ChildAnimatorStateMachine> listCASM = new List<ChildAnimatorStateMachine> (childASM); var casms = listCASM.Where (c => c.stateMachine.name.Contains ("Group")).ToArray (); foreach (ChildAnimatorStateMachine item in casms) { mainASM.RemoveStateMachine (item.stateMachine); }
// 读配置文件 XmlConfig xc = ReadXml ();
Vector3 startPos = mainASM.anyStatePosition;
// 根据配置创建自状态机 for (int index = 0; index < xc.datas.Count; index++) { Data data = xc.datas [index];
// 设置特技参数, ParameterName = "GroupParameter" + data.INDEX.ToString (); animatorController.AddParameter (ParameterName, AnimatorControllerParameterType.Trigger);
// 创建子状态机 AnimatorStateMachine sub = AddSubStateMachine<AnimatorEvent> ("Group_" + data.INDEX, ParameterName, mainASM, startPos + new Vector3 (stateWidth * index, -stateHeight, 0)); // 创建子状态机中的state SetStateInSubMachine (sub, data); lastAnimatorState = null; } }
/// <summary> /// 创建sub state machine用于放置特效组中的动画 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stateName"></param> /// <param name="sm"></param> /// <param name="position"></param> /// <param name="data"></param> /// <returns></returns> private static AnimatorStateMachine AddSubStateMachine<T> (string stateName, string para, AnimatorStateMachine sm, Vector3 position) where T : StateMachineBehaviour { AnimatorStateMachine sub = sm.AddStateMachine (stateName, position); sub.AddStateMachineBehaviour<T> (); AnimatorStateTransition transition = mainASM.defaultState.AddTransition (sub, false); transition.AddCondition (AnimatorConditionMode.If, 0, para); return sub; }
/// <summary> /// 根据配置数据在子状态机中创建state /// </summary> /// <typeparam name="T"></typeparam> /// <param name="subSM"></param> /// <param name="data"></param> private static void SetStateInSubMachine (AnimatorStateMachine subSM, Data data) { AnimatorState newState; string stateName; Vector3 pos; List<AnimationClip> acArray = new List<AnimationClip> (); SetAnimationClip (data.Clip1, data.Clip1Count, ref acArray); SetAnimationClip (data.Clip2, data.Clip2Count, ref acArray); SetAnimationClip (data.Clip3, data.Clip3Count, ref acArray);
for (int x = 1; x <= acArray.Count; x++) { stateName = "GroupState_" + data.INDEX + "_" + x.ToString (); pos = subSM.entryPosition + new Vector3 (stateWidth, -stateHeight * x, 0); newState = AddState (stateName, subSM, pos, acArray [x - 1], x, acArray.Count); lastAnimatorState = newState; } }
static void SetAnimationClip (string clipName, int count, ref List<AnimationClip> acArray) { for (int i = 0; i < count; i++) { if (clipName == die.name) { acArray.Add (die); } if (clipName == jump.name) { acArray.Add (jump); } if (clipName == BackLeap.name) { acArray.Add (BackLeap); } if (clipName == ForwardLeap.name) { acArray.Add (ForwardLeap); } } }
static AnimatorState AddState<T> (string stateName, AnimatorStateMachine sm, float threshold, string parameter, Vector3 position, AnimationClip clip, bool first = false, bool last = false) where T : StateMachineBehaviour { AnimatorStateTransition animatorStateTransition; // 生成AnimatorState AnimatorState animatorState = sm.AddState (stateName, position); // 设置动画片段 animatorState.motion = clip; // 创建AnimatorStateTransition // entry连接到特技组的第一个动画 if (first) { animatorStateTransition = sm.AddAnyStateTransition (animatorState); animatorStateTransition.AddCondition (AnimatorConditionMode.Equals, threshold, parameter); } // 最后一个动画连接到stand if (last) { animatorStateTransition = animatorState.AddTransition (mainASM.defaultState); }
// 特技组内的连接创建 if (!first && !last) { animatorStateTransition = animatorState.AddTransition (mainASM.defaultState); }
animatorStateTransition = lastAnimatorState.AddTransition (animatorState, true);
//AnimatorStateTransition 的设置 animatorStateTransition.canTransitionToSelf = false; animatorState.AddStateMachineBehaviour<T> (); return animatorState; }
static AnimatorState AddState (string stateName, AnimatorStateMachine sm, Vector3 position, AnimationClip clip, int index, int count) { AnimatorStateTransition animatorStateTransition = null; // 生成AnimatorState AnimatorState animatorState = sm.AddState (stateName, position); // 设置动画片段 animatorState.motion = clip; // 创建AnimatorStateTransition // AnyState连接到特技组的第一个动画 if (index == 1) { //animatorStateTransition = sm.AddAnyStateTransition(animatorState); //animatorStateTransition.canTransitionToSelf = false; } // 最后一个动画连接到main animator machine的default state if (index == count) { animatorStateTransition = animatorState.AddTransition (mainASM.defaultState); animatorStateTransition.hasExitTime = true; }
// 特技组内的连接创建 if (lastAnimatorState != null) { animatorStateTransition = lastAnimatorState.AddTransition (animatorState, true); }
return animatorState; }
#endregion
#region public method
static XmlConfig ReadXml () { //string xmlStr = File.ReadAllText(Application.dataPath.ToString() + "/StreamingAssets/XMLConfigFiles/Stunt.xml"); //Debug.Log(xmlStr); //string objTxt = Regex.Replace(xmlStr, @"<!--[^-]*-->", string.Empty, RegexOptions.IgnoreCase); //Debug.Log(objTxt); return DeserializeFromXml<XmlConfig> (Application.dataPath.ToString () + "/StreamingAssets/XMLConfigFiles/data.xml"); }
/// <summary> /// 从某一XML文件反序列化到某一类型 /// </summary> /// <param name="filePath">待反序列化的XML文件名称</param> /// <param name="type">反序列化出的</param> /// <returns></returns> public static T DeserializeFromXml<T> (string filePath) { try { if (!System.IO.File.Exists (filePath)) throw new ArgumentNullException (filePath + " not Exists");
using (System.IO.StreamReader reader = new System.IO.StreamReader (filePath)) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer (typeof(T)); T ret = (T)xs.Deserialize (reader); return ret; } } catch (Exception ex) { return default(T); } }
#endregion }
#region 序列化需要的model [XmlType (TypeName = "config")] public class XmlConfig { [XmlArray ("datas")] public List<Data> datas { get; set; } }
[XmlType (TypeName = "data")] public class Data { [XmlAttribute] public int INDEX; [XmlAttribute] public string Clip1; [XmlAttribute] public int Clip1Count; [XmlAttribute] public string Clip2; [XmlAttribute] public int Clip2Count; [XmlAttribute] public string Clip3; [XmlAttribute] public int Clip3Count; }
#endregion
|