一、制作图集
使用工具:free texture packer
备注:导出格式选Unity 3D
二、使用图集
使用工具:TexturePackerImporter
1、导入图集
导入到资源目录中
2、编辑器使用图集
3、代码中使用图集
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TexturesTool
{
/// <summary>
/// 缓存资源
/// </summary>
private Dictionary<string, Dictionary<string, Sprite>> spriteAtlasSourceName = new Dictionary<string, Dictionary<string, Sprite>>();
public Sprite LoadSpriteInAtlas(string path, string spriteName)
{
if (LoadSpriteAtlasInResource(path))
{
return LoadSpriteInCache(path, spriteName);
}
else
{
return null;
}
}
/// <summary>
/// 从缓存中加载资源
/// </summary>
/// <param name="path"></param>
/// <param name="spriteName"></param>
/// <returns></returns>
private Sprite LoadSpriteInCache(string path, string spriteName)
{
if (spriteAtlasSourceName[path].ContainsKey(spriteName))
{
return spriteAtlasSourceName[path][spriteName];
}
else
{
Debug.Log(string.Format("资源集{0}中不存在资源{1}", path, spriteName));
return null;
}
}
/// <summary>
/// 加载资源
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private bool LoadSpriteAtlasInResource(string path)
{
if (spriteAtlasSourceName.ContainsKey(path))
{
return true;
}
spriteAtlasSourceName.Add(path, new Dictionary<string, Sprite>());
var sprites = Resources.LoadAll<Sprite>(path);
if (sprites == null)
{
Debug.Log("资源不存在:" + path);
return false;
}
for (int i = 0; i < sprites.Length; i++)
{
spriteAtlasSourceName[path].Add(sprites[i].name, sprites[i]);
}
return true;
}
}
留言