Unity3D-UGUI图集打包与动态使用(TexturePacker)

Unity3D-UGUI图集打包与动态使⽤(TexturePacker)
前⾔
在⽤UGUI时,我们也需要将⼀个个⼩图打包成图集,以减⼩Drawcall(类似coco2d-x⼀样,打包成图集⼀次性加载以内存换取图⽚读取效率),UGUI打包并使⽤图集有两种⽅法:⼀种是使⽤系统⾃带的打包⼯具SpritePacker;⼀种是使⽤TexturePacker打包图⽚并使⽤;
正⽂
本⽂所使⽤的是Unity3d 5.4版本,TexturePacker 4.2.3版本
1,先⽤TexturePacker打⼩图打包成我们所需要的图集,打包的格式要注意是"Unity - Texture2D sprite sheet"(有⼀些低版本的TP是没有这个格式的),
打包之后会有⼀个.png和⼀个.tpsheet,不⽤作其他修改,将这两个⽂件放在⼯程资源中,这时从⼯程看这只是⼀张⼤图,并不能算是⼀个图集,使⽤⾥⾯的⼩图(这时虽然可以⽤unity3d⾃带功能,⼿动对图⽚进⾏裁剪,但裁剪的⼩图⼤⼩基本是不对的)
氢气压缩机 高压
我们只需像使⽤单独⼩图⼀样,将图集⾥的⼩图拖进Source Image⾥即可。这时我们还只能在编辑器⾥设置使⽤图集。
3,我们还需要在程序中 动态加载图集并使⽤图集⾥的⼩图,才算是完整的。unity3d 并没有明确api说明我们如何⽤这种图集,⽽常⽤Resources.Load()加载只能返回单独的⼀个图⽚纹理,所以我们⽤另⼀个⽅法 Resources.LoadAll();加载整⼀张图集,此⽅法会返回⼀个Object[],⾥⾯包含了图集的纹理 Texture2D和图集下的全部Sprite,所以我们就可以根据object 的类型和名字到我们需要的某张⼩图⽚。
4.下⾯写了⼀个图集纹理的管理类,去统⼀管理加载,是⼀个单例类,个不被销毁的GameObject绑定就⾏, 代码⽐较简单,⽤⼀个Dictionary按图集的路径过key将加载过的图集缓存起来,需要时再由外部删除掉,下⾯是代码
using UnityEngine;
抗氧化植物素
using System.Collections;
using System.Collections.Generic;
//纹理图集加载管理
public class PPTextureManage : MonoBehaviour {
private static GameObject m_pMainObject;
private static PPTextureManage m_pContainer = null;
public static PPTextureManage getInstance(){
if(m_pContainer == null){
m_pContainer = m_pMainObject.GetComponent<PPTextureManage> ();
}
return m_pContainer;
}
private Dictionary<string, Object[]> m_pAtlasDic;//图集的集合
void Awake(){
initData ();钢水脱氧
}
private void initData(){
PPTextureManage.m_pMainObject = gameObject;
m_pAtlasDic = new Dictionary<string, Object[]> ();
海水防腐涂料}
// Use this for initialization
void Start () {
}
文件传输管理系统//加载图集上的⼀个精灵
public Sprite LoadAtlasSprite(string _spriteAtlasPath,string _spriteName){
Sprite _sprite = FindSpriteFormBuffer (_spriteAtlasPath,_spriteName);
if (_sprite == null) {
Object[] _atlas = Resources.LoadAll (_spriteAtlasPath);
m_pAtlasDic.Add (_spriteAtlasPath,_atlas);
_sprite = SpriteFormAtlas (_atlas,_spriteName);
}
return _sprite;
}
//删除图集缓存
public void DeleteAtlas(string _spriteAtlasPath){
if (m_pAtlasDic.ContainsKey (_spriteAtlasPath)) {
m_pAtlasDic.Remove (_spriteAtlasPath);
溯源防伪
}
}
//从缓存中查图集,并出sprite
private Sprite FindSpriteFormBuffer(string _spriteAtlasPath,string _spriteName){  if (m_pAtlasDic.ContainsKey (_spriteAtlasPath)) {
Object[] _atlas = m_pAtlasDic[_spriteAtlasPath];
Sprite _sprite = SpriteFormAtlas(_atlas,_spriteName);
return _sprite;
}
return null;
}
//从图集中,并出sprite
private Sprite SpriteFormAtlas(Object[] _atlas,string _spriteName){
for (int i = 0; i < _atlas.Length; i++) {
if (_atlas [i].GetType () == typeof(UnityEngine.Sprite)) {
if(_atlas [i].name == _spriteName){
return (Sprite)_atlas [i];
}
}
}
Debug.LogWarning ("图⽚名:"+_spriteName+";在图集中不到");
return null;
}
}
5,代码使⽤⽰例
Sprite _sprite = Instance().LoadAtlasSprite("common/game/CommPackAltas","⼩图名字");

本文发布于:2024-09-21 22:39:02,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/179202.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:图集   加载   打包   需要
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议