/// <summary>
        /// Initialise this animation bridge.
        /// </summary>
        protected void Init()
        {
            // Get character reference
            myCharacter = (IMob)gameObject.GetComponent(typeof(IMob));
            if (myCharacter == null)
            {
                myCharacter = (IMob)gameObject.GetComponentInParent(typeof(IMob));
            }
            if (myCharacter == null)
            {
                Debug.LogError("Mecanim Animation Bridge (2D) unable to find Character or Enemy reference");
            }
            myCharacter.ChangeAnimationState += AnimationStateChanged;
            myAnimator        = GetComponentInChildren <Animator>();
            defaultController = myAnimator.runtimeAnimatorController;
            if (myAnimator == null)
            {
                Debug.LogError("Platform Animator unable to find Unity Animator reference");
            }

            animationStateOverrideLookup = new Dictionary <string, AnimatorOverrideController> ();
            foreach (AnimatorControllerMapping mapping in mappings)
            {
                animationStateOverrideLookup.Add(mapping.overrrideState, mapping.controller);
            }

            queuedStates     = new Queue <string> ();
            queuedPriorities = new Queue <int> ();
            state            = AnimationState.NONE.AsString();
            priority         = -1;

            TimeManager.Instance.GamePaused   += HandleGamePaused;
            TimeManager.Instance.GameUnPaused += HandleGameUnPaused;

#if UNITY_EDITOR
        #if UNITY_5_3_OR_NEWER
            // In editor mode build a list of handled states for error messaging and the like
            if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
            {
                editor_stateNames = new List <string>();
                UnityEditor.Animations.AnimatorStateMachine stateMachine = ((UnityEditor.Animations.AnimatorController)defaultController).layers[0].stateMachine;
                for (int i = 0; i < stateMachine.states.Length; i++)
                {
                    editor_stateNames.Add(stateMachine.states[i].state.name);
                }
            }
        #else
            // In editor mode build a list of handled states for error messaging and the like
            if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
            {
                editor_stateNames = new List <string>();
                UnityEditorInternal.StateMachine stateMachine = ((UnityEditorInternal.AnimatorController)defaultController).GetLayer(0).stateMachine;
                for (int i = 0; i < stateMachine.stateCount; i++)
                {
                    editor_stateNames.Add(stateMachine.GetState(i).name);
                }
            }
        #endif
#endif
        }
Ejemplo n.º 2
0
        List <AnimationClip> GetAnimationLengths()
        {
            List <AnimationClip> animationClips = new List <AnimationClip>();

#if (!UNITY_4_3 && !UNITY_4_4 && !UNITY_4_5 && !UNITY_4_6)
            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 UnityEditorInternal.AnimatorController)
            {
                UnityEditorInternal.StateMachine m = ((UnityEditorInternal.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)Resources.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
        }
        /// <summary>
        /// Init this instance.
        /// </summary>
        virtual protected void Init()
        {
            // Get character reference
            myCharacter = (IMob)gameObject.GetComponent(typeof(IMob));
            if (myCharacter == null)
            {
                myCharacter = (IMob)gameObject.GetComponentInParent(typeof(IMob));
            }
            if (myCharacter == null)
            {
                Debug.LogError("Mecanim Animation Bridge (3D) unable to find Character or Enemy reference");
            }

            if (myCharacter != null)
            {
                // Events
                myCharacter.ChangeAnimationState += AnimationStateChanged;
                if (myCharacter is Character)
                {
                    ((Character)myCharacter).Respawned += HandleRespawned;
                }

                myAnimator = GetComponentInChildren <Animator>();
                if (myAnimator == null)
                {
                    Debug.LogError("Platform Animator unable to find Unity Animator reference");
                }
                defaultController = myAnimator.runtimeAnimatorController;

                // Get an aimer if one is present
                ProjectileAimer tmpAimer = ((Component)myCharacter).gameObject.GetComponent <ProjectileAimer> ();
                if (tmpAimer != null && myCharacter is Character && (tmpAimer.aimType == ProjectileAimingTypes.EIGHT_WAY || tmpAimer.aimType == ProjectileAimingTypes.SIX_WAY))
                {
                    aimer = tmpAimer;
                }

                // Set up animation overrides
                animationStateOverrideLookup = new Dictionary <string, AnimatorOverrideController> ();
                foreach (AnimatorControllerMapping mapping in mappings)
                {
                    animationStateOverrideLookup.Add(mapping.overrrideState, mapping.controller);
                }
            }
#if UNITY_EDITOR
        #if UNITY_5
            // In editor mode build a list of handled states for error messaging and the like
            if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
            {
                editor_stateNames = new List <string>();
                UnityEditor.Animations.AnimatorStateMachine stateMachine = ((UnityEditor.Animations.AnimatorController)defaultController).layers[0].stateMachine;
                for (int i = 0; i < stateMachine.states.Length; i++)
                {
                    editor_stateNames.Add(stateMachine.states[i].state.name);
                }
                if (!editor_stateNames.Contains("IDLE"))
                {
                    Debug.LogWarning("Its recommended that you have a default state called 'IDLE' which can act as a hard reset state for actions like respawning");
                }
            }
        #else
            // In editor mode build a list of handled states for error messaging and the like
            if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
            {
                editor_stateNames = new List <string>();
                UnityEditorInternal.StateMachine stateMachine = ((UnityEditorInternal.AnimatorController)defaultController).GetLayer(0).stateMachine;
                for (int i = 0; i < stateMachine.stateCount; i++)
                {
                    editor_stateNames.Add(stateMachine.GetState(i).name);
                }
                if (!editor_stateNames.Contains("IDLE"))
                {
                    Debug.LogWarning("Its recommended that you have a default state called 'IDLE' which can act as a hard reset state for actions like respawning");
                }
            }
        #endif
#endif
        }
        void CheckResources()
        {
            ActiveTextures.Clear();
            ActiveMaterials.Clear();
            ActiveMeshDetails.Clear();
            MissingObjects.Clear();
            thingsMissing = false;

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

            MaterialDetails skyMat = new MaterialDetails();

            skyMat.material = RenderSettings.skybox;
            skyMat.isSky    = true;
            ActiveMaterials.Add(skyMat);

            //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);
                        }
                    }
                    else if (tSpriteRenderer.sprite == null)
                    {
                        MissingGraphic tMissing = new MissingGraphic();
                        tMissing.Object = tSpriteRenderer.transform;
                        tMissing.type   = "sprite";
                        tMissing.name   = tSpriteRenderer.transform.name;
                        MissingObjects.Add(tMissing);
                        thingsMissing = true;
                    }
                }
            }

            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;
                            tMaterialDetails.isgui    = true;
                            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);
                            tTextureDetail.isSky    = tMaterialDetails.isSky;
                            tTextureDetail.instance = tMaterialDetails.instance;
                            tTextureDetail.isgui    = tMaterialDetails.isgui;
                            ActiveTextures.Add(tTextureDetail);
                        }
                    }

                    //if the texture was downloaded, it won't be included in the editor dependencies
                    if (tMaterial.HasProperty("_MainTex"))
                    {
                        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);
                }
                else if (tMesh == null && tMeshFilter.transform.GetComponent("TextContainer") == null)
                {
                    MissingGraphic tMissing = new MissingGraphic();
                    tMissing.Object = tMeshFilter.transform;
                    tMissing.type   = "mesh";
                    tMissing.name   = tMeshFilter.transform.name;
                    MissingObjects.Add(tMissing);
                    thingsMissing = true;
                }

                var meshRenderrer = tMeshFilter.transform.GetComponent <MeshRenderer>();

                if (meshRenderrer == null || meshRenderrer.sharedMaterial == null)
                {
                    MissingGraphic tMissing = new MissingGraphic();
                    tMissing.Object = tMeshFilter.transform;
                    tMissing.type   = "material";
                    tMissing.name   = tMeshFilter.transform.name;
                    MissingObjects.Add(tMissing);
                    thingsMissing = true;
                }
            }

            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);
                }
                else if (tMesh == null)
                {
                    MissingGraphic tMissing = new MissingGraphic();
                    tMissing.Object = tSkinnedMeshRenderer.transform;
                    tMissing.type   = "mesh";
                    tMissing.name   = tSkinnedMeshRenderer.transform.name;
                    MissingObjects.Add(tMissing);
                    thingsMissing = true;
                }
                if (tSkinnedMeshRenderer.sharedMaterial == null)
                {
                    MissingGraphic tMissing = new MissingGraphic();
                    tMissing.Object = tSkinnedMeshRenderer.transform;
                    tMissing.type   = "material";
                    tMissing.name   = tSkinnedMeshRenderer.transform.name;
                    MissingObjects.Add(tMissing);
                    thingsMissing = true;
                }
            }

            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 || UNITY_5_3_OR_NEWER
                    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 || UNITY_5_3_OR_NEWER
                        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 || UNITY_5_3_OR_NEWER
                            UnityEditor.Animations.AnimatorState state = sm.states[i].state;
                            Motion m = state.motion;
#endif
                            if (m != null)
                            {
                                AnimationClip clip = m as AnimationClip;

                                if (clip != null)
                                {
                                    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);
                                }
                            }
                        }
                        if (fieldType == typeof(Mesh))
                        {
                            Mesh tMesh = field.GetValue(script) as Mesh;
                            if (tMesh != null)
                            {
                                MeshDetails tMeshDetails = FindMeshDetails(tMesh);
                                if (tMeshDetails == null)
                                {
                                    tMeshDetails          = new MeshDetails();
                                    tMeshDetails.mesh     = tMesh;
                                    tMeshDetails.instance = true;
                                    ActiveMeshDetails.Add(tMeshDetails);
                                }
                            }
                        }
                        if (fieldType == typeof(Material))
                        {
                            Material tMaterial = field.GetValue(script) as Material;
                            if (tMaterial != null)
                            {
                                MaterialDetails tMatDetails = FindMaterialDetails(tMaterial);
                                if (tMatDetails == null)
                                {
                                    tMatDetails          = new MaterialDetails();
                                    tMatDetails.instance = true;
                                    tMatDetails.material = tMaterial;
                                    if (!ActiveMaterials.Contains(tMatDetails))
                                    {
                                        ActiveMaterials.Add(tMatDetails);
                                    }
                                }
                                if (tMaterial.mainTexture)
                                {
                                    var tSpriteTextureDetail = GetTextureDetail(tMaterial.mainTexture);
                                    if (!ActiveTextures.Contains(tSpriteTextureDetail))
                                    {
                                        ActiveTextures.Add(tSpriteTextureDetail);
                                    }
                                }
                                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, tMatDetails);
                                        if (!ActiveTextures.Contains(tTextureDetail))
                                        {
                                            ActiveTextures.Add(tTextureDetail);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            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); });
            ActiveTextures = ActiveTextures.Distinct().ToList();
            ActiveMeshDetails.Sort(delegate(MeshDetails details1, MeshDetails details2) { return(details2.mesh.vertexCount - details1.mesh.vertexCount); });

            collectedInPlayingMode = Application.isPlaying;
        }