Exemplo n.º 1
0
        protected static void DoAnimatorStateMachine(UnityEditor.Animations.AnimatorStateMachine stateMachine, AnimationClip defaultAnimationClip, Dictionary <string, AnimationClip> dic)
        {
            if (stateMachine == null)
            {
                return;
            }

            foreach (ChildAnimatorState childAnimatorState in stateMachine.states)
            {
                string        name = childAnimatorState.state.name;
                AnimationClip clip;
                if (dic.TryGetValue(name, out clip))
                {
                    childAnimatorState.state.motion = clip;
                }
                else
                {
                    childAnimatorState.state.motion = defaultAnimationClip;
                }
            }

            foreach (ChildAnimatorStateMachine childAnimatorStateMachine in stateMachine.stateMachines)
            {
                DoAnimatorStateMachine(childAnimatorStateMachine.stateMachine, defaultAnimationClip, dic);
            }
        }
        private List <AnimationClip> GetClipsFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine)
        {
            List <AnimationClip> list = new List <AnimationClip>();

            for (int i = 0; i != stateMachine.states.Length; ++i)
            {
                UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i];
                list.Add(state.state.motion as AnimationClip);
            }
            for (int i = 0; i != stateMachine.stateMachines.Length; ++i)
            {
                list.AddRange(GetClipsFromStatemachine(stateMachine.stateMachines[i].stateMachine));
            }

            var distinctClips = list.Select(q => (AnimationClip)q).Distinct().ToList();

            for (int i = 0; i < distinctClips.Count; i++)
            {
                if (distinctClips[i] && generateAnims.ContainsKey(distinctClips[i].name) == false)
                {
                    generateAnims.Add(distinctClips[i].name, true);
                }
            }
            return(list);
        }
        private static Dictionary <int, int> GetStatesFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine)
        {
            Dictionary <int, int> dict = new Dictionary <int, int>();

            for (int i = 0; i != stateMachine.states.Length; ++i)
            {
                UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i];
                if (state.state.motion is UnityEditor.Animations.BlendTree)
                {
                    //UnityEditor.Animations.BlendTree blendTree = state.state.motion as UnityEditor.Animations.BlendTree;
                    //ChildMotion[] childMotion = blendTree.children;
                    //for (int j = 0; j != childMotion.Length; ++j)
                    //{
                    //    list.Add(childMotion[j].motion as AnimationClip);
                    //}
                }
                else if (state.state.motion != null)
                {
                    AnimationClip clip = state.state.motion as AnimationClip;
                    dict.Add(state.state.nameHash, Animator.StringToHash(clip.name));
                }
            }
            for (int i = 0; i != stateMachine.stateMachines.Length; ++i)
            {
                Dictionary <int, int> childDict = GetStatesFromStatemachine(stateMachine.stateMachines[i].stateMachine);
                foreach (var d in childDict)
                {
                    dict.Add(d.Key, d.Value);
                }
            }
            // var distinctClips = list.Select(q => (AnimationClip)q).Distinct().ToList();

            return(dict);
        }
        private List<AnimationClip> GetClipsFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine)
        {
            List<AnimationClip> list = new List<AnimationClip>();
            for (int i = 0; i != stateMachine.states.Length; ++i)
            {
                UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i];
				if (state.state.motion is UnityEditor.Animations.BlendTree)
				{
					UnityEditor.Animations.BlendTree blendTree = state.state.motion as UnityEditor.Animations.BlendTree;
					ChildMotion[] childMotion = blendTree.children;
					for(int j = 0; j != childMotion.Length; ++j) 
					{
						list.Add(childMotion[j].motion as AnimationClip);
					}
				}
				else if (state.state.motion != null)
                	list.Add(state.state.motion as AnimationClip);
            }
            for (int i = 0; i != stateMachine.stateMachines.Length; ++i)
            {
                list.AddRange(GetClipsFromStatemachine(stateMachine.stateMachines[i].stateMachine));
            }

            var distinctClips = list.Select(q => (AnimationClip)q).Distinct().ToList();
            for (int i = 0; i < distinctClips.Count; i++)
            {
                if (distinctClips[i] && generateAnims.ContainsKey(distinctClips[i].name) == false)
                    generateAnims.Add(distinctClips[i].name, true);
            }
            return list;
        }
Exemplo n.º 5
0
        public static void PlayMotion(string path, string name, UnityEditor.Animations.AnimatorController ac)
        {
            UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[0].stateMachine;

            for (int i = 0; i < sm.states.Length; i++)
            {
                sm.RemoveState(sm.states[i].state);
            }
            addMotionToController(name, ac);
        }
Exemplo n.º 6
0
        private void InitController()
        {
            if (!this.m_Clip)
            {
                return;
            }

            if (!this.m_Clip.legacy && ((this.m_AvatarPreview != null) && (this.m_AvatarPreview.Animator != null)))
            {
                bool flag = true;
                if (this.m_Controller == null)
                {
                    this.m_Controller = new AnimatorController();
                    this.m_Controller.pushUndoX(false);
                    this.m_Controller.hideFlags = HideFlags.HideAndDontSave;
                    this.m_Controller.AddLayer("preview");
                    this.m_StateMachine = this.m_Controller.layers[0].stateMachine;
                    this.m_StateMachine.pushUndoX(false);
                    this.m_StateMachine.hideFlags = HideFlags.HideAndDontSave;
                    if (this.avatarMask != null)
                    {
                        AnimatorControllerLayer[] layers = this.m_Controller.layers;
                        layers[0].avatarMask     = this.avatarMask;
                        this.m_Controller.layers = layers;
                    }
                    flag = false;
                }
                if (this.m_State == null)
                {
                    this.m_State = this.m_StateMachine.AddState("preview");
                    this.m_State.pushUndoX(false);
                    AnimatorControllerLayer[] layers = this.m_Controller.layers;
                    this.m_State.motion      = this.m_Clip;
                    this.m_Controller.layers = layers;
                    this.m_State.iKOnFeet    = this.m_AvatarPreview.IKOnFeet;
                    this.m_State.hideFlags   = HideFlags.HideAndDontSave;
                    flag = false;
                }
                AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
                if (this.m_AvatarPreview.Animator.GetEffectiveAnimatorControllerX() != this.m_Controller)
                {
                    AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
                }
                if (!flag)
                {
                    this.m_AvatarPreview.Animator.Play(0, 0, 0f);
                    this.m_AvatarPreview.Animator.Update(0f);
                    if (this.m_FirstInitialization)
                    {
                        this.m_AvatarPreview.ResetPreviewFocus();
                        this.m_FirstInitialization = false;
                    }
                }
            }
        }
Exemplo n.º 7
0
 private void DestroyController()
 {
     if ((this.m_AvatarPreview != null) && (this.m_AvatarPreview.Animator != null))
     {
         AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, null);
     }
     UnityEngine.Object.DestroyImmediate(this.m_Controller);
     UnityEngine.Object.DestroyImmediate(this.m_State);
     this.m_Controller   = null;
     this.m_StateMachine = null;
     this.m_State        = null;
 }
Exemplo n.º 8
0
 public void SetPushUndo(UnityEditor.Animations.AnimatorStateMachine asm, bool flag)
 {
     if (asm == null)
     {
         return;
     }
     if (dg_set_pushUndo == null || dg_set_pushUndo.Target != (object)asm)
     {
         dg_set_pushUndo = (Action <bool>)Delegate.CreateDelegate(typeof(Action <bool>), asm, asm.GetType().GetProperty("pushUndo", BindingFlags.NonPublic | BindingFlags.Instance).GetSetMethod(true));
     }
     dg_set_pushUndo(flag);
 }
Exemplo n.º 9
0
    List <AnimationClip> GetAnimationLengths()
    {
        List <AnimationClip> animationClips = new List <AnimationClip>();

                #if UNITY_5_1
        RuntimeAnimatorController controller = transform.GetComponent <Animator>().runtimeAnimatorController;

        for (int i = 0; i < controller.animationClips.Length; i++)
        {
            AnimationClip clip = new AnimationClip();
            // Obviously loading it depends on where/how clip is stored, best case its a resource, worse case you have to search asset database.

            string path = AssetDatabase.GetAssetPath(controller.animationClips[i]);
            clip = (AnimationClip)AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip));

            //clip = (AnimationClip)Resources.LoadAssetAtPath(_puppet2DPath+"/Animation/" + m.GetState(i).GetMotion().name + ".anim", typeof(AnimationClip));
            animationClips.Add(clip);
        }

        return(animationClips);
                #else
        RuntimeAnimatorController controller = transform.GetComponent <Animator>().runtimeAnimatorController;
        if (controller is UnityEditor.Animations.AnimatorController)
        {
            UnityEditor.Animations.AnimatorStateMachine m = ((UnityEditor.Animations.AnimatorController)controller).GetLayer(0).stateMachine;

            for (int i = 0; i < m.stateCount; i++)

            {
                AnimationClip clip = new AnimationClip();
                // Obviously loading it depends on where/how clip is stored, best case its a resource, worse case you have to search asset database.
                if (m.GetState(i).GetMotion())
                {
                    string path = AssetDatabase.GetAssetPath(m.GetState(i).GetMotion());
                    clip = (AnimationClip)AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip));

                    //clip = (AnimationClip)Resources.LoadAssetAtPath(_puppet2DPath+"/Animation/" + m.GetState(i).GetMotion().name + ".anim", typeof(AnimationClip));
                    animationClips.Add(clip);
                }
                if (clip)

                {
                    Debug.Log(clip.name + ", length is " + clip.length);
                }
            }
        }
        return(animationClips);
                #endif
    }
    void Reset()
    {
        displayEvents = null;

        targetController   = null;
        targetStateMachine = null;
        targetState        = null;
        targetEvent        = null;

        selectedLayer = 0;
        selectedState = 0;
        selectedEvent = 0;

        MecanimEventEditorPopup.Destroy();
    }
Exemplo n.º 11
0
    private static void AddStateTransition(string path, UnityEditor.Animations.AnimatorControllerLayer layer, string motionName, Vector3 pos)
    {
        UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine;
        Motion motion = AssetDatabase.LoadAssetAtPath(path, typeof(Motion)) as Motion;          //根据动画文件读取它的Motion对象

        UnityEditor.Animations.AnimatorState state = sm.AddState(motionName, pos);              //motion.name

        state.motion = motion;                                                                  //取出动画名子 添加到state里面
        UnityEditor.Animations.AnimatorStateTransition trans = sm.AddAnyStateTransition(state); //把state添加在layer里面

        trans.hasExitTime = false;                                                              //把默认的时间条件删除 //false
        //trans.exitTime = 1f; //0.9f

        trans.duration            = 0f;   //默认过渡时间 // 0.1f
        trans.canTransitionToSelf = true; //默认true
    }
Exemplo n.º 12
0
    public override string[] GetOptions()
    {
        List <string> paramList = new List <string>();

        if (controller != null)
        {
            UnityEditor.Animations.AnimatorControllerLayer[] layers = controller.layers;
            //List<AnimatorStateInfo> states = new List<AnimatorStateInfo>();
            foreach (UnityEditor.Animations.AnimatorControllerLayer layer in layers)
            {
                UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine as UnityEditor.Animations.AnimatorStateMachine;
                GetAllStates(sm, layer.name, paramList);
            }
        }
        return(paramList.ToArray());
    }
Exemplo n.º 13
0
 static void FindAllAniInControl(UnityEditor.Animations.AnimatorStateMachine machine, List <AnimationClip> list)
 {
     for (int i = 0; i < machine.states.Length; i++)
     {
         var m = machine.states[i].state.motion;
         if (list.Contains(m as AnimationClip) == false)
         {
             list.Add(m as AnimationClip);
         }
     }
     for (int i = 0; i < machine.stateMachines.Length; i++)
     {
         var m = machine.stateMachines[i].stateMachine;
         FindAllAniInControl(m, list);
     }
 }
Exemplo n.º 14
0
    public void setLevel(int level, RuntimeAnimatorController animatorController, RuntimeAnimatorController animatorControllerClone)
    {
        LevelGK levelGK = getLevelGK(level);

        if (levelGK == null)
        {
            return;
        }

        _flyDistance = levelGK._flyDistance;
        _moveSpeed   = levelGK._moveSpeed;
        _responsive  = levelGK._responsive;



        _animatorController      = animatorController;
        _animatorControllerClone = animatorControllerClone;


        UnityEditor.Animations.AnimatorController   ac = _animatorController as UnityEditor.Animations.AnimatorController;
        UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[0].stateMachine;
        for (int i = 0; i < sm.states.Length; i++)
        {
            UnityEditor.Animations.AnimatorState state = sm.states[i].state;
            //			Debug.Log(string.Format("State: {0}", state.uniqueName));
            if (state.name.Equals("save") || state.name.Equals("save center"))
            {
                state.speed = _responsive;
            }
            else if (state.name.Equals("move left") || state.name.Equals("move right"))
            {
                state.speed = _moveSpeed;
            }
        }

        ac = _animatorControllerClone as UnityEditor.Animations.AnimatorController;
        sm = ac.layers[0].stateMachine;
        for (int i = 0; i < sm.states.Length; i++)
        {
            UnityEditor.Animations.AnimatorState state = sm.states[i].state;
            //			Debug.Log(string.Format("State: {0}", state.uniqueName));
            if (state.name.Equals("save") || state.name.Equals("save center"))
            {
                state.speed = _responsive;
            }
        }
    }
        public static AnimatorState addNoExistState(AnimatorControllerLayer layer, AnimationClip newClip, bool autoCreate = true)
        {
            if (newClip == null || layer == null)
            {
                return(null);
            }

            UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine;
            AnimatorState state = getExistState(sm, newClip.name);

            if (state == null)
            {
                state = sm.AddState(newClip.name);
            }
            state.motion = newClip;
            return(state);
        }
Exemplo n.º 16
0
    private void traverseFillStateDict(int depth, string path, UnityEditor.Animations.AnimatorStateMachine stateMachine)
    {
        foreach (var sta in stateMachine.stateMachines)
        {
            //Debug.Log("Depth=" + depth + " stateMachines " + sta.stateMachine);
            string newPath = path;
            newPath = newPath + sta.stateMachine.name + "->";
            traverseFillStateDict(depth + 1, newPath, sta.stateMachine);
        }

        foreach (UnityEditor.Animations.ChildAnimatorState sta in stateMachine.states)
        {
            if (sta.state != null)
            {
                stateDict[sta.state.name] = sta.state;
            }
        }
    }
Exemplo n.º 17
0
    private static UnityEditor.Animations.AnimatorState CreateState(UnityEditor.Animations.AnimatorStateMachine machine, string name, AnimationClip anim, Vector3 pos)
    {
        foreach (var item in machine.states)
        {
            if (item.state.name == name)
            {
                return(item.state);
            }
        }
        UnityEditor.Animations.AnimatorState state = machine.AddState(name);
        state.motion = anim;
        //state.SetAnimationClip(anim);
        var state1 = ArrayUtility.Find <UnityEditor.Animations.ChildAnimatorState>(machine.states, x => x.state == state);

        state1.position = pos;
        //state.position = pos;
        return(state);
    }
Exemplo n.º 18
0
        public AnimatorStateMachine AddStateMachine(string name, Vector3 position)
        {
            AnimatorStateMachine stateMachine = new AnimatorStateMachine();

            stateMachine.hideFlags = HideFlags.HideInHierarchy;
            stateMachine.name      = MakeUniqueStateMachineName(name);

            AddStateMachine(stateMachine, position);

            if (AssetDatabase.GetAssetPath(this) != "")
            {
                AssetDatabase.AddObjectToAsset(stateMachine, AssetDatabase.GetAssetPath(this));
            }

            undoHandler.DoUndoCreated(stateMachine, "StateMachine Created");

            return(stateMachine);
        }
Exemplo n.º 19
0
        /// <summary>
        ///   <para>Utility function to add an outgoing transition from the source state machine to the destination.</para>
        /// </summary>
        /// <param name="sourceStateMachine">The source state machine.</param>
        /// <param name="destinationStateMachine">The destination state machine.</param>
        /// <param name="destinationState">The destination state.</param>
        /// <returns>
        ///   <para>The Animations.AnimatorTransition transition that was created.</para>
        /// </returns>
        public AnimatorTransition AddStateMachineTransition(AnimatorStateMachine sourceStateMachine, AnimatorStateMachine destinationStateMachine)
        {
            this.undoHandler.DoUndo((UnityEngine.Object) this, "StateMachine Transition Added");
            AnimatorTransition[] machineTransitions = this.GetStateMachineTransitions(sourceStateMachine);
            AnimatorTransition   animatorTransition = new AnimatorTransition();

            if ((bool)((UnityEngine.Object)destinationStateMachine))
            {
                animatorTransition.destinationStateMachine = destinationStateMachine;
            }
            if (AssetDatabase.GetAssetPath((UnityEngine.Object) this) != string.Empty)
            {
                AssetDatabase.AddObjectToAsset((UnityEngine.Object)animatorTransition, AssetDatabase.GetAssetPath((UnityEngine.Object) this));
            }
            animatorTransition.hideFlags = HideFlags.HideInHierarchy;
            ArrayUtility.Add <AnimatorTransition>(ref machineTransitions, animatorTransition);
            this.SetStateMachineTransitions(sourceStateMachine, machineTransitions);
            return(animatorTransition);
        }
Exemplo n.º 20
0
        public AnimatorTransition AddStateMachineTransition(AnimatorStateMachine sourceStateMachine, AnimatorStateMachine destinationStateMachine)
        {
            this.undoHandler.DoUndo(this, "StateMachine Transition Added");
            AnimatorTransition[] stateMachineTransitions = this.GetStateMachineTransitions(sourceStateMachine);
            AnimatorTransition   objectToAdd             = new AnimatorTransition();

            if (destinationStateMachine != null)
            {
                objectToAdd.destinationStateMachine = destinationStateMachine;
            }
            if (AssetDatabase.GetAssetPath(this) != string.Empty)
            {
                AssetDatabase.AddObjectToAsset(objectToAdd, AssetDatabase.GetAssetPath(this));
            }
            objectToAdd.hideFlags = HideFlags.HideInHierarchy;
            ArrayUtility.Add <AnimatorTransition>(ref stateMachineTransitions, objectToAdd);
            this.SetStateMachineTransitions(sourceStateMachine, stateMachineTransitions);
            return(objectToAdd);
        }
Exemplo n.º 21
0
        public AnimatorTransition AddStateMachineTransition(AnimatorStateMachine sourceStateMachine, AnimatorStateMachine destinationStateMachine)
        {
            this.undoHandler.DoUndo(this, "StateMachine Transition Added");
            AnimatorTransition[] stateMachineTransitions = this.GetStateMachineTransitions(sourceStateMachine);
            AnimatorTransition   animatorTransition      = new AnimatorTransition();

            if (destinationStateMachine)
            {
                animatorTransition.destinationStateMachine = destinationStateMachine;
            }
            if (AssetDatabase.GetAssetPath(this) != "")
            {
                AssetDatabase.AddObjectToAsset(animatorTransition, AssetDatabase.GetAssetPath(this));
            }
            animatorTransition.hideFlags = HideFlags.HideInHierarchy;
            ArrayUtility.Add <AnimatorTransition>(ref stateMachineTransitions, animatorTransition);
            this.SetStateMachineTransitions(sourceStateMachine, stateMachineTransitions);
            return(animatorTransition);
        }
Exemplo n.º 22
0
        public static bool StateMachineRelativePath(AnimatorStateMachine parent, AnimatorStateMachine toFind,
                                                    ref List <AnimatorStateMachine> hierarchy)
        {
            hierarchy.Add(parent);
            if (parent == toFind)
            {
                return(true);
            }
            var childStateMachines = AnimatorStateMachine.StateMachineCache.GetChildStateMachines(parent);

            for (int i = 0; i < childStateMachines.Length; i++)
            {
                if (StateMachineRelativePath(childStateMachines[i].stateMachine, toFind, ref hierarchy))
                {
                    return(true);
                }
            }
            hierarchy.Remove(parent);
            return(false);
        }
Exemplo n.º 23
0
    private void AddAllStates(UnityEditor.Animations.AnimatorStateMachine stateMachine, string substate = "")
    {
        foreach (var state in stateMachine.states)
        {
            if (state.state.motion is AnimationClip)
            {
                animationClips.Add(new AnimationGroup()
                {
                    substate = substate, name = state.state.name, animationClip = state.state.motion as AnimationClip
                });
            }
            else if (state.state.motion is UnityEditor.Animations.BlendTree)
            {
                AddBlendTreeClips(state.state.motion as UnityEditor.Animations.BlendTree, state.state.name, substate);
            }
        }

        foreach (var childStateMachine in stateMachine.stateMachines)
        {
            AddAllStates(childStateMachine.stateMachine, childStateMachine.stateMachine.name);
        }
    }
        private static AnimatorState getExistState(UnityEditor.Animations.AnimatorStateMachine stateMachine, string name)
        {
            AnimatorState state = null;

            foreach (ChildAnimatorState animatorState in stateMachine.states)
            {
                if (animatorState.state.name == name)
                {
                    state = animatorState.state;
                    break;
                }
            }
            if (state == null)
            {
                foreach (ChildAnimatorStateMachine childAnimatorStateMachine in stateMachine.stateMachines)
                {
                    state = getExistState(childAnimatorStateMachine.stateMachine, name);
                    break;
                }
            }
            return(state);
        }
Exemplo n.º 25
0
    private static void AddTransition(UnityEditor.Animations.AnimatorStateMachine machine, UnityEditor.Animations.AnimatorState from, UnityEditor.Animations.AnimatorState to, bool removedef, params object[] conditions)
    {
        UnityEditor.Animations.AnimatorStateTransition transition = null;
        transition = from == null?machine.AddAnyStateTransition(to) : from.AddTransition(to);

        //Unity5中默认是没有condition的
        //if (removedef) transition.RemoveCondition(0);

        transition.offset      = 0.0f;
        transition.duration    = 0.0f;
        transition.hasExitTime = false;

        for (var i = 0; i < conditions.Length;)
        {
            if (conditions.Length - i < 2)
            {
                break;
            }

            //var cond  = transition.AddCondition();
            var mode  = (UnityEditor.Animations.AnimatorConditionMode)conditions[i++];
            var param = conditions[i++].ToString();

            //cond.mode      = mode;
            //cond.parameter = param;

            float threshold = 0f;
            if (i >= conditions.Length)
            {
                break;
            }
            if (mode != UnityEditor.Animations.AnimatorConditionMode.If && mode != UnityEditor.Animations.AnimatorConditionMode.IfNot)
            {
                threshold = (int)conditions[i++];
            }

            transition.AddCondition(mode, threshold, param);
        }
    }
Exemplo n.º 26
0
        public AnimatorTransition AddStateMachineTransition(AnimatorStateMachine sourceStateMachine, AnimatorStateMachine destinationStateMachine)
        {
            undoHandler.DoUndo(this, "StateMachine Transition Added");

            AnimatorTransition[] transitionsVector = GetStateMachineTransitions(sourceStateMachine);
            AnimatorTransition   newTransition     = new AnimatorTransition();

            if (destinationStateMachine)
            {
                newTransition.destinationStateMachine = destinationStateMachine;
            }

            if (AssetDatabase.GetAssetPath(this) != "")
            {
                AssetDatabase.AddObjectToAsset(newTransition, AssetDatabase.GetAssetPath(this));
            }

            newTransition.hideFlags = HideFlags.HideInHierarchy;
            ArrayUtility.Add(ref transitionsVector, newTransition);
            SetStateMachineTransitions(sourceStateMachine, transitionsVector);

            return(newTransition);
        }
Exemplo n.º 27
0
    private void FillAnimatorState(int depth, UnityEditor.Animations.AnimatorStateMachine stateMachine)
    {
        foreach (var sta in stateMachine.stateMachines)
        {
            Debug.Log("Depth=" + depth + " stateMachines " + sta.stateMachine);
            FillAnimatorState(depth + 1, sta.stateMachine);
        }

        foreach (UnityEditor.Animations.ChildAnimatorState sta in stateMachine.states)
        {
            UnityEditor.Animations.BlendTree blendTree = sta.state.motion as UnityEditor.Animations.BlendTree;
            if (blendTree != null)
            {
                Debug.LogError("Depth=" + depth + " blendTree need to be refined" + sta.state.motion);
                int mCount = blendTree.children.Length;

                //foreach (var cmotion in blendTree.children)
                for (int i = 0; i < mCount; i++)
                {
                    Debug.Log("Blend Clip " + blendTree.children[i].motion);
                    Vector2 position = blendTree.children[i].position;
                    blendTree.AddChild(emptyClip, position);
                }
                for (int i = 0; i < mCount; i++)
                {
                    blendTree.RemoveChild(0);
                }
                sta.state.motion = blendTree;
            }
            else
            {
                Debug.Log("Depth=" + depth + " Clip " + sta.state.motion);
                sta.state.motion = emptyClip;
            }
        }
    }
        private static List <AnimClipData> GetClipsFromStatemachine(UnityEditor.Animations.AnimatorStateMachine stateMachine)
        {
            List <AnimClipData> list = new List <AnimClipData>();

            for (int i = 0; i != stateMachine.states.Length; ++i)
            {
                UnityEditor.Animations.ChildAnimatorState state = stateMachine.states[i];
                if (state.state.motion is UnityEditor.Animations.BlendTree)
                {
                    UnityEditor.Animations.BlendTree blendTree = state.state.motion as UnityEditor.Animations.BlendTree;
                    ChildMotion[] childMotion = blendTree.children;
                    for (int j = 0; j != childMotion.Length; ++j)
                    {
                        AnimClipData clipData = new AnimClipData();
                        clipData.clip  = childMotion[j].motion as AnimationClip;
                        clipData.speed = 1;
                        list.Add(clipData);
                    }
                }
                else if (state.state.motion != null)
                {
                    AnimClipData clipData = new AnimClipData();
                    clipData.clip  = state.state.motion as AnimationClip;
                    clipData.speed = state.state.speed;
                    list.Add(clipData);
                }
            }
            for (int i = 0; i != stateMachine.stateMachines.Length; ++i)
            {
                list.AddRange(GetClipsFromStatemachine(stateMachine.stateMachines[i].stateMachine));
            }

            list = list.Select(q => (AnimClipData)q).Distinct().ToList();

            return(list);
        }
Exemplo n.º 29
0
        public static bool StateMachineRelativePath(AnimatorStateMachine parent, AnimatorStateMachine toFind, ref List <AnimatorStateMachine> hierarchy)
        {
            hierarchy.Add(parent);
            bool result;

            if (parent == toFind)
            {
                result = true;
            }
            else
            {
                for (int i = 0; i < parent.stateMachines.Length; i++)
                {
                    if (MecanimUtilities.StateMachineRelativePath(parent.stateMachines[i].stateMachine, toFind, ref hierarchy))
                    {
                        result = true;
                        return(result);
                    }
                }
                hierarchy.Remove(parent);
                result = false;
            }
            return(result);
        }
Exemplo n.º 30
0
    void CheckResources()
    {
        ActiveTextures.Clear();
        ActiveMaterials.Clear();
        ActiveMeshDetails.Clear();

        Renderer[] renderers = FindObjects <Renderer>();

        //Debug.Log("Total renderers "+renderers.Length);
        foreach (Renderer renderer in renderers)
        {
            //Debug.Log("Renderer is "+renderer.name);
            foreach (Material material in renderer.sharedMaterials)
            {
                MaterialDetails tMaterialDetails = FindMaterialDetails(material);
                if (tMaterialDetails == null)
                {
                    tMaterialDetails          = new MaterialDetails();
                    tMaterialDetails.material = material;
                    ActiveMaterials.Add(tMaterialDetails);
                }
                tMaterialDetails.FoundInRenderers.Add(renderer);
            }

            if (renderer is SpriteRenderer)
            {
                SpriteRenderer tSpriteRenderer = (SpriteRenderer)renderer;

                if (tSpriteRenderer.sprite != null)
                {
                    var tSpriteTextureDetail = GetTextureDetail(tSpriteRenderer.sprite.texture, renderer);
                    if (!ActiveTextures.Contains(tSpriteTextureDetail))
                    {
                        ActiveTextures.Add(tSpriteTextureDetail);
                    }
                }
            }
        }

        if (IncludeGuiElements)
        {
            Graphic[] graphics = FindObjects <Graphic>();

            foreach (Graphic graphic in graphics)
            {
                if (graphic.mainTexture)
                {
                    var tSpriteTextureDetail = GetTextureDetail(graphic.mainTexture, graphic);
                    if (!ActiveTextures.Contains(tSpriteTextureDetail))
                    {
                        ActiveTextures.Add(tSpriteTextureDetail);
                    }
                }

                if (graphic.materialForRendering)
                {
                    MaterialDetails tMaterialDetails = FindMaterialDetails(graphic.materialForRendering);
                    if (tMaterialDetails == null)
                    {
                        tMaterialDetails          = new MaterialDetails();
                        tMaterialDetails.material = graphic.materialForRendering;
                        ActiveMaterials.Add(tMaterialDetails);
                    }
                    tMaterialDetails.FoundInGraphics.Add(graphic);
                }
            }
        }

        foreach (MaterialDetails tMaterialDetails in ActiveMaterials)
        {
            Material tMaterial = tMaterialDetails.material;
            if (tMaterial != null)
            {
                var dependencies = EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial });
                foreach (Object obj in dependencies)
                {
                    if (obj is Texture)
                    {
                        Texture tTexture       = obj as Texture;
                        var     tTextureDetail = GetTextureDetail(tTexture, tMaterial, tMaterialDetails);
                        ActiveTextures.Add(tTextureDetail);
                    }
                }

                //if the texture was downloaded, it won't be included in the editor dependencies
                if (tMaterial.mainTexture != null && !dependencies.Contains(tMaterial.mainTexture))
                {
                    var tTextureDetail = GetTextureDetail(tMaterial.mainTexture, tMaterial, tMaterialDetails);
                    ActiveTextures.Add(tTextureDetail);
                }
            }
        }


        MeshFilter[] meshFilters = FindObjects <MeshFilter>();

        foreach (MeshFilter tMeshFilter in meshFilters)
        {
            Mesh tMesh = tMeshFilter.sharedMesh;
            if (tMesh != null)
            {
                MeshDetails tMeshDetails = FindMeshDetails(tMesh);
                if (tMeshDetails == null)
                {
                    tMeshDetails      = new MeshDetails();
                    tMeshDetails.mesh = tMesh;
                    ActiveMeshDetails.Add(tMeshDetails);
                }
                tMeshDetails.FoundInMeshFilters.Add(tMeshFilter);
            }
        }

        SkinnedMeshRenderer[] skinnedMeshRenderers = FindObjects <SkinnedMeshRenderer>();

        foreach (SkinnedMeshRenderer tSkinnedMeshRenderer in skinnedMeshRenderers)
        {
            Mesh tMesh = tSkinnedMeshRenderer.sharedMesh;
            if (tMesh != null)
            {
                MeshDetails tMeshDetails = FindMeshDetails(tMesh);
                if (tMeshDetails == null)
                {
                    tMeshDetails      = new MeshDetails();
                    tMeshDetails.mesh = tMesh;
                    ActiveMeshDetails.Add(tMeshDetails);
                }
                tMeshDetails.FoundInSkinnedMeshRenderer.Add(tSkinnedMeshRenderer);
            }
        }

        if (IncludeSpriteAnimations)
        {
            Animator[] animators = FindObjects <Animator>();
            foreach (Animator anim in animators)
            {
                                #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3
                UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
                                #elif UNITY_5
                UnityEditor.Animations.AnimatorController ac = anim.runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
                                #endif

                //Skip animators without layers, this can happen if they don't have an animator controller.
                if (!ac || ac.layers == null || ac.layers.Length == 0)
                {
                    continue;
                }

                for (int x = 0; x < anim.layerCount; x++)
                {
                                        #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3
                    UnityEditorInternal.StateMachine sm = ac.GetLayer(x).stateMachine;
                    int cnt = sm.stateCount;
                                        #elif UNITY_5
                    UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[x].stateMachine;
                    int cnt = sm.states.Length;
                                        #endif

                    for (int i = 0; i < cnt; i++)
                    {
                                                #if UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3
                        UnityEditorInternal.State state = sm.GetState(i);
                        Motion m = state.GetMotion();
                                                #elif UNITY_5
                        UnityEditor.Animations.AnimatorState state = sm.states[i].state;
                        Motion m = state.motion;
                                                #endif
                        if (m != null)
                        {
                            AnimationClip clip = m as AnimationClip;

                            EditorCurveBinding[] ecbs = AnimationUtility.GetObjectReferenceCurveBindings(clip);

                            foreach (EditorCurveBinding ecb in ecbs)
                            {
                                if (ecb.propertyName == "m_Sprite")
                                {
                                    foreach (ObjectReferenceKeyframe keyframe in AnimationUtility.GetObjectReferenceCurve(clip, ecb))
                                    {
                                        Sprite tSprite = keyframe.value as Sprite;

                                        if (tSprite != null)
                                        {
                                            var tTextureDetail = GetTextureDetail(tSprite.texture, anim);
                                            if (!ActiveTextures.Contains(tTextureDetail))
                                            {
                                                ActiveTextures.Add(tTextureDetail);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (IncludeScriptReferences)
        {
            MonoBehaviour[] scripts = FindObjects <MonoBehaviour>();
            foreach (MonoBehaviour script in scripts)
            {
                BindingFlags flags  = BindingFlags.Public | BindingFlags.Instance;                // only public non-static fields are bound to by Unity.
                FieldInfo[]  fields = script.GetType().GetFields(flags);

                foreach (FieldInfo field in fields)
                {
                    System.Type fieldType = field.FieldType;
                    if (fieldType == typeof(Sprite))
                    {
                        Sprite tSprite = field.GetValue(script) as Sprite;
                        if (tSprite != null)
                        {
                            var tSpriteTextureDetail = GetTextureDetail(tSprite.texture, script);
                            if (!ActiveTextures.Contains(tSpriteTextureDetail))
                            {
                                ActiveTextures.Add(tSpriteTextureDetail);
                            }
                        }
                    }
                }
            }
        }

        TotalTextureMemory = 0;
        foreach (TextureDetails tTextureDetails in ActiveTextures)
        {
            TotalTextureMemory += tTextureDetails.memSizeKB;
        }

        TotalMeshVertices = 0;
        foreach (MeshDetails tMeshDetails in ActiveMeshDetails)
        {
            TotalMeshVertices += tMeshDetails.mesh.vertexCount;
        }

        // Sort by size, descending
        ActiveTextures.Sort(delegate(TextureDetails details1, TextureDetails details2) { return(details2.memSizeKB - details1.memSizeKB); });
        ActiveMeshDetails.Sort(delegate(MeshDetails details1, MeshDetails details2) { return(details2.mesh.vertexCount - details1.mesh.vertexCount); });

        collectedInPlayingMode = Application.isPlaying;
    }