2019-03-27 Unity 图集管理

作者: 抠脚码农真的惨 | 来源:发表于2019-03-27 20:30 被阅读0次

最近正好在整理项目中的UI部分,所以写了这个文章记录一下。在不借用第三方插件的情况下,Unity当前有两种打图集的方式。1.Legacy Sprite Packer 这个是老版本的打图集方式。

如果打开原来的Sprite Packer有这样一个提示

image

在Editor->Project Setting->Editor 中设置

image

然后设置图片Packing Tag。

image

在打开点击Pack就会打出图集

image

本人认为这种打包方式会有一个比较严重的问题,因为图集的信息是放在了当前的项目的Library/AtlasCache文件加下面,所以图集信息只能放在包体中,不更新整包的话,就不能更新图集信息,比如说,你后面准备在线热更新一个系统,会添加图集信息,到那时这个图集信息就不能通过AssetBundle的方式更新。所有我们项目舍弃了这种方法。

正好在网上看到了Unity2017以后的有新的图集方法,SpriteAtlas。

image

设置方法如下:

image

暂时我只用到了Type=Master的情况。

下面说一下,读取Sprite的方法:


static void LoadSpriteFromAtlas()

    {

        string atlasPath = "Assets/AssetsPackage/Atlas/ItemIconAtlas.spriteatlas";

        SpriteAtlas srpiteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(atlasPath);

        Sprite sprite = srpiteAtlas.GetSprite("42159");

        Debug.Log(sprite.name);

        Image image = GameObject.Find("Canvas/Image").GetComponent<Image>();

        image.sprite = sprite;

    }

接下来就是知道一个sprite在哪个图集中,也有可能不在Atlas中。所以在AssetBundle模式下,我先生成一个映射关系的Map,将关系文件保存在txt中,将txt文件也打到AssetBundle中。在初始化AssetBundle管理器的时候,初始化这个Dictionary,后面我们就可以拿到sprite和atlas的对应关系。代码如下:


using System.IO;

using UnityEditor;

using UnityEngine;

using UnityEngine.U2D;

using System.Collections.Generic;

using System;

public class AtlasEditor

{

    public const string SpriteAtlasMapPath = "Assets/Editor/AssetBundleEditor/BuildPipeline/SpriteAtlasMap.txt";

    [MenuItem("Tools/UI/Atlas/LoadSpriteAtlas")]

    public static void LoadSpriteAtlas()

    {

        string atlasPath = "Assets/AssetsPackage/Atlas/";

        string[] atlasFileList = Directory.GetFiles(atlasPath, "*.spriteatlas", SearchOption.AllDirectories);

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < atlasFileList.Length; i++)

        {

            string filePath = atlasFileList[i];

            SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(filePath);

            // spriteAtlas中精灵个数

            int spriteCount = spriteAtlas.spriteCount;

            // sprite 

            Sprite[] spriteArray = new Sprite[spriteCount];

            // spriteArray得到数组

            spriteAtlas.GetSprites(spriteArray);

            // atlas 路径

            string atlasAssetPath = AssetDatabase.GetAssetPath(spriteAtlas);

            // 把相对的关系存下来

            for (int j = 0; j < spriteArray.Length; j++)

            {

                string spriteAssetPath = AssetDatabase.GetAssetPath(spriteArray[j].texture);

                sb.Append(StringUtils.Concat(spriteAssetPath, "\t", atlasAssetPath, "\n"));

            }

        }

        FileUtils.SafeWriteAllText(SpriteAtlasMapPath, sb.ToString());

    }

    [MenuItem("Tools/UI/Atlas/GetSpriteMap")]

    public static Dictionary<string, string> GetSpriteMap()

    {

        Dictionary<string, string> map = new Dictionary<string, string>();

        string text = FileUtils.SafeReadAllText(SpriteAtlasMapPath);

        string[] lines = text.Split(("\n").ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

        for (int i = 0; i < lines.Length; i++)

        {

            string line = lines[i];

            string[] paths = line.Split(("\t").ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            string assetPath = paths[0];

            string spriteAtlasPath = paths[1];

            map[assetPath] = spriteAtlasPath;

        }

        return map;

    }

}

我们项目所用的策略大分率(大于512 * 512)不打入图集中,其他的图片都会打入图集中。不打如图集中的从AssetBundle中正常加载,在Atlas中的sprite加载方法:


T ret = null;

        string atlasName = AssetBundleLoader.GetSrpiteAtlas(assetPath);

        // 如果在图集中,并且加载的是Sprite

        if (!string.IsNullOrEmpty(atlasName))

        {

            SpriteAtlas spriteAtlas = ab.LoadAsset<SpriteAtlas>(atlasName);

            string srpiteName = AssetBundleLoader.GetSpriteName(assetPath);

            ret = spriteAtlas.GetSprite(srpiteName) as T;

        }

        else

        {

            ret = ab.LoadAsset<T>(assetPath);

        }

相关文章

网友评论

    本文标题:2019-03-27 Unity 图集管理

    本文链接:https://www.haomeiwen.com/subject/ysntbqtx.html