Unity一键修改Textture 格式工具

在这里插入图片描述
代码:

/**
 * 图片格式设置 
 * @author cyh
 * @data 2018年10月115日
 */
using UnityEngine;
using UnityEditor;

public class LTextureFormat : MonoBehaviour
{
    

    [MenuItem("Tools/Texture/DefaultImporter")]
	static void ChangeTextureInfo()
    {
        Object[] textures = GetSelectedTextures();
        foreach (Texture2D texture in textures)
        {
            string path = AssetDatabase.GetAssetPath(texture);
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            textureImporter.textureType = TextureImporterType.Default;
            textureImporter.textureShape = TextureImporterShape.Texture2D;
            textureImporter.npotScale = TextureImporterNPOTScale.None;
            textureImporter.mipmapEnabled = false;
            textureImporter.isReadable = false;
            AssetDatabase.ImportAsset(path);
        }
        Debug.Log("Default Importer is finish!");
    }

    [MenuItem("Tools/Texture/AtlasTextureFormat")]
    static void AtlasTextureFormat()
    {
        AndroidTextureFormat_ETC2_8bits();
        IOSTextureFormat_PVRTC_RGBA4();
        Debug.Log("AtlasTextureFormat is finish!");
    }

    [MenuItem("Tools/Texture/Change Texture Format/Android Auto")]
    static void AndroidTextureFormat_Auto()
    {
        AChangeTextureFormatSettings(TextureImporterFormat.Automatic);
    }

    [MenuItem("Tools/Texture/Change Texture Format/Android DXT5")]
    static void AndroidTextureFormat_DXT5()
    {
        AChangeTextureFormatSettings(TextureImporterFormat.DXT5);
    }

    [MenuItem("Tools/Texture/Change Texture Format/Android ETC2 8 bits")]
    static void AndroidTextureFormat_ETC2_8bits()
    {
        AChangeTextureFormatSettings(TextureImporterFormat.ETC2_RGBA8);
    }
    
    [MenuItem("Tools/Texture/Change Texture Format/IOS Auto")]
    static void IOSTextureFormat_PVRTC_RGBA4()
    {
        IAutoChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA4);
    }

    [MenuItem("Tools/Texture/Change Texture Format/IOS RGBA32")]
    static void IOSTextureFormat_PVRTC_RGBA32()
    {
        IChangeTextureFormatSettings(TextureImporterFormat.RGBA32);
    }

    static void AChangeTextureFormatSettings(TextureImporterFormat tf)
    {
        Object[] textures = GetSelectedTextures();
        foreach (Texture2D texture in textures)
        {
            string path = AssetDatabase.GetAssetPath(texture);
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            TextureImporterPlatformSettings tp = textureImporter.GetPlatformTextureSettings("Android");
            tp.overridden = true;
            tp.format = tf;
            textureImporter.SetPlatformTextureSettings(tp);
            AssetDatabase.ImportAsset(path);
        }
        Debug.Log("[Android] >>> TextureFormatSettings is finish!");
    }

    static void IChangeTextureFormatSettings(TextureImporterFormat tf)
    {
        Object[] textures = GetSelectedTextures();
        foreach (Texture2D texture in textures)
        {
            string path = AssetDatabase.GetAssetPath(texture);
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            TextureImporterPlatformSettings tp = textureImporter.GetPlatformTextureSettings("iPhone");
            tp.overridden = true;
            tp.format = tf;
            textureImporter.SetPlatformTextureSettings(tp);
            AssetDatabase.ImportAsset(path);
        }
        Debug.Log("[IOS] >>> TextureFormatSettings is finish!");
    }

    static void IAutoChangeTextureFormatSettings(TextureImporterFormat tf)
    {
        Object[] textures = GetSelectedTextures();
        foreach (Texture2D texture in textures)
        {
            string path = AssetDatabase.GetAssetPath(texture);
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            TextureImporterPlatformSettings tp = textureImporter.GetPlatformTextureSettings("iPhone");
            tp.overridden = true;
            tp.format = texture.width == texture.height ? tf : TextureImporterFormat.RGBA32;
            textureImporter.SetPlatformTextureSettings(tp);
            AssetDatabase.ImportAsset(path);
        }
        Debug.Log("[IOS] >>> TextureFormatSettings is finish!");
    }

    static Object[] GetSelectedTextures()
    {
        return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值