示例#1
0
        static void FixImportSettings(GameObject gameObject)
        {
            MeshRenderer[] renderers = gameObject.GetComponentsInChildren <MeshRenderer>();
            MeshFilter[]   filters   = gameObject.GetComponentsInChildren <MeshFilter>();

            List <Texture> allTextures = new List <Texture>();

            foreach (MeshRenderer renderer in renderers)
            {
                Material[] sharedMaterials = renderer.sharedMaterials;

                foreach (Material m in sharedMaterials)
                {
                    if (m == null)
                    {
                        continue;
                    }
                    //
                    Texture[] textures = MaterialConverter.GetValidTexturesForMaterial(m);
                    allTextures.AddRange(textures);
                }
            }

            List <Mesh> allMeshes = new List <Mesh>();

            foreach (MeshFilter filter in filters)
            {
                if (filter.sharedMesh != null)
                {
                    allMeshes.Add(filter.sharedMesh);
                }
            }

            EditorUtility.DisplayProgressBar("Fixing Textures", "", 0.0f);
            int progress = 0;

            string report = string.Empty;

            foreach (Texture tex in allTextures)
            {
                TextureConverter.TextureSupportIssues issues = TextureConverter.GetSupportIssuesForTexture(tex);
                if (issues != TextureConverter.TextureSupportIssues.None && (issues & TextureConverter.TextureSupportIssues.Dimensions) != TextureConverter.TextureSupportIssues.Dimensions)
                {
                    report += TextureConverter.GetTextureSupportReport(issues, tex);

                    EditorUtility.DisplayProgressBar("Fixing Textures", "Processing " + tex.name, (float)((float)progress / (float)allTextures.Count));

                    string          path     = AssetDatabase.GetAssetPath(tex);
                    TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path);
                    if (importer == null)
                    {
                        continue;
                    }

                    if ((issues & TextureConverter.TextureSupportIssues.ReadWrite) == TextureConverter.TextureSupportIssues.ReadWrite)
                    {
                        importer.isReadable = true;
                    }
                    if ((issues & TextureConverter.TextureSupportIssues.Format) == TextureConverter.TextureSupportIssues.Format)
                    {
                        importer.textureCompression = TextureImporterCompression.Uncompressed;
                        TextureImporterPlatformSettings platformSettings = importer.GetPlatformTextureSettings("Standalone");
                        platformSettings.overridden = true;
                        platformSettings.format     = TextureImporterFormat.RGBA32;
                        importer.SetPlatformTextureSettings(platformSettings);
                    }
                    if ((issues & TextureConverter.TextureSupportIssues.Dimensions) == TextureConverter.TextureSupportIssues.Dimensions)
                    {
                    }
                    importer.SaveAndReimport();
                }
                else if ((issues & TextureConverter.TextureSupportIssues.Dimensions) == TextureConverter.TextureSupportIssues.Dimensions)
                {
                    Debug.Log("Texture '" + tex.name + "' is using an unspported dimension '" + tex.dimension.ToString() + "' an cannot be converted.");
                }
                progress++;
            }

            EditorUtility.DisplayProgressBar("Fixing Meshes", "", 0.0f);
            progress = 0;

            foreach (Mesh mesh in allMeshes)
            {
                EditorUtility.DisplayProgressBar("Fixing Meshes", "Processing " + mesh.name, (float)((float)progress / (float)allMeshes.Count));

                if (!mesh.isReadable)
                {
                    report += "Mesh '" + mesh.name + "' is not readable.\n";
                    string        path     = AssetDatabase.GetAssetPath(mesh);
                    ModelImporter importer = (ModelImporter)ModelImporter.GetAtPath(path);
                    importer.isReadable = true;
                    importer.SaveAndReimport();
                }
                progress++;
            }

            EditorUtility.ClearProgressBar();

            Debug.Log("Fix report for GameObject '" + gameObject.name + "':\n" + report);
        }