Unity3D Texture导入设置(包含手动和自动设置)

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.Text.RegularExpressions;
using System.IO;

public class UISpritePostprocessor : AssetPostprocessor
{
    static string effectPath = "Game/Art/Effects";
    static string effectFloder = "effect_texture";
    static string fontFloder1 = "/Font";
    static string fontFloder2 = "/font";
    static string SPRITES_DIR = "Assets/Game/Art/Textures/";

    void OnPreprocessTexture()
    {
        TextureImporter textureImporter = (TextureImporter)assetImporter;
        SetTextureFormat(textureImporter);
    }

    void OnPostprocessTexture(Texture2D texture)
    {

    }

    private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] moveFromAssetPath)
    {

    }

    [MenuItem("AssetsTools/将选择文件夹的贴图设置成规定格式", false, 22)]
    static void SetSelectTextures()
    {
        Object[] selects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        for (int i = 0; i < selects.Length; ++i)
        {
            Object selected = selects[i];
            string path = AssetDatabase.GetAssetPath(selected);
            AssetImporter asset = AssetImporter.GetAtPath(path);
            TextureImporter textureImporter = asset as TextureImporter;
            if (textureImporter != null)
            {
                SetTextureFormat(textureImporter);
                SetTexturePackingTag(textureImporter, path);
                textureImporter.SaveAndReimport();
            }
        }
        AssetDatabase.Refresh();
    }

    public static void SetTextureFormat(TextureImporter textureImporter)
    {
        textureImporter.isReadable = false;
        textureImporter.mipmapEnabled = false;
        textureImporter.npotScale = TextureImporterNPOTScale.None;
        textureImporter.alphaIsTransparency = true;

        TextureImporterPlatformSettings ios = textureImporter.GetPlatformTextureSettings("iPhone");
        ios.overridden = true;
        ios.format = TextureImporterFormat.ASTC_6x6;
        textureImporter.SetPlatformTextureSettings(ios);

        TextureImporterPlatformSettings android = textureImporter.GetPlatformTextureSettings("Android");
        android.overridden = true;
        android.format = TextureImporterFormat.ETC2_RGBA8;
        textureImporter.SetPlatformTextureSettings(android);

        TextureImporterPlatformSettings pc = textureImporter.GetPlatformTextureSettings("Standalone");
        pc.overridden = true;
        pc.format = TextureImporterFormat.RGBA32;
        textureImporter.SetPlatformTextureSettings(pc);
    }

    private static bool IsNeedAtlas(Texture2D texture)
    {
        int width = texture.width;
        int height = texture.height;
        if (height > 1024 & width > 512)
        {
            return false;
        }
        else if (width > 1024 & height > 512)
        {
            return false;
        }
        else if (width <= 2048 & height <= 2048)
        {
            return true;
        }
        return false;
    }

    private static bool IsEffectAssets(string path)
    {
        path = path.Replace("\\", "/");
        return path.Contains(effectPath) || path.Contains(effectFloder) || path.Contains(fontFloder1) || path.Contains(fontFloder2);
    }

    private static void SetAssetsPackingTag(string[] assets)
    {
        for (int i = 0; i < assets.Length; ++i)
        {
            string path = assets[i];
            var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            if (textureImporter && textureImporter.textureType == TextureImporterType.Sprite)
            {
                if (SetTexturePackingTag(textureImporter, path))
                    textureImporter.SaveAndReimport();

            }
        }
    }

    public static bool SetTexturePackingTag(TextureImporter textureImporter, string path)
    {
        bool needChange = false;
        string tag = GetSpritePackingTag(path);
        Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
        if (!IsNeedAtlas(texture))
        {
            tag = "";
        }
        if (tag != textureImporter.spritePackingTag)
        {
            textureImporter.spritePackingTag = tag;
            needChange = true;
        }
        return needChange;
    }

    public static string GetSpritePackingTag(string path)
    {
        string packingTag = path.ToLower();
        packingTag = Path.GetDirectoryName(packingTag);
        packingTag = packingTag.Replace("\\", "/");
        packingTag = packingTag.Replace(SPRITES_DIR.ToLower(), "");
        packingTag = packingTag.Replace('/', '_');
        return packingTag;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值