public AssetStateController CreateControllerAsset(string name) { AssetStateController asset = ScriptableObject.CreateInstance<AssetStateController>(); asset.name = name; AssetDatabase.CreateAsset(asset, GetNewAssetPath(name)); AssetDatabase.Refresh(); return asset; }
/// <summary> /// 将AniamtorController转换为Asset /// </summary> /// <param name="path"></param> public void ExportToAsset(string exPath, AnimatorController animCtrl) { selectPath = exPath; // 生成动画控制器 AssetStateController assetCtrl = CreateControllerAsset(animCtrl.name); //生成Parameter if(animCtrl.parameters != null && animCtrl.parameters.Length > 0) { AssetParameter paramerters = CreatParameterAsset(string.Format("{0}_Paramerters", animCtrl.name)); assetCtrl.parameters = paramerters; int tlen = animCtrl.parameters.Length; paramerters.parameters = new AssetParameter.Parameter[tlen]; for (int i = 0; i < tlen; i++) { AssetParameter.Parameter tpamdata = new AssetParameter.Parameter(); tpamdata.CopyData(animCtrl.parameters[i]); paramerters.parameters[i] = tpamdata; } } // 生成动画层 AnimatorControllerLayer[] animCtrlLayers = animCtrl.layers; assetCtrl.stateLayers = new AssetStateLayer[animCtrlLayers.Length]; for (int i = 0; i < animCtrlLayers.Length; i++) { EditorUtility.DisplayProgressBar("Transform Layer...", string.Format("duel with the {0} Layer...", i), i / (float)animCtrlLayers.Length); string layerName = string.Format("{0}_layer{1}", animCtrl.name, i ); AssetStateLayer assetLayer = CreateLayerAsset(layerName); assetCtrl.stateLayers[i] = assetLayer; AnimatorControllerLayer animCtrlLayer = animCtrlLayers[i]; assetLayer.avatarMask = animCtrlLayer.avatarMask; assetLayer.isAdditive = animCtrlLayer.blendingMode == AnimatorLayerBlendingMode.Additive; assetLayer.timing = animCtrlLayer.syncedLayerAffectsTiming; // 判断是否是同步层 int syncLayerIndex = animCtrlLayer.syncedLayerIndex; bool isSync = syncLayerIndex != -1; AnimatorControllerLayer targetLayer = isSync ? animCtrlLayers[syncLayerIndex] : animCtrlLayer; assetLayer.sourceLayerIndex = syncLayerIndex; // 生成动画组 List <AssetStateGroup> assetGroups = new List<AssetStateGroup>(); // 生成默认动画组 AnimatorStateMachine animCtrlStateMachine = targetLayer.stateMachine; string defaultGroupName = string.Format("{0}_group_default", layerName); AssetStateGroup assetDefaultGroup = CreateGroupAsset(defaultGroupName); TransStateGroup(animCtrlStateMachine, assetDefaultGroup, isSync, animCtrlLayer); assetGroups.Add(assetDefaultGroup); // 生成子动画组(子控制器) ChildAnimatorStateMachine[] subMachines = animCtrlStateMachine.stateMachines; for (int j = 0; j < subMachines.Length; j++) { EditorUtility.DisplayProgressBar("Transform SubMachines...", string.Format("duel with the {0} Machine...", j), j / (float)subMachines.Length); AnimatorStateMachine originSubStateMachine = subMachines[j].stateMachine; string subGroupName = string.Format("{0}_group_{1}_{2}", layerName, j, originSubStateMachine.name); AssetStateGroup assetSubGroup = CreateGroupAsset(subGroupName); TransStateGroup(originSubStateMachine, assetSubGroup, isSync, animCtrlLayer, originSubStateMachine.name); assetGroups.Add(assetSubGroup); if (originSubStateMachine.stateMachines != null && originSubStateMachine.stateMachines.Length > 0) { Debug.LogErrorFormat("not support more then 1 layer subMachines, subMachine name:{0}", originSubStateMachine.name); } } assetLayer.stateGroups = assetGroups.ToArray(); } EditorUtility.ClearProgressBar(); }