当前位置:   article > 正文

Unity图集初始化参数设置_atlasgenerator

atlasgenerator

生成图集时修改一部分参数

// ******************************************************************
//       /\ /|       @file       AtlasGenerator.cs
//       \ V/        @brief      图集生成器
//       | "")       @author     Shadowrabbit, yingtu0401@gmail.com
//       /  |                    
//      /  \\        @Modified    2021-11-09 12:37:42
//    *(__\_\        @Copyright   Copyright (c)  2021, Shadowrabbit
// ******************************************************************

using System;
using System.IO;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine;
using UnityEngine.U2D;
using Object = UnityEngine.Object;

public static class AtlasGenerator
{
    [MenuItem("Assets/Create/AutoSpriteAtlas", false, priority = -1003)]
    private static void CreateAutoSpriteAtlas()
    {
        //获取选中目录
        var folderName = SelectObjectPathInfo(out var folderDir);
        if (folderName == null || folderDir == null || folderName.Equals(string.Empty) ||
            folderDir.Equals(string.Empty))
        {
            Debug.LogError($"[王越]操作对象应为目录,当前未知");
            return;
        }

        //非法操作 在文件右键点了生成
        if (folderName.IndexOf(".", StringComparison.Ordinal) != -1)
        {
            Debug.LogError($"[王越]操作对象应为目录,当前为文件{folderName}");
            return;
        }

        var fixFolderDir = folderDir.Replace("\\", "/");
        //atlas所在完整目录
        var fullFolder = $"{fixFolderDir}/{folderName}";
        //不在预期路径下
        if (fullFolder.IndexOf("/AssetBundles/Atlas/", StringComparison.Ordinal) == -1)
        {
            Debug.LogError($"[王越]操作目标应在Assets/AssetBundles/Atlas/下,当前为{fullFolder}");
            return;
        }

        //子目录数组
        var folderArray = Directory.GetDirectories(fullFolder);
        if (folderArray.Length > 1)
        {
            Debug.LogWarning($"[王越]{fullFolder}子目录的数量大于1,超出预期");
        }

        //资源所在目录
        var atlasPath = $"{fullFolder}/{folderName}.spriteatlas";
        //删除原图集
        if (File.Exists(atlasPath))
        {
            AssetDatabase.DeleteAsset(atlasPath);
        }

        var atlas = new SpriteAtlas();
        // 设置参数 可根据项目具体情况进行设置
        var packSetting = new SpriteAtlasPackingSettings
        {
            enableTightPacking = false,
            padding = 4,
            enableRotation = true
        };
        atlas.SetPackingSettings(packSetting);
        var textureSetting = new SpriteAtlasTextureSettings
        {
            readable = false,
            generateMipMaps = false,
            sRGB = true,
            filterMode = FilterMode.Bilinear
        };
        atlas.SetTextureSettings(textureSetting);
        //安卓设置
        var androidSetting = new TextureImporterPlatformSettings
        {
            name = "Android",
            maxTextureSize = 2048,
            format = TextureImporterFormat.ASTC_4x4,
            compressionQuality = 50,
            overridden = true
        };
        //ios设置
        var iosSetting = new TextureImporterPlatformSettings
        {
            name = "iPhone",
            maxTextureSize = 2048,
            format = TextureImporterFormat.ASTC_4x4,
            compressionQuality = 50,
            overridden = true
        };
        //PC
        var pcSetting = new TextureImporterPlatformSettings
        {
            name = "Standalone",
            maxTextureSize = 2048,
            format = TextureImporterFormat.DXT5,
            compressionQuality = 50,
            overridden = true
        };
        atlas.SetPlatformSettings(androidSetting);
        atlas.SetPlatformSettings(iosSetting);
        atlas.SetPlatformSettings(pcSetting);
        AssetDatabase.CreateAsset(atlas, atlasPath);
        //引用当前目录下png
        var dir = new DirectoryInfo(fullFolder);
        var files = dir.GetFiles("*.png");
        foreach (var file in files)
        {
            atlas.Add(new Object[] { AssetDatabase.LoadAssetAtPath<Sprite>($"{fullFolder}/{file.Name}") });
        }

        //引用子目录
        foreach (var folder in folderArray)
        {
            var obj = AssetDatabase.LoadAssetAtPath(folder, typeof(Object));
            atlas.Add(new[] { obj });
        }

        AssetDatabase.SaveAssets();
        Debug.Log($"[王越]atlas生成成功: {atlasPath}");
    }

    /// <summary>
    /// 获取选中目录
    /// </summary>
    /// <param name="dirName"></param>
    /// <returns>filename</returns>
    private static string SelectObjectPathInfo(out string dirName)
    {
        dirName = string.Empty;
        if (Selection.activeInstanceID < 0)
        {
            return dirName;
        }

        var path = AssetDatabase.GetAssetPath(Selection.activeInstanceID);
        dirName = Path.GetDirectoryName(path);
        return Path.GetFileName(path);
    }
}
  • 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
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/637026
推荐阅读
相关标签
  

闽ICP备14008679号