Пример #1
0
 private void CreateController()
 {
     if (this.m_Controller == null && this.m_AvatarPreview != null && this.m_AvatarPreview.Animator != null && this.m_RefTransition != null)
     {
         this.m_LayerIndex           = 0;
         this.m_Controller           = new AnimatorController();
         this.m_Controller.pushUndo  = false;
         this.m_Controller.hideFlags = HideFlags.HideAndDontSave;
         this.m_Controller.AddLayer("preview");
         bool flag = true;
         if (this.m_LayerMask != null)
         {
             int humanoidBodyPartCount = this.m_LayerMask.humanoidBodyPartCount;
             int num = 0;
             while (num < humanoidBodyPartCount && flag)
             {
                 if (!this.m_LayerMask.GetHumanoidBodyPartActive(num))
                 {
                     flag = false;
                 }
                 num++;
             }
             if (!flag)
             {
                 this.m_Controller.AddLayer("Additionnal");
                 this.m_LayerIndex++;
                 AnimatorControllerLayer[] layers = this.m_Controller.layers;
                 layers[this.m_LayerIndex].avatarMask = this.m_LayerMask;
                 this.m_Controller.layers             = layers;
             }
         }
         this.m_StateMachine           = this.m_Controller.layers[this.m_LayerIndex].stateMachine;
         this.m_StateMachine.pushUndo  = false;
         this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave;
         this.m_SrcMotion = this.m_RefSrcState.motion;
         this.m_DstMotion = this.m_RefDstState.motion;
         this.m_ParameterMinMax.Clear();
         if (this.m_SrcMotion && this.m_SrcMotion is BlendTree)
         {
             BlendTree blendTree = this.m_SrcMotion as BlendTree;
             for (int i = 0; i < blendTree.recursiveBlendParameterCount; i++)
             {
                 string recursiveBlendParameter = blendTree.GetRecursiveBlendParameter(i);
                 if (this.m_Controller.IndexOfParameter(recursiveBlendParameter) == -1)
                 {
                     this.m_Controller.AddParameter(recursiveBlendParameter, AnimatorControllerParameterType.Float);
                     this.m_ParameterMinMax.Add(new Vector2(blendTree.GetRecursiveBlendParameterMin(i), blendTree.GetRecursiveBlendParameterMax(i)));
                 }
             }
         }
         if (this.m_DstMotion && this.m_DstMotion is BlendTree)
         {
             BlendTree blendTree2 = this.m_DstMotion as BlendTree;
             for (int j = 0; j < blendTree2.recursiveBlendParameterCount; j++)
             {
                 string recursiveBlendParameter2 = blendTree2.GetRecursiveBlendParameter(j);
                 int    num2 = this.m_Controller.IndexOfParameter(recursiveBlendParameter2);
                 if (num2 == -1)
                 {
                     this.m_Controller.AddParameter(recursiveBlendParameter2, AnimatorControllerParameterType.Float);
                     this.m_ParameterMinMax.Add(new Vector2(blendTree2.GetRecursiveBlendParameterMin(j), blendTree2.GetRecursiveBlendParameterMax(j)));
                 }
                 else
                 {
                     this.m_ParameterMinMax[num2] = new Vector2(Mathf.Min(blendTree2.GetRecursiveBlendParameterMin(j), this.m_ParameterMinMax[num2][0]), Mathf.Max(blendTree2.GetRecursiveBlendParameterMax(j), this.m_ParameterMinMax[num2][1]));
                 }
             }
         }
         this.m_SrcState           = this.m_StateMachine.AddState(this.m_RefSrcState.name);
         this.m_SrcState.pushUndo  = false;
         this.m_SrcState.hideFlags = HideFlags.HideAndDontSave;
         this.m_DstState           = this.m_StateMachine.AddState(this.m_RefDstState.name);
         this.m_DstState.pushUndo  = false;
         this.m_DstState.hideFlags = HideFlags.HideAndDontSave;
         this.CopyStateForPreview(this.m_RefSrcState, ref this.m_SrcState);
         this.CopyStateForPreview(this.m_RefDstState, ref this.m_DstState);
         this.m_Transition           = this.m_SrcState.AddTransition(this.m_DstState, true);
         this.m_Transition.pushUndo  = false;
         this.m_Transition.hideFlags = HideFlags.DontSave;
         this.CopyTransitionForPreview(this.m_RefTransition, ref this.m_Transition);
         this.DisableIKOnFeetIfNeeded();
         AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
         AnimatorController expr_400 = this.m_Controller;
         expr_400.OnAnimatorControllerDirty = (Action)Delegate.Combine(expr_400.OnAnimatorControllerDirty, new Action(this.ControllerDirty));
     }
 }
Пример #2
0
    private static void createAC(string objPath, string objName, Dictionary <string, animCond> conds, List <animClip> lstInfo)
    {
        //查找混合树
        List <animClip> blendInfo = new List <animClip>();

        for (int i = 0; i < lstInfo.Count; i++)
        {
            if (lstInfo[i].blendIndex > 0)
            {
                blendInfo.Add(lstInfo[i]);
            }
        }
        int                     index    = objPath.LastIndexOf("/");
        string                  savePath = getAnimPathByName(objName, "ac");
        string                  clipPath = getAnimPathByName(objName, "clip");
        AnimatorController      ac       = AnimatorController.CreateAnimatorControllerAtPath(Path.Combine(savePath, objName + ".controller"));
        AnimatorControllerLayer layer    = ac.layers[0];
        //添加clip
        AnimatorStateMachine stateMachine = layer.stateMachine;

        string[] files = Directory.GetFiles(clipPath, "*anim");
        Dictionary <string, Motion> dictClips = new Dictionary <string, Motion>();

        for (int i = 0; i < files.Length; i++)
        {
            Motion cp = AssetDatabase.LoadAssetAtPath <Motion>(files[i]);
            if (cp != null)
            {
                dictClips.Add(cp.name, cp);
            }
        }

        AnimatorState animState = null;

        //先创建blendTree
        if (blendInfo.Count > 0)
        {
            if (!isExitParam(ac, "tree"))
            {
                ac.AddParameter("tree", AnimatorControllerParameterType.Float);
            }
            BlendTree btree = null;
            animState            = ac.CreateBlendTreeInController(defaultAnim, out btree);
            btree.blendParameter = "tree";
            for (int i = 0; i < blendInfo.Count; i++)
            {
                string cname = blendInfo[i].clipName;
                if (dictClips.ContainsKey(cname))
                {
                    btree.AddChild(dictClips[cname]);
                }
            }
            stateMachine.AddState(animState, new Vector2(600, -200));
            stateMachine.defaultState = animState;
        }

        int startY = 0;
        int startX = 300;

        for (int i = 0; i < lstInfo.Count; i++)
        {
            if (lstInfo[i].blendIndex > 0)
            {
                continue;
            }
            string name = lstInfo[i].clipName;
            animState        = stateMachine.AddState(name, new Vector2(startX, startY * 80));
            animState.motion = dictClips[name];
            startY++;
        }
        //连线
        ChildAnimatorState[] states = stateMachine.states;
        for (int i = 0; i < states.Length; i++)
        {
            ChildAnimatorState currState = states[i];
            string             name      = currState.state.name;
            if (conds.ContainsKey(name))
            {
                animCond cond          = conds[name];
                string   fromStateName = cond.fromState;
                if (fromStateName == "Any State")
                {
                    AnimatorStateTransition trans = stateMachine.AddAnyStateTransition(currState.state);
                    if (!isExitParam(ac, cond.fromCond))
                    {
                        ac.AddParameter(cond.fromCond, AnimatorControllerParameterType.Trigger);
                    }
                    trans.AddCondition(AnimatorConditionMode.If, 0, cond.fromCond);
                }
                string             toStateName = cond.toState;
                ChildAnimatorState toState     = getState(states, toStateName);
                if (toState.state != null && name != defaultAnim)
                {
                    AnimatorStateTransition trans = currState.state.AddTransition(toState.state);
                    if (cond.toCond == "None")
                    {
                        trans.hasExitTime = false;
                    }
                    else if (cond.toCond == "Exit")
                    {
                        trans.hasExitTime = true;
                    }
                    else
                    {
                        if (!isExitParam(ac, cond.toCond))
                        {
                            ac.AddParameter(cond.toCond, AnimatorControllerParameterType.Trigger);
                        }
                        trans.AddCondition(AnimatorConditionMode.If, 0, cond.toCond);
                    }
                }
            }
        }
        ac.name = objName;
        //AssetDatabase.CreateAsset(ac, Path.Combine(savePath, objName + ".controller"));
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
Пример #3
0
    public static void AddBlendTree1DComponents(EntityManager entityManager, Entity entity, BlendTree blendTree)
    {
        entityManager.AddComponentData(entity, new BlendTree1DData
        {
            BlendParameter = new StringHash(blendTree.blendParameter)
        });

        var buffer = entityManager.AddBuffer <BlendTree1DMotionData>(entity);

        for (int i = 0; i < blendTree.children.Length; i++)
        {
            buffer.Add(new BlendTree1DMotionData
            {
                MotionThreshold = blendTree.children[i].threshold,
                MotionSpeed     = blendTree.children[i].timeScale,
                Motion          = UnityEditor.Animations.BlendTreeConvertHelper.Convert(blendTree.children[i].motion),
                MotionType      = UnityEditor.Animations.BlendTreeConvertHelper.GetMotionType(blendTree.children[i].motion),
            });
        }
    }
Пример #4
0
    public static void AddBlendTree2DComponents(EntityManager entityManager, Entity entity, BlendTree blendTree)
    {
        var blendSpaceData = new BlendTree2DData
        {
            BlendParam  = blendTree.blendParameter,
            BlendParamY = blendTree.blendParameterY,
        };

        entityManager.AddComponentData(entity, blendSpaceData);


        var blendSpaceEntries = entityManager.AddBuffer <BlendTree2DMotionData>(entity);

        for (int i = 0; i < blendTree.children.Length; i++)
        {
            blendSpaceEntries.Add(new BlendTree2DMotionData
            {
                MotionPosition = blendTree.children[i].position,
                MotionSpeed    = blendTree.children[i].timeScale,
                Motion         = UnityEditor.Animations.BlendTreeConvertHelper.Convert(blendTree.children[i].motion),
                MotionType     = UnityEditor.Animations.BlendTreeConvertHelper.GetMotionType(blendTree.children[i].motion),
            });
        }
    }
Пример #5
0
        /// <summary>Initializes the `transition` based on the <see cref="BlendTree.children"/>.</summary>
        private static ChildMotion[] InitializeChildren <TTransition, TState>(ref TTransition transition, BlendTree blendTree)
            where TTransition : ManualMixerTransition <TState>, new()
            where TState : ManualMixerState
        {
            transition = new TTransition();

            var children = blendTree.children;

            transition.Animations = new Object[children.Length];
            float[] speeds          = new float[children.Length];
            var     hasCustomSpeeds = false;

            for (int i = 0; i < children.Length; i++)
            {
                var child = children[i];
                transition.Animations[i] = child.motion is AnimationClip ?
                                           child.motion :
                                           (Object)GenerateTransition(blendTree, child.motion);

                if ((speeds[i] = child.timeScale) != 1)
                {
                    hasCustomSpeeds = true;
                }
            }

            if (hasCustomSpeeds)
            {
                transition.Speeds = speeds;
            }

            return(children);
        }
Пример #6
0
 public static void AddChild(this BlendTree blendTree, Motion motion, float threshold, string parameter)
 {
     blendTree.AddChild(motion, Vector2.zero, threshold, parameter);
 }
        public int TraverseBlendTree(ref BlendTree tree)
        {
            bool treeExist;
            var  treeInfo = controllerInfo.blendTrees.AddObject(tree, out treeExist);

            if (treeExist)
            {
                return(treeInfo.Key);
            }
            var json = treeInfo.Value;

            json.AddField("name", tree.name);

            var childrenJSON = new JSONObject(JSONObject.Type.ARRAY);

            json.AddField("children", childrenJSON);
            ChildMotion[] children = tree.children;
            for (int i = 0; i < children.Length; i++)
            {
                var childJSON = new JSONObject(JSONObject.Type.OBJECT);
                childrenJSON.Add(childJSON);
                var    motionJSON = new JSONObject(JSONObject.Type.OBJECT);
                Motion motion     = children[i].motion;
                if (motion.GetType() == typeof(AnimationClip))
                {
                    AnimationClip clip = motion as AnimationClip;
                    motionJSON.AddField("type", "AnimationClip");
                    motionJSON.AddField("id", HandleAnimationClip(ref clip));
                }
                else if (motion.GetType() == typeof(BlendTree))
                {
                    BlendTree sub = motion as BlendTree;
                    motionJSON.AddField("type", "BlendTree");
                    motionJSON.AddField("id", TraverseBlendTree(ref sub));
                }
                childJSON.AddField("motion", motionJSON);
                childJSON.AddField("timeScale", children[i].timeScale);
            }

            var blendJSON = new JSONObject(JSONObject.Type.OBJECT);

            json.AddField("blend", blendJSON);
            blendJSON.AddField("type", (int)tree.blendType);
            if (tree.blendType == BlendTreeType.Simple1D)
            {
                bool temp;
                blendJSON.AddField("parameter", controllerInfo.parameters.AddObject(tree.blendParameter, out temp).Key);
                var thresholdsJSON = new JSONObject(JSONObject.Type.ARRAY);
                blendJSON.AddField("thresholds", thresholdsJSON);
                for (int i = 0; i < children.Length; i++)
                {
                    thresholdsJSON.Add(children[i].threshold);
                }
                blendJSON.AddField("minThreshold", tree.minThreshold);
                blendJSON.AddField("maxThreshold", tree.maxThreshold);
            }
            else if (tree.blendType == BlendTreeType.SimpleDirectional2D || tree.blendType == BlendTreeType.FreeformDirectional2D || tree.blendType == BlendTreeType.FreeformCartesian2D)
            {
                bool temp;
                blendJSON.AddField("parameterX", controllerInfo.parameters.AddObject(tree.blendParameter, out temp).Key);
                blendJSON.AddField("parameterY", controllerInfo.parameters.AddObject(tree.blendParameterY, out temp).Key);
                var positionsJSON = new JSONObject(JSONObject.Type.ARRAY);
                blendJSON.AddField("positions", positionsJSON);
                for (int i = 0; i < children.Length; i++)
                {
                    var pos     = children[i].position;
                    var posJSON = new JSONObject(JSONObject.Type.ARRAY);
                    positionsJSON.Add(posJSON);
                    posJSON.Add(pos.x);
                    posJSON.Add(pos.y);
                }
            }
            else if (tree.blendType == BlendTreeType.Direct)
            {
                var paramJSON = new JSONObject(JSONObject.Type.ARRAY);
                blendJSON.AddField("parameters", paramJSON);
                for (int i = 0; i < children.Length; i++)
                {
                    if (children[i].directBlendParameter != null)
                    {
                        paramJSON.Add(children[i].directBlendParameter);
                    }
                    else
                    {
                        paramJSON.Add(new JSONObject(JSONObject.Type.NULL));
                    }
                }
            }
            return(treeInfo.Key);
        }
        void CreateController()
        {
            if (m_Controller == null && m_AvatarPreview != null && m_AvatarPreview.Animator != null && m_RefTransition != null)
            {
                // controller
                m_LayerIndex           = 0;
                m_Controller           = new AnimatorController();
                m_Controller.pushUndo  = false;
                m_Controller.hideFlags = HideFlags.HideAndDontSave;
                m_Controller.AddLayer("preview");

                bool isDefaultMask = true;

                if (m_LayerMask != null)
                {
                    for (AvatarMaskBodyPart i = 0; i < AvatarMaskBodyPart.LastBodyPart && isDefaultMask; i++)
                    {
                        if (!m_LayerMask.GetHumanoidBodyPartActive(i))
                        {
                            isDefaultMask = false;
                        }
                    }

                    if (!isDefaultMask)
                    {
                        m_Controller.AddLayer("Additionnal");
                        m_LayerIndex++;
                        AnimatorControllerLayer[] layers = m_Controller.layers;
                        layers[m_LayerIndex].avatarMask = m_LayerMask;
                        m_Controller.layers             = layers;
                    }
                }
                m_StateMachine           = m_Controller.layers[m_LayerIndex].stateMachine;
                m_StateMachine.pushUndo  = false;
                m_StateMachine.hideFlags = HideFlags.HideAndDontSave;

                m_SrcMotion = m_RefSrcState.motion;
                m_DstMotion = m_RefDstState.motion;

                /// Add parameters
                m_ParameterMinMax.Clear();

                if (m_SrcMotion && m_SrcMotion is BlendTree)
                {
                    BlendTree leftBlendTree = m_SrcMotion as BlendTree;

                    for (int i = 0; i < leftBlendTree.recursiveBlendParameterCount; i++)
                    {
                        string blendValueName = leftBlendTree.GetRecursiveBlendParameter(i);
                        if (m_Controller.IndexOfParameter(blendValueName) == -1)
                        {
                            m_Controller.AddParameter(blendValueName, AnimatorControllerParameterType.Float);
                            m_ParameterMinMax.Add(new Vector2(leftBlendTree.GetRecursiveBlendParameterMin(i), leftBlendTree.GetRecursiveBlendParameterMax(i)));
                        }
                    }
                }

                if (m_DstMotion && m_DstMotion is BlendTree)
                {
                    BlendTree rightBlendTree = m_DstMotion as BlendTree;

                    for (int i = 0; i < rightBlendTree.recursiveBlendParameterCount; i++)
                    {
                        string blendValueName = rightBlendTree.GetRecursiveBlendParameter(i);
                        int    parameterIndex = m_Controller.IndexOfParameter(blendValueName);
                        if (parameterIndex == -1)
                        {
                            m_Controller.AddParameter(blendValueName, AnimatorControllerParameterType.Float);
                            m_ParameterMinMax.Add(new Vector2(rightBlendTree.GetRecursiveBlendParameterMin(i), rightBlendTree.GetRecursiveBlendParameterMax(i)));
                        }
                        else
                        {
                            m_ParameterMinMax[parameterIndex] =
                                new Vector2(Mathf.Min(rightBlendTree.GetRecursiveBlendParameterMin(i), m_ParameterMinMax[parameterIndex][0]),
                                            Mathf.Max(rightBlendTree.GetRecursiveBlendParameterMax(i), m_ParameterMinMax[parameterIndex][1]));
                        }
                    }
                }


                // states
                m_SrcState           = m_StateMachine.AddState(m_RefSrcState.name);
                m_SrcState.pushUndo  = false;
                m_SrcState.hideFlags = HideFlags.HideAndDontSave;
                m_DstState           = m_StateMachine.AddState(m_RefDstState.name);
                m_DstState.pushUndo  = false;
                m_DstState.hideFlags = HideFlags.HideAndDontSave;

                CopyStateForPreview(m_RefSrcState, ref m_SrcState);
                CopyStateForPreview(m_RefDstState, ref m_DstState);

                // transition
                m_Transition           = m_SrcState.AddTransition(m_DstState, true);
                m_Transition.pushUndo  = false;
                m_Transition.hideFlags = HideFlags.DontSave;
                CopyTransitionForPreview(m_RefTransition, ref m_Transition);

                DisableIKOnFeetIfNeeded();


                AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);

                m_Controller.OnAnimatorControllerDirty += ControllerDirty;
            }
        }
 public void BlendTreeHierarchyChanged(BlendTree blendTree)
 {
     this.blendTreeGraph.BuildFromBlendTree(this.blendTreeGraph.rootBlendTree);
     base.Repaint();
 }
        public int HandleState(ref AnimatorState state)
        {
            bool stateExist;
            var  stateInfo = controllerInfo.states.AddObject(state, out stateExist);

            if (!stateExist)
            {
                stateInfo.Value.AddField("name", state.name);
                var        stateJSON        = stateInfo.Value;
                JSONObject stateTransitions = new JSONObject(JSONObject.Type.ARRAY);
                for (int j = 0; j < state.transitions.Length; j++)
                {
                    AnimatorStateTransition trans = state.transitions[j];
                    stateTransitions.Add(HandleStateTransition(state.name, ref trans));
                }
                stateJSON.AddField("transitions", stateTransitions);

                // animationClip
                Motion motion = state.motion;
                if (motion == null)
                {
                    stateJSON.AddField("motion", new JSONObject(JSONObject.Type.NULL));
                }
                else if (motion.GetType() == typeof(AnimationClip))
                {
                    JSONObject    clipJSON = new JSONObject(JSONObject.Type.OBJECT);
                    AnimationClip clip     = motion as AnimationClip;
                    if (handleBeforeAnimationClip != null && !handleBeforeAnimationClip(clip, state, gameObject))
                    {
                        // 如果return false,说明这个animationclip被外面所接管
                        clipJSON.AddField("id", "");
                    }
                    else
                    {
                        string clipUid = HandleAnimationClip(ref clip);
                        clipJSON.AddField("id", clipUid);
                    }
                    clipJSON.AddField("type", "AnimationClip");
                    stateJSON.AddField("motion", clipJSON);
                }
                else if (motion.GetType() == typeof(BlendTree))
                {
                    JSONObject btJSON = new JSONObject(JSONObject.Type.OBJECT);
                    BlendTree  bt     = motion as BlendTree;
                    btJSON.AddField("type", "BlendTree");
                    btJSON.AddField("id", TraverseBlendTree(ref bt));
                    stateJSON.AddField("motion", btJSON);
                }
                stateJSON.AddField("cycleOffset", state.cycleOffset);
                if (state.cycleOffsetParameterActive)
                {
                    stateJSON.AddField("cycleOffsetParameter", state.cycleOffsetParameter);
                }
                else
                {
                    stateJSON.AddField("cycleOffsetParameter", new JSONObject(JSONObject.Type.NULL));
                }
                stateJSON.AddField("mirror", state.mirror);
                if (state.mirrorParameterActive)
                {
                    stateJSON.AddField("mirrorParameter", state.mirrorParameter);
                }
                else
                {
                    stateJSON.AddField("mirrorParameter", new JSONObject(JSONObject.Type.NULL));
                }
                stateInfo.Value.AddField("speed", state.speed);
                if (state.speedParameterActive)
                {
                    stateJSON.AddField("speedParameter", state.speedParameter);
                }
                else
                {
                    stateJSON.AddField("speedParameter", new JSONObject(JSONObject.Type.NULL));
                }
#if UNITY_2017_1_OR_NEWER
                if (state.timeParameterActive)
                {
                    stateJSON.AddField("timeParameter", state.timeParameter);
                }
                else
                {
#endif
                stateJSON.AddField("timeParameter", new JSONObject(JSONObject.Type.NULL));
#if UNITY_2017_1_OR_NEWER
            }
#endif
                return(stateInfo.Key);
            }
            else
            {
                return(stateInfo.Key);
            }
        }
 public void BlendParameterInputChanged(BlendTree blendTree)
 {
     base.Repaint();
 }
 public State(BlendTree value)
 {
     this.value            = value;
     this.stateNameLength  = value.name.Length;
     this.motionNameLength = value.children.ToList().OrderBy(i => i.motion.name).First().motion.name.Length;
 }
Пример #13
0
        public UnityEditor.Animations.BlendTree GetBlendTreeToMotion(AnimatorController animatorController, string name, BlendTree blendTreeConfig)
        {
            UnityEditor.Animations.BlendTree blendTree = new UnityEditor.Animations.BlendTree();

            blendTree.name = name;

            List <UnityEditor.Animations.ChildMotion> childMotionList = new List <UnityEditor.Animations.ChildMotion>();

            for (int i = 0; i < blendTreeConfig.childMotions.Count; i++)
            {
                var childMotionConfig = blendTreeConfig.childMotions[i];
                if (childMotionConfig.blendTree != null &&
                    string.IsNullOrWhiteSpace(childMotionConfig.blendTree.blendParameter) == false)
                {
                    UnityEditor.Animations.ChildMotion childMotion = new UnityEditor.Animations.ChildMotion();
                    childMotion.threshold = childMotionConfig.threshold;
                    childMotion.timeScale = childMotionConfig.timeScale;
                    childMotion.motion    = GetBlendTreeToMotion(animatorController, blendTree.name + "_" + i, childMotionConfig.blendTree);
                    childMotionList.Add(childMotion);
                }
                else
                {
                    UnityEditor.Animations.ChildMotion childMotion = new UnityEditor.Animations.ChildMotion();
                    childMotion.threshold = childMotionConfig.threshold;
                    childMotion.timeScale = childMotionConfig.timeScale;
                    childMotionList.Add(childMotion);
                }
            }

            blendTree.children = childMotionList.ToArray();

            blendTree.blendParameter         = blendTreeConfig.blendParameter;
            blendTree.useAutomaticThresholds = blendTreeConfig.useAutomaticThresholds;

            if (blendTreeConfig.useAutomaticThresholds)
            {
                if (blendTreeConfig.minThreshold > int.MinValue && blendTreeConfig.maxThreshold > int.MinValue)
                {
                    blendTree.minThreshold = blendTreeConfig.minThreshold;
                    blendTree.maxThreshold = blendTreeConfig.maxThreshold;
                }
                else
                {
                    blendTree.minThreshold = 0;
                    blendTree.maxThreshold = blendTree.children.Length - 1;
                }
            }

            if (AssetDatabase.GetAssetPath(animatorController) != "")
            {
                var objList = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(animatorController));

                foreach (var item in objList)
                {
                    if (item != null && blendTree.name == item.name)
                    {
                        DestroyImmediate(item, true);
                    }
                }
                AssetDatabase.AddObjectToAsset(blendTree, AssetDatabase.GetAssetPath(animatorController));
                Debug.Log(AssetDatabase.GetAssetPath(blendTree));
            }
            return(blendTree);
        }
Пример #14
0
        /// <summary>
        /// 查找stateMachine中的state
        /// </summary>
        /// <param name="_stateMachine"></param>
        /// <param name="_tempStateDic"></param>
        private void ForEachState(AnimatorControllerLayer _layer, AnimatorStateMachine _stateMachine, Dictionary <string, State> _tempStateDic)
        {
            string keyStr    = string.Empty;
            bool   baseState = _layer.name == _stateMachine.name;

            _stateMachine.states.ToList().ForEach((state) =>
            {
                if (state.state.motion == null)
                {
                    Debug.LogError(string.Format("AnimationState : {0}'s motion is null", state.state.name));

                    if (baseState)
                    {
                        keyStr = state.state.name;
                    }
                    else
                    {
                        keyStr = string.Format("{0}/{1}", _stateMachine.name, state.state.name);
                    }

                    _tempStateDic.Add(keyStr, new State(state.state, _layer.name, keyStr));
                }
                else
                {
                    if (state.state.motion.GetType() == typeof(BlendTree))
                    {
                        BlendTree blendTree = (BlendTree)state.state.motion;

                        var blendTreeDic = GetBlendTreeDic(blendTree);
                        blendTreeDic.ToList().ForEach((pair) =>
                        {
                            if (baseState)
                            {
                                keyStr = string.Format("{0}/{1}", state.state.name, pair.Key);
                            }
                            else
                            {
                                keyStr = string.Format("{0}/{1}/{2}", _stateMachine.name, state.state.name, pair.Key);
                            }

                            _tempStateDic.Add(keyStr, new State(pair.Value, _layer.name, keyStr));
                        });
                    }
                    else
                    {
                        if (baseState)
                        {
                            keyStr = state.state.name;
                        }
                        else
                        {
                            keyStr = string.Format("{0}/{1}", _stateMachine.name, state.state.name);
                        }

                        _tempStateDic.Add(keyStr, new State(state.state, _layer.name, keyStr));
                    }
                }
            });

            _stateMachine.stateMachines.ToList().ForEach((stateMachine) =>
            {
                ForEachState(_layer, stateMachine.stateMachine, _tempStateDic);
            });
        }
Пример #15
0
        private void ForComplexMassiveBlend(List <TransitionCondition> transitionConditions, List <IAnimatedBehavior> behaviors, BlendTree originalBlendTreeTemplate)
        {
            foreach (var transitionCondition in transitionConditions)
            {
                var motions = behaviors.Select(Derive).ToList();
                var state   = CreateComplexMassiveBlendState(motions, originalBlendTreeTemplate, ToPotentialGridPosition(transitionCondition));

                CreateTransition(GetNullableStageValue(transitionCondition),
                                 transitionCondition.Permutation,
                                 state, transitionCondition.TransitionDuration);
            }
        }
Пример #16
0
 public State(BlendTree value, string _layerName, string _statePath)
 {
     this.value     = value;
     this.layerName = _layerName;
     this.statePath = _statePath;
 }
Пример #17
0
 private static void RegisterBlendTreeAsAsset(AnimatorController animatorController, BlendTree blendTree)
 {
     if (AssetDatabase.GetAssetPath(animatorController) != "")
     {
         AssetDatabase.AddObjectToAsset(blendTree, AssetDatabase.GetAssetPath(animatorController));
     }
 }
Пример #18
0
 private void CreateController()
 {
     if (((this.m_Controller == null) && (this.m_AvatarPreview != null)) && ((this.m_AvatarPreview.Animator != null) && (this.m_RefTransition != null)))
     {
         this.m_LayerIndex           = 0;
         this.m_Controller           = new AnimatorController();
         this.m_Controller.pushUndo  = false;
         this.m_Controller.hideFlags = HideFlags.HideAndDontSave;
         this.m_Controller.AddLayer("preview");
         bool flag = true;
         if (this.m_LayerMask != null)
         {
             for (AvatarMaskBodyPart part = AvatarMaskBodyPart.Root; (part < AvatarMaskBodyPart.LastBodyPart) && flag; part += 1)
             {
                 if (!this.m_LayerMask.GetHumanoidBodyPartActive(part))
                 {
                     flag = false;
                 }
             }
             if (!flag)
             {
                 this.m_Controller.AddLayer("Additionnal");
                 this.m_LayerIndex++;
                 AnimatorControllerLayer[] layers = this.m_Controller.layers;
                 layers[this.m_LayerIndex].avatarMask = this.m_LayerMask;
                 this.m_Controller.layers             = layers;
             }
         }
         this.m_StateMachine           = this.m_Controller.layers[this.m_LayerIndex].stateMachine;
         this.m_StateMachine.pushUndo  = false;
         this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave;
         this.m_SrcMotion = this.m_RefSrcState.motion;
         this.m_DstMotion = this.m_RefDstState.motion;
         this.m_ParameterMinMax.Clear();
         if ((this.m_SrcMotion != null) && (this.m_SrcMotion is BlendTree))
         {
             BlendTree srcMotion = this.m_SrcMotion as BlendTree;
             for (int i = 0; i < srcMotion.recursiveBlendParameterCount; i++)
             {
                 string recursiveBlendParameter = srcMotion.GetRecursiveBlendParameter(i);
                 if (this.m_Controller.IndexOfParameter(recursiveBlendParameter) == -1)
                 {
                     this.m_Controller.AddParameter(recursiveBlendParameter, AnimatorControllerParameterType.Float);
                     this.m_ParameterMinMax.Add(new Vector2(srcMotion.GetRecursiveBlendParameterMin(i), srcMotion.GetRecursiveBlendParameterMax(i)));
                 }
             }
         }
         if ((this.m_DstMotion != null) && (this.m_DstMotion is BlendTree))
         {
             BlendTree dstMotion = this.m_DstMotion as BlendTree;
             for (int j = 0; j < dstMotion.recursiveBlendParameterCount; j++)
             {
                 string name = dstMotion.GetRecursiveBlendParameter(j);
                 int    num3 = this.m_Controller.IndexOfParameter(name);
                 if (num3 == -1)
                 {
                     this.m_Controller.AddParameter(name, AnimatorControllerParameterType.Float);
                     this.m_ParameterMinMax.Add(new Vector2(dstMotion.GetRecursiveBlendParameterMin(j), dstMotion.GetRecursiveBlendParameterMax(j)));
                 }
                 else
                 {
                     Vector2 vector  = this.m_ParameterMinMax[num3];
                     Vector2 vector2 = this.m_ParameterMinMax[num3];
                     this.m_ParameterMinMax[num3] = new Vector2(Mathf.Min(dstMotion.GetRecursiveBlendParameterMin(j), vector[0]), Mathf.Max(dstMotion.GetRecursiveBlendParameterMax(j), vector2[1]));
                 }
             }
         }
         this.m_SrcState           = this.m_StateMachine.AddState(this.m_RefSrcState.name);
         this.m_SrcState.pushUndo  = false;
         this.m_SrcState.hideFlags = HideFlags.HideAndDontSave;
         this.m_DstState           = this.m_StateMachine.AddState(this.m_RefDstState.name);
         this.m_DstState.pushUndo  = false;
         this.m_DstState.hideFlags = HideFlags.HideAndDontSave;
         this.CopyStateForPreview(this.m_RefSrcState, ref this.m_SrcState);
         this.CopyStateForPreview(this.m_RefDstState, ref this.m_DstState);
         this.m_Transition           = this.m_SrcState.AddTransition(this.m_DstState, true);
         this.m_Transition.pushUndo  = false;
         this.m_Transition.hideFlags = HideFlags.DontSave;
         this.CopyTransitionForPreview(this.m_RefTransition, ref this.m_Transition);
         this.DisableIKOnFeetIfNeeded();
         AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
         this.m_Controller.OnAnimatorControllerDirty = (Action)Delegate.Combine(this.m_Controller.OnAnimatorControllerDirty, new Action(this.ControllerDirty));
     }
 }
Пример #19
0
    void CreateANewController()
    {
        var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/" + animationControllerName + ".controller");

        controller.AddParameter("Forwards", AnimatorControllerParameterType.Float);
        controller.AddParameter("Horizontal", AnimatorControllerParameterType.Float);
        controller.AddParameter("Crouching", AnimatorControllerParameterType.Bool);
        controller.AddParameter("Speed", AnimatorControllerParameterType.Float);
        controller.AddParameter("Engaging", AnimatorControllerParameterType.Bool);

        controller.AddParameter("Melee", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("Reloading", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("Fire", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("Sprinting", AnimatorControllerParameterType.Bool);


        //LAYER 1///////////////////////////////////////////
        //Add Outer States
        var baseStateMachine = controller.layers[0].stateMachine;


        //Set Move Tree
        //new BlendTree();
        BlendTree     moveTree  = new BlendTree();
        AnimatorState moveState = controller.CreateBlendTreeInController("Move", out moveTree, 0);

        //Add Transitions
        AnimatorState baseCrouching = null;

        if (crouchingAnimation)
        {
            baseCrouching        = baseStateMachine.AddState("Crouch");
            baseCrouching.motion = crouchingAnimation;

            //Move
            var moveToCrouchTranistion = moveState.AddTransition(baseCrouching, false);
            moveToCrouchTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Crouching");

            var crouchToMoveTranistion = baseCrouching.AddTransition(moveState, false);
            crouchToMoveTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0.25f, "Crouching");
        }

        if (fullBodyMeleeAnimation && meleeAnimation)
        {
            var baseMelee = baseStateMachine.AddState("Melee");
            baseMelee.motion = meleeAnimation;

            //Melee
            if (baseCrouching != null)
            {
                var crouchToMeleeTranistion = baseCrouching.AddTransition(baseMelee, false);
                crouchToMeleeTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Melee");
            }

            //Move
            var moveToMeleeTranistion = moveState.AddTransition(baseMelee, false);
            moveToMeleeTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Melee");

            baseMelee.AddTransition(moveState, true);
        }

        //
        if (sprintingAnimation)
        {
            var baseSprint = baseStateMachine.AddState("Sprint");
            baseSprint.motion = sprintingAnimation;

            //Move
            var moveToSprintTranistion = moveState.AddTransition(baseSprint, false);
            moveToSprintTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Sprinting");

            var sprintToMoveTranistion = baseSprint.AddTransition(moveState, false);
            sprintToMoveTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0.25f, "Sprinting");
        }
        //Set Parameters
        moveTree.blendType       = BlendTreeType.FreeformDirectional2D;
        moveTree.blendParameter  = "Horizontal";
        moveTree.blendParameterY = "Forwards";

        //Add Animations
        float walkThreshold = 0.5f;
        float runThreshold  = 1.0f;

        moveTree.AddChild(idleAnimation, new Vector2(0.0f, 0.0f));

        moveTree.AddChild(runForwardsAnimation, new Vector2(0.0f, runThreshold));
        if (walkForwardsAnimation)
        {
            moveTree.AddChild(walkForwardsAnimation, new Vector2(0.0f, walkThreshold));
        }

        moveTree.AddChild(runBackwardsAnimation, new Vector2(0.0f, -runThreshold));
        if (walkBackwardsAnimation)
        {
            moveTree.AddChild(walkBackwardsAnimation, new Vector2(0.0f, -walkThreshold));
        }

        moveTree.AddChild(runRightAnimation, new Vector2(runThreshold, 0.0f));
        if (walkRightAnimation)
        {
            moveTree.AddChild(walkRightAnimation, new Vector2(walkThreshold, 0.0f));
        }

        moveTree.AddChild(runLeftAnimation, new Vector2(-runThreshold, 0.0f));
        if (walkLeftAnimation)
        {
            moveTree.AddChild(walkLeftAnimation, new Vector2(-walkThreshold, 0.0f));
        }

        ////////////////////////////////////////////////////
        //Layer 2
        ////////////////////////////////////////////////////

        controller.AddLayer("UpperBody");
        var upperStateMachine = controller.layers[1].stateMachine;


        var upperAim = upperStateMachine.AddState("Aim");

        upperAim.motion = aimingAnimation;

        BlendTree     idleUpperTree = new BlendTree();
        AnimatorState upperIdle     = controller.CreateBlendTreeInController("Idle", out idleUpperTree, 1);

        idleUpperTree.blendParameter = "Speed";

        idleUpperTree.AddChild(idleUpperAnim, 0.0f);
        if (walkUpperAnim)
        {
            idleUpperTree.AddChild(walkUpperAnim, 0.5f);
        }
        if (runUpperAnim)
        {
            idleUpperTree.AddChild(runUpperAnim, 1.0f);
        }


        //Idle Transitions
        var idleToAim = upperIdle.AddTransition(upperAim, false);

        idleToAim.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Engaging");

        var aimToidle = upperAim.AddTransition(upperIdle, false);

        aimToidle.AddCondition(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0.25f, "Engaging");

        //Melee
        if (meleeAnimation)
        {
            AnimatorState upperMelee = upperStateMachine.AddState("Melee");
            upperMelee.motion = meleeAnimation;

            var aimToMeleeTranistion = upperAim.AddTransition(upperMelee, false);
            aimToMeleeTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Melee");

            upperMelee.AddTransition(upperAim, true);
        }
        if (sprintingAnimation)
        {
            var upperSprint = upperStateMachine.AddState("Sprint");
            upperSprint.motion = sprintingAnimation;

            //Move
            var aimToSprintTranistion = upperAim.AddTransition(upperSprint, false);
            aimToSprintTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Sprinting");

            var sprintToAimTranistion = upperSprint.AddTransition(upperAim, false);
            sprintToAimTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0.25f, "Sprinting");
        }

        //Reload
        if (reloadAnimation)
        {
            AnimatorState reloadAnim = upperStateMachine.AddState("Reload");
            reloadAnim.motion = reloadAnimation;

            var aimToReloadTranistion = upperAim.AddTransition(reloadAnim, false);
            aimToReloadTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Reloading");

            reloadAnim.AddTransition(upperAim, true);
        }
        //Fire
        if (fireAnimation)
        {
            AnimatorState fireAnim = upperStateMachine.AddState("Fire");
            fireAnim.motion = fireAnimation;

            var aimToFireTranistion = upperAim.AddTransition(fireAnim, false);
            aimToFireTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Fire");

            fireAnim.AddTransition(upperAim, true);
        }
    }
    void CreateANewController()
    {
        var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath ("Assets/"+animationControllerName+".controller");

            controller.AddParameter("Forwards", AnimatorControllerParameterType.Float);
            controller.AddParameter("Horizontal", AnimatorControllerParameterType.Float);
            controller.AddParameter("Crouching", AnimatorControllerParameterType.Bool);
            controller.AddParameter("Speed", AnimatorControllerParameterType.Float);
            controller.AddParameter("Engaging", AnimatorControllerParameterType.Bool);

            controller.AddParameter("Melee", AnimatorControllerParameterType.Trigger);
            controller.AddParameter("Reloading", AnimatorControllerParameterType.Trigger);
            controller.AddParameter("Fire", AnimatorControllerParameterType.Trigger);

            //LAYER 1///////////////////////////////////////////
            //Add Outer States
            var baseStateMachine = controller.layers[0].stateMachine;

            //Set Move Tree
            //new BlendTree();
            BlendTree moveTree = new BlendTree();
            AnimatorState moveState = controller.CreateBlendTreeInController("Move", out moveTree, 0);

            //Add Transitions
            AnimatorState baseCrouching = null;
            if(crouchingAnimation){
                baseCrouching = baseStateMachine.AddState("Crouch");
                baseCrouching.motion = crouchingAnimation;

                //Move
                var moveToCrouchTranistion = moveState.AddTransition(baseCrouching, false);
                moveToCrouchTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Crouching");

                var crouchToMoveTranistion = baseCrouching.AddTransition(moveState, false);
                crouchToMoveTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0.25f, "Crouching");

            }

            if(fullBodyMeleeAnimation && meleeAnimation){
                var baseMelee = baseStateMachine.AddState("Melee");
                baseMelee.motion = meleeAnimation;

                //Melee
                if(baseCrouching != null){
                    var crouchToMeleeTranistion = baseCrouching.AddTransition(baseMelee, false);
                    crouchToMeleeTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Melee");
                }

                //Move
                var moveToMeleeTranistion = moveState.AddTransition(baseMelee, false);
                moveToMeleeTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Melee");

            }

            //Set Parameters
            moveTree.blendType = BlendTreeType.FreeformDirectional2D;
            moveTree.blendParameter = "Horizontal";
            moveTree.blendParameterY = "Forwards";

            //Add Animations
            float walkThreshold = 0.5f;
            float runThreshold = 1.0f;

            moveTree.AddChild(idleAnimation, new Vector2(0.0f, 0.0f));

            moveTree.AddChild(runForwardsAnimation, new Vector2(0.0f, runThreshold));
            if (walkForwardsAnimation)
            {
                moveTree.AddChild(walkForwardsAnimation, new Vector2(0.0f, walkThreshold));
            }

            moveTree.AddChild(runBackwardsAnimation, new Vector2(0.0f, -runThreshold));
            if (walkBackwardsAnimation)
            {
                moveTree.AddChild(walkBackwardsAnimation, new Vector2(0.0f, -walkThreshold));
            }

            moveTree.AddChild(runRightAnimation, new Vector2(runThreshold, 0.0f));
            if (walkRightAnimation)
            {
                moveTree.AddChild(walkRightAnimation, new Vector2(walkThreshold, 0.0f));
            }

            moveTree.AddChild(runLeftAnimation, new Vector2(-runThreshold, 0.0f));
            if (walkLeftAnimation)
            {
                moveTree.AddChild(walkLeftAnimation, new Vector2(-walkThreshold, 0.0f));
            }

            ////////////////////////////////////////////////////
            //Layer 2
            ////////////////////////////////////////////////////

            controller.AddLayer("UpperBody");
            var upperStateMachine = controller.layers[1].stateMachine;

            var upperAim = upperStateMachine.AddState("Aim");
            upperAim.motion = aimingAnimation;

            BlendTree idleUpperTree = new BlendTree();
            AnimatorState upperIdle = controller.CreateBlendTreeInController("Idle", out idleUpperTree, 1);
            idleUpperTree.blendParameter = "Speed";

            idleUpperTree.AddChild(idleUpperAnim,0.0f);
            if(walkUpperAnim)
                idleUpperTree.AddChild(walkUpperAnim,0.5f);
            if(runUpperAnim)
                idleUpperTree.AddChild(runUpperAnim,1.0f);

            //Idle Transitions
            var idleToAim = upperIdle.AddTransition(upperAim, false);
            idleToAim.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Engaging");

            var aimToidle = upperAim.AddTransition(upperIdle, false);
            aimToidle.AddCondition(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0.25f, "Engaging");

            //Melee
            if(meleeAnimation)
                {
                    AnimatorState upperMelee = upperStateMachine.AddState("Melee");
                    upperMelee.motion = meleeAnimation;

                    var aimToMeleeTranistion = upperAim.AddTransition(upperMelee, false);
                    aimToMeleeTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Melee");

                    upperMelee.AddTransition(upperAim, true);
                }
            //Reload
            if(reloadAnimation)
                {
                    AnimatorState reloadAnim = upperStateMachine.AddState("Reload");
                    reloadAnim.motion = reloadAnimation;

                    var aimToReloadTranistion = upperAim.AddTransition(reloadAnim, false);
                    aimToReloadTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Reloading");

                    reloadAnim.AddTransition(upperAim, true);
                }
            //Fire
            if(fireAnimation)
                {
                    AnimatorState fireAnim = upperStateMachine.AddState("Fire");
                    fireAnim.motion = fireAnimation;

                    var aimToFireTranistion = upperAim.AddTransition(fireAnim, false);
                    aimToFireTranistion.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0.25f, "Fire");

                    fireAnim.AddTransition(upperAim, true);
                }
    }
Пример #21
0
 public static void AddChild(this BlendTree blendTree, Motion motion, Vector2 position, string parameter)
 {
     blendTree.AddChild(motion, position, 0.0f, parameter);
 }