栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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(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;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号