unity编辑器批量修改纹理资源的设置


using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Gxzz
{
    internal sealed class Texture2DImportSettingEditor : EditorWindow
    {
        private string m_Path;
        private readonly string kResRootName = "Assets";
        private PlatformFormatName m_PlatformFormatName;
        private TextureImporterFormat m_TextureImporterFormat;

        private readonly Dictionary<PlatformFormatName,string> m_TextureImporterFormatDic = new Dictionary<PlatformFormatName, string>
        {
            {PlatformFormatName.Standalone,"Standalone"},
            {PlatformFormatName.Web,"Web"},
            {PlatformFormatName.iPhone,"iPhone"},
            {PlatformFormatName.Android,"Android"},
            {PlatformFormatName.WindowsStoreApps,"Windows Store Apps"},
            {PlatformFormatName.PS4,"PS4"},
            {PlatformFormatName.PSM,"PSM"},
            {PlatformFormatName.XboxOne,"XboxOne"},
            {PlatformFormatName.Nintendo3DS,"Nintendo 3DS"},
            {PlatformFormatName.tvOS,"tvOS"}
        };
       
        [MenuItem("Tools/Texture2DImport")]
        public static void Create()
        {
            var window = CreateWindow<Texture2DImportSettingEditor>();
            window.name = "Texture2DImportSettingEditor";
            window.maxSize = new Vector2(500, 1000);
            window.Show();
        }

        private void OnGUI()
        {
            CreateTextureImport();
        }

        void CreateTextureImport()
        {
            EditorGUILayout.BeginVertical("Box");
            {
                CreateSetPath();
                EditorGUILayout.Space(10);
                CreateTextureImporterFormat();
                EditorGUILayout.Space(10);
                CreatePlatformFormatName();
                EditorGUILayout.Space(10);
                CreateSettextureImporterngBtn();
            }

            EditorGUILayout.EndVertical();
        }


        void CreateTextureImporterFormat()
        {
            EditorGUILayout.BeginHorizontal("Box");
            {
                m_TextureImporterFormat =
                    (TextureImporterFormat) EditorGUILayout.EnumPopup("format格式设置", m_TextureImporterFormat);
            }
            EditorGUILayout.EndHorizontal();
        }

        void CreatePlatformFormatName()
        {
            EditorGUILayout.BeginHorizontal("Box");
            {
                m_PlatformFormatName = (PlatformFormatName)EditorGUILayout.EnumPopup("平台名字设置", m_PlatformFormatName);
            }
            EditorGUILayout.EndHorizontal();
        }
       
       
        void CreateSetPath()
        {
            EditorGUILayout.BeginHorizontal("Box");
            {
                EditorGUILayout.LabelField("文件夹", GUILayout.Width(40));
                m_Path = EditorGUILayout.TextField(m_Path);
                if (GUILayout.Button("浏览"))
                {
                    m_Path = EditorUtility.OpenFolderPanel("Texture2D", m_Path, "");
                }
            }
            EditorGUILayout.EndHorizontal();
        }

        void CreateSettextureImporterngBtn()
        {
            EditorGUILayout.BeginHorizontal("Box");
            {
                if (GUILayout.Button("设置"))
                {
                    SetTexture();
                }
            }
            EditorGUILayout.EndHorizontal();
        }


        void SetTexture()
        {
            var indexOf = m_Path.IndexOf(kResRootName, StringComparison.Ordinal);
            if (indexOf == -1) return;
            var searchInFolders = m_Path.Substring(indexOf, m_Path.Length - indexOf);

            var allPath = AssetDatabase.FindAssets("t:Sprite", new string[] {searchInFolders});
            for (var i = 0; i < allPath.Length; i++)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(allPath[i]);
                var texture = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D)) as Texture2D;
                if (texture == null) continue;
                var path = AssetDatabase.GetAssetPath(texture);


                var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
                if (textureImporter != null)
                {
                    textureImporter.maxTextureSize = 2048;
                    textureImporter.mipmapEnabled = false;
                    textureImporter.wrapMode = TextureWrapMode.Clamp;
                    textureImporter.textureType = TextureImporterType.Sprite;
                    textureImporter.filterMode = FilterMode.Bilinear;
                    textureImporter.isReadable = false;
                    textureImporter.mipmapEnabled = false;

                    var result = m_TextureImporterFormatDic.TryGetValue(m_PlatformFormatName, out var formatName);
                    if (result)
                    {
                        var importerSettings = new TextureImporterPlatformSettings
                        {
                            overridden =  true,
                            name = formatName,
                            format = m_TextureImporterFormat
                        };
                        textureImporter.SetPlatformTextureSettings(importerSettings);
                        textureImporter.SaveAndReimport();
                    }
                           
                }

                AssetDatabase.ImportAsset(path);
            }
        }
    }

    public enum PlatformFormatName
    {
        Standalone = 0,
        Web,
        iPhone,
        Android,
        WindowsStoreApps,
        PS4,
        PSM,
        XboxOne,
        Nintendo3DS,
        tvOS
    }
}

 

posted @ 2020-10-17 09:30  MiKiNuo  阅读(526)  评论(0)    收藏  举报