当前位置:   article > 正文

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

unity textureimporter
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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/112642
推荐阅读
相关标签
  

闽ICP备14008679号