コード例 #1
0
 public AssetStateGroup CreateGroupAsset(string name)
 {
     AssetStateGroup asset = ScriptableObject.CreateInstance<AssetStateGroup>();
     asset.name = name;
     AssetDatabase.CreateAsset(asset, GetNewAssetPath(name));
     AssetDatabase.Refresh();
     return asset;
 }
コード例 #2
0
        /// <summary>
        /// 把 AnimatorStateMachine 转换为 AssetStateGroup
        /// </summary>
        /// <param name="originStateMachine"></param>
        /// <param name="assetStateGroup"></param>
        /// <param name="isSync"></param>
        /// <param name="groupName"></param>
        private void TransStateGroup(AnimatorStateMachine originStateMachine, AssetStateGroup assetStateGroup,
            bool isSync, AnimatorControllerLayer overrideLayer, string groupName = null)
        {
            ChildAnimatorState[] animCtrlStates = originStateMachine.states;
            List<AssetState.AnimationState> assetMotions = new List<AssetState.AnimationState>();
            List<AssetBlendTree> assetBlendTrees = new List<AssetBlendTree>();
            for (int j = 0; j < animCtrlStates.Length; j++)
            {
                EditorUtility.DisplayProgressBar("Transform State Group...", string.Format("duel with the {0} state...", j), j / (float)animCtrlStates.Length);

                AnimatorState state = animCtrlStates[j].state;

                AnimatorStateTransition[] transs = state.transitions;
                List<AssetTransitions.Transtions> stateTranstions = new List<AssetTransitions.Transtions>();
                for (int i = 0; i < transs.Length; i++)
                {
                    AnimatorStateTransition transdata = transs[i];
                    AssetTransitions.Transtions ttranstion = new AssetTransitions.Transtions();
                    ttranstion.CopyData(transdata);
                    stateTranstions.Add(ttranstion);
                }


                Motion motion = isSync ? overrideLayer.GetOverrideMotion(state) : state.motion;           // 如果是同步层,取OverrideMotion
                bool isBlendTree = motion is BlendTree;
                if (isBlendTree)
                {
                    BlendTree originBlendTree = motion as BlendTree;
                    string blendTreeName = string.Format("{0}_blendTree_{1}", assetStateGroup.name, state.name);
                    AssetBlendTree assetBlendTree = CreateBlendTreeAsset(blendTreeName);
                    TransBlendTree(originBlendTree, assetBlendTree, state.name);
                    assetBlendTree.transtions = stateTranstions.ToArray();
                    assetBlendTrees.Add(assetBlendTree);
                }
                else
                {
                    AnimationClip originClip = motion as AnimationClip;
                    AssetState.AnimationState newMotion = new AssetState.AnimationState();
                    newMotion.clip = originClip;
                    newMotion.speed = state.speed;
                    newMotion.stateName = state.name;
                    newMotion.transtions = stateTranstions.ToArray();
                    assetMotions.Add(newMotion);
                }

                
            }

            assetStateGroup.groupName = groupName;
            assetStateGroup.motions = assetMotions.ToArray();
            assetStateGroup.blendTrees = assetBlendTrees.ToArray();
        }
コード例 #3
0
        /// <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();
        }