查看: 4407|回复: 0
打印 上一主题 下一主题

Unity3d批量修改贴图导入设置工具脚本

[复制链接]

435

主题

2

听众

6371

积分

高级设计师

Rank: 6Rank: 6

纳金币
6372
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-23 11:33:09 |只看该作者 |倒序浏览
这个脚本我一直再用,它小巧,但是威力大.特别针对大批量贴图要调整尺寸等等的时候作用尤为明显.



在菜单中添加"Custom→Texture"的方式来批量改变所选的贴图导入设置.Unity本身只能一次打开一张图片进行导入设置,目前这个脚本可以批量更改贴图格式,是否开启MipMap,调整纹理最大尺寸,是否可读等等.








用法是把脚本放在你项目的资源目录的Editor文件夹下.然后选择你要批处理的纹理.到菜单中选择要处理的类型就可以了.



ChangeTextureImportSettings.cs for Unity2.x




csharp 代码
using UnityEngine;

using UnityEditor;



// /////////////////////////////////////////////////////////////////////////////////////////////////////////

//

// Batch Texture import settings modifier.

//

// Modifies all selected textures in the project window and applies the requested modification on the

// textures. Idea was to have the same choices for multiple files as you would have if you open the

// import settings of a single texture. Put this into Assets/Editor and once compiled by Unity you find

// the new functionality in Custom -> Texture. Enjoy! :-)

//

// Based on the great work of benblo in this thread:

// http://f***m.unity3d.com/viewtopic.php?t=16079&start=0&postdays=0&postorder=asc&highlight=textureimporter

//

// Developed by Martin Schultz, Decane in August 2009

// e-mail: ms@decane.net

//

// /////////////////////////////////////////////////////////////////////////////////////////////////////////

public class ChangeTextureImportSettings : ScriptableObject {

   

    [MenuItem ("Custom/Texture/Change Texture Format/Auto")]

    static void ChangeTextureFormat_Auto() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic);

    }

        

    [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT1")]

    static void ChangeTextureFormat_RGB_DXT1() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT1);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT5")]

    static void ChangeTextureFormat_RGB_DXT5() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT5);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGB 16 bit")]

    static void ChangeTextureFormat_RGB_16bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB16);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGB 24 bit")]

    static void ChangeTextureFormat_RGB_24bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB24);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/Alpha 8 bit")]

    static void ChangeTextureFormat_Alpha_8bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.Alpha8);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGBA 16 bit")]

    static void ChangeTextureFormat_RGBA_16bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB16);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGBA 32 bit")]

    static void ChangeTextureFormat_RGBA_32bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB32);

    }

   

    // ----------------------------------------------------------------------------

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/32")]

    static void ChangeTextureSize_32() {

        SelectedChangeMaxTextureSize(32);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/64")]

    static void ChangeTextureSize_64() {

        SelectedChangeMaxTextureSize(64);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/128")]

    static void ChangeTextureSize_128() {

        SelectedChangeMaxTextureSize(128);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/256")]

    static void ChangeTextureSize_256() {

        SelectedChangeMaxTextureSize(256);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/512")]

    static void ChangeTextureSize_512() {

        SelectedChangeMaxTextureSize(512);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/1024")]

    static void ChangeTextureSize_1024() {

        SelectedChangeMaxTextureSize(1024);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/2048")]

    static void ChangeTextureSize_2048() {

        SelectedChangeMaxTextureSize(2048);

    }

   

    // ----------------------------------------------------------------------------

   

    [MenuItem ("Custom/Texture/Change MipMap/Enable MipMap")]

    static void ChangeMipMap_On() {

        SelectedChangeMimMap(***e);

    }

   

    [MenuItem ("Custom/Texture/Change MipMap/Disable MipMap")]

    static void ChangeMipMap_Off() {

        SelectedChangeMimMap(false);

    }

   

    // ----------------------------------------------------------------------------

   

    static void SelectedChangeMimMap(bool enabled) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.mipmapEnabled = enabled;   

            AssetDatabase.ImportAsset(path);

        }

    }

   

    static void SelectedChangeMaxTextureSize(int size) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.maxTextureSize = size;  

            AssetDatabase.ImportAsset(path);

        }

    }

   

    static void SelectedChangeTextureFormatSettings(TextureImporterFormat newFormat) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            //Debug.Log("path: " + path);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.textureFormat = newFormat;  

            AssetDatabase.ImportAsset(path);

        }

    }

   

    static Object[] GetSelectedTextures()

    {

        return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);

    }

}




ChangeTextureImportSettingsUnity3 for Unity3.x




csharp 代码
using UnityEngine;

using UnityEditor;



// /////////////////////////////////////////////////////////////////////////////////////////////////////////

//

// Batch Texture import settings modifier.

//

// Modifies all selected textures in the project window and applies the requested modification on the

// textures. Idea was to have the same choices for multiple files as you would have if you open the

// import settings of a single texture. Put this into Assets/Editor and once compiled by Unity you find

// the new functionality in Custom -> Texture. Enjoy! :-)

//

// Based on the great work of benblo in this thread:

// http://f***m.unity3d.com/viewtopic.php?t=16079&start=0&postdays=0&postorder=asc&highlight=textureimporter

//

// Developed by Martin Schultz, Decane in August 2009

// e-mail: ms@decane.net

//

// Updated for Unity 3.0 by col000r in August 2010

// http://col000r.blogspot.com

//

// /////////////////////////////////////////////////////////////////////////////////////////////////////////

public class ChangeTextureImportSettingsUnity3 : ScriptableObject {

   

    [MenuItem ("Custom/Texture/Change Texture Format/Auto Compressed")]

    static void ChangeTextureFormat_AutoCompressed() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticCompressed);

    }



    [MenuItem ("Custom/Texture/Change Texture Format/Auto 16bit")]

    static void ChangeTextureFormat_Auto16Bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic16bit);

    }



    [MenuItem ("Custom/Texture/Change Texture Format/Auto ***ecolor")]

    static void ChangeTextureFormat_Auto***ecolor() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic***ecolor);

    }

        

    [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT1")]

    static void ChangeTextureFormat_RGB_DXT1() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT1);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT5")]

    static void ChangeTextureFormat_RGB_DXT5() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT5);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGB 16 bit")]

    static void ChangeTextureFormat_RGB_16bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB16);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGB 24 bit")]

    static void ChangeTextureFormat_RGB_24bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB24);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/Alpha 8 bit")]

    static void ChangeTextureFormat_Alpha_8bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.Alpha8);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/ARGB 16 bit")]

    static void ChangeTextureFormat_RGBA_16bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB16);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGBA 32 bit")]

    static void ChangeTextureFormat_RGBA_32bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.RGBA32);

    }



    [MenuItem ("Custom/Texture/Change Texture Format/ARGB 32 bit")]

    static void ChangeTextureFormat_ARGB_32bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB32);

    }



    [MenuItem ("Custom/Texture/Change Texture Format/RGB PVRTC 2bit")]

    static void ChangeTextureFormat_RGB_PVRTC_2bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB2);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGBA PVRTC 2bit")]

    static void ChangeTextureFormat_RGBA_PVRTC_2bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA2);

    }   



    [MenuItem ("Custom/Texture/Change Texture Format/RGB PVRTC 4bit")]

    static void ChangeTextureFormat_RGB_PVRTC_4bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB4);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Format/RGBA PVRTC 4bit")]

    static void ChangeTextureFormat_RGBA_PVRTC_4bit() {

        SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA4);

    }

        

    // ----------------------------------------------------------------------------

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/32")]

    static void ChangeTextureSize_32() {

        SelectedChangeMaxTextureSize(32);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/64")]

    static void ChangeTextureSize_64() {

        SelectedChangeMaxTextureSize(64);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/128")]

    static void ChangeTextureSize_128() {

        SelectedChangeMaxTextureSize(128);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/256")]

    static void ChangeTextureSize_256() {

        SelectedChangeMaxTextureSize(256);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/512")]

    static void ChangeTextureSize_512() {

        SelectedChangeMaxTextureSize(512);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/1024")]

    static void ChangeTextureSize_1024() {

        SelectedChangeMaxTextureSize(1024);

    }

   

    [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/2048")]

    static void ChangeTextureSize_2048() {

        SelectedChangeMaxTextureSize(2048);

    }

   

    // ----------------------------------------------------------------------------

   

    [MenuItem ("Custom/Texture/Change MipMap/Enable MipMap")]

    static void ChangeMipMap_On() {

        SelectedChangeMimMap(***e);

    }

   

    [MenuItem ("Custom/Texture/Change MipMap/Disable MipMap")]

    static void ChangeMipMap_Off() {

        SelectedChangeMimMap(false);

    }

   

    // ----------------------------------------------------------------------------





    [MenuItem ("Custom/Texture/Change Non Power of 2/None")]

    static void ChangeNPOT_None() {

        SelectedChangeNonPowerOf2(TextureImporterNPOTScale.None);

    }



    [MenuItem ("Custom/Texture/Change Non Power of 2/ToNearest")]

    static void ChangeNPOT_ToNearest() {

        SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToNearest);

    }

   

    [MenuItem ("Custom/Texture/Change Non Power of 2/ToLarger")]

    static void ChangeNPOT_ToLarger() {

       SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToLarger);

    }



    [MenuItem ("Custom/Texture/Change Non Power of 2/ToSmaller")]

    static void ChangeNPOT_ToSmaller() {

        SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToSmaller);

    }   

   

    // ----------------------------------------------------------------------------



    [MenuItem ("Custom/Texture/Change Is Readable/Enable")]

    static void ChangeIsReadable_Yes() {

        SelectedChangeIsReadable(***e);

    }



    [MenuItem ("Custom/Texture/Change Is Readable/Disable")]

    static void ChangeIsReadable_No() {

        SelectedChangeIsReadable(false);

    }   

   

    // ----------------------------------------------------------------------------









    static void SelectedChangeIsReadable(bool enabled) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.isReadable = enabled;   

            AssetDatabase.ImportAsset(path);

        }

    }





    static void SelectedChangeNonPowerOf2(TextureImporterNPOTScale npot) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.npotScale = npot;   

            AssetDatabase.ImportAsset(path);

        }

    }

   

    static void SelectedChangeMimMap(bool enabled) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.mipmapEnabled = enabled;   

            AssetDatabase.ImportAsset(path);

        }

    }

   

    static void SelectedChangeMaxTextureSize(int size) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.maxTextureSize = size;  

            AssetDatabase.ImportAsset(path);

        }

    }

   

    static void SelectedChangeTextureFormatSettings(TextureImporterFormat newFormat) {

   

        Object[] textures = GetSelectedTextures();

        Selection.objects = new Object[0];

        foreach (Texture2D texture in textures)  {

            string path = AssetDatabase.GetAssetPath(texture);

            //Debug.Log("path: " + path);

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            textureImporter.textureFormat = newFormat;  

            AssetDatabase.ImportAsset(path);

        }

    }

   

    static Object[] GetSelectedTextures()

    {

        return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);

    }

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-23 05:23 , Processed in 0.107661 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部