Unity 来讲一讲关于SpriteAtlas的事情

恭喜熊猫先生,在半年的散养状态之后,终于再次开始工作了。(小声比比,今年真的是好难找工作啊。模棱两可的能力,四处碰壁)

SpriteAtlas是unity2017引入的新内容。其实就是图集。最大的好处就是,同一图集里的图片,在渲染的时候,会只产生一个DrawCall。(可能大概也许是这样)

今天就来说一说关于这个东西的几件事情。搜别人的文章,真的是太难了,各种找不到……

以unity2018的api为基础来主要说几个吧。

首先,是存放的问题。

atlas和其要导入的Texture或者Sprite,切记不能放在Resources目录下。因为Resources目录下的文件,是不会被打入到图集中的。

然后,是加载的问题。

简言之:

SpriteAtlas sa = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(path);

SpriteAtlas是UnityEngine.U2D中的类。

在官方的描述中,给的做法是做个继承了Mono的脚本,然后把SpriteAtlas加入到其List<SpriteAtlass>中。但是这样的话,岂不是很麻烦。每新建一个就要挂载进去,或者就写个自动化添加的。

自动化添加听起来很不错,不过我主要是不想做一个这样的脚本。【狗头】

一定能用代码解决的。想一想,atlas不能放在Resources里,所以肯定不是Resources.Load()方法了。所以就需要使用AssetDatabase这个能管理整个工程的文件的静态类了。

最后,说一下关于平台的属性的问题。

在这里插入图片描述
有的人的Unity没有后面两个小图标,这两个分别是IOS和Android的开发支持包的。可以在Build Settings中下载。
在这里插入图片描述
以安卓来举例子,默认是这个样子的。Override for Android的意思是出安卓包的时候,各种参数使用这里选择的数据。
在这里插入图片描述
然而,昨天接到了一个任务,因为每次创建新的图集的时候,这个都是默认没有选的,就要手动改,就很麻烦。其实麻烦倒是其次,最重要的是容易忘记,要是忘了的话,在出包的时候就达不到理想的样子。就要重(jia)出(ban)。

所以就需要我在有人创建新的图集时,自动勾选override,同时将Format改为RGBA ASTC 4x4.

简言之:

public class AssetsPostManager : AssetPostprocessor {
    static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {

        if (0 < importedAsset.Length)
        {
            foreach (string filePath in importedAsset)
            {
                //判断是否是文件 跳过文件夹
                if (filePath.Contains("."))
                {
                    //通过后缀名做判断文件类型
                    string suffix = filePath.Substring(filePath.LastIndexOf('.') + 1);

                    switch (suffix)
                    {
                        case "spriteatlas":
                            OnPostprocessSpriteAtlas(filePath);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    //SpriteAtlas
    private static void OnPostprocessSpriteAtlas(string path)
    {
        SpriteAtlas sa = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(path);
        SpriteAtlasExtensions.SetPlatformSettings(sa, new TextureImporterPlatformSettings
        {
            name = "Android",
            overridden = true,
            format = TextureImporterFormat.ASTC_RGBA_4x4,
        });
    }

}

接下来讲一下这个代码和我当时遇到的坑。

AssetPostprocessor

AssetPostprocessor lets you hook into the import pipeline and run scripts prior or after importing assets.
允许您挂接到导入管道并在导入资产之前或之后运行脚本。

也可以看一下这篇文章,很直观。
https://blog.csdn.net/qq826364410/article/details/86515209

听起来,是个不错的编辑器工具类。(感觉走向工具人的道路被打开了)

public static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)

所有的资源的导入,删除,移动,都会调用此方法,注意,这个方法是static的。可以看到他的参数,导入的资产,删除的资产,移动的资产,资产移动前的路径。

所以我就花了十分钟,做了一下分类。把SpriteAtlas的摘出来,在一个方法里做修改。

至于OnPostprocessSpriteAtlas,并不是原生的,希望看到的人不要被误导。

接下来,就是花费了我好几个小时的修改其参数的部分了。

首先,花了二十多分钟,在UnityEditor中找到了可以修改参数的静态类,SpriteAtlasExtensions。

F12进去看一下,主要有三个方法。

        // 摘要:
        //     Set the SpriteAtlasPackingSettings to use when packing this SpriteAtlas
        public static void SetPackingSettings(this SpriteAtlas spriteAtlas, SpriteAtlasPackingSettings src);
        // 摘要:
        //     Set the platform specific settings.
        public static void SetPlatformSettings(this SpriteAtlas spriteAtlas, TextureImporterPlatformSettings src);
        // 摘要:
        //     Set the SpriteAtlasTextureSettings for the packed texture generated by this SpriteAtlas.
        public static void SetTextureSettings(this SpriteAtlas spriteAtlas, SpriteAtlasTextureSettings src);

看一下面板:
在这里插入图片描述
红色的部分,Packing。所以其参数都在SetPackingSettings中处理。

蓝色的部分, Texture。所以其参数都在SetTextureSettings中处理。

最下面的,关于各个平台设置的。所以其参数都在SetPlatformSettings中处理。

这时候去看一下api,发现,并没有设置哪个平台的???

只好再去Unity Api网站里看一下。上面说,TextureImporterPlatformSettings是TextureImporter的一种变形。

那好,我就去看看TextureImporter。还不错,发现了几个有用的方法。

        [Obsolete("Use UnityEditor.TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings) instead.")]
        public void SetPlatformTextureSettings(string platform, int maxTextureSize, TextureImporterFormat textureFormat);
        // 摘要:
        //     Set specific target platform settings.
        public void SetPlatformTextureSettings(TextureImporterPlatformSettings platformSettings);
        // 摘要:
        //     Set specific target platform settings.
        [Obsolete("Use UnityEditor.TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings) instead.")]
        public void SetPlatformTextureSettings(string platform, int maxTextureSize, TextureImporterFormat textureFormat, int compressionQuality, bool allowsAlphaSplit);
        // 摘要:
        //     Set specific target platform settings.
        [Obsolete("Use UnityEditor.TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings) instead.")]
        public void SetPlatformTextureSettings(string platform, int maxTextureSize, TextureImporterFormat textureFormat, [DefaultValue(false)] bool allowsAlphaSplit);
        

string platform,可以设置平台了。突然,Obsolete???

什么鬼,已经被弃用了。感到迷茫。

开始冲浪了加去看API。一个小时后,我投降。

只好等主程有时间了,来看看咋办。

这时候,我就只能回到梦开始的地方,再看看TextureImporterPlatformSettings这个类。

又是一个突然,我看到一个name的字段。这个到底是干嘛的?

一开始,我以为会有很多套配置,所以可以作为备注使用。现在觉得,有必要吗?

打开摘要一看:

// 摘要:
        //     Name of the build target.
        public string name { get; set; }

the build target

再看看文件里是啥样的:

platformSettings:
    - serializedVersion: 2
      m_BuildTarget: Android
      m_MaxTextureSize: 2048
      m_ResizeAlgorithm: 0
      m_TextureFormat: 54
      m_TextureCompression: 1
      m_CompressionQuality: 50
      m_CrunchedCompression: 0
      m_AllowsAlphaSplitting: 0
      m_Overridden: 1
      m_AndroidETC2FallbackOverride: 0

m_BuildTarget:Android

我……

在这里插入图片描述
好了,去看一下API
在这里插入图片描述
妥了。

难。真的难。工具人太难了。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值