TexturepackerGUI&Unity3d native2d Sprite
来源:互联网 发布:sql 总金额 编辑:程序博客网 时间:2024/06/08 20:03
TexturepackerGUI生成的图集如何在Unity3d里边用native 2d sprite显示切好的小图
a.解析Texturepacker的json用的是Litjson这个库
b.Texturepacker导出图集的时候不能用旋转(因为unity3d支持:(
上码:
using UnityEngine;using System.Collections;using System.Collections.Generic;using System.IO;using UnityEditor;using LitJson;public class EAtlasBuild { public class ERect { public int x, y; public int w, h; public ERect(JsonData jsonData) { x = (int)jsonData["x"]; y = (int)jsonData["y"]; w = (int)jsonData["w"]; h = (int)jsonData["h"]; } } public class ESpriteData { public string m_strName = ""; public ERect frame; public bool rotated = false; public bool trimmed = false; public ERect spriteSourceSize; public int sourceWidth; public int sourceHeight; public ESpriteData(JsonData jsonData,string _name) { m_strName = _name; frame = new ERect(jsonData["frame"]); rotated = (bool)jsonData["rotated"]; trimmed = (bool)jsonData["trimmed"]; spriteSourceSize = new ERect(jsonData["spriteSourceSize"]); JsonData sourceSize = jsonData["sourceSize"]; sourceWidth = (int)sourceSize["w"]; sourceHeight = (int)sourceSize["h"]; } } [MenuItem("Custom/SetAtlas")] static public void SetAtlas() { string atlasPath = Application.dataPath + "/_Atlas/UI/UI.png"; string atlasTxtPath = Application.dataPath + "/_Atlas/UI/UI.txt"; string jsonStr = File.ReadAllText(atlasTxtPath); JsonData frames = JsonMapper.ToObject(jsonStr)["frames"]; IDictionary dict = frames as IDictionary; List<ESpriteData> lstSprite = new List<ESpriteData>(); foreach (string key in dict.Keys) { string fileName = key; ESpriteData sprite = new ESpriteData(frames[fileName], fileName); lstSprite.Add(sprite); } Debug.Log("count:" + lstSprite.Count); string atlasAssetsPath = atlasPath.Substring(atlasPath.IndexOf("Assets")); TextureImporter textureImporter = AssetImporter.GetAtPath(atlasAssetsPath) as TextureImporter; if (textureImporter != null) { Texture2D texture2d = Resources.LoadAssetAtPath<Texture2D>(atlasAssetsPath); int width = texture2d.width; int height = texture2d.height; SpriteMetaData[] sprites = new SpriteMetaData[lstSprite.Count]; for (int i = 0; i < lstSprite.Count; i++) { ESpriteData spriteData = lstSprite[i]; ERect frame = spriteData.frame; sprites[i] = new SpriteMetaData(); sprites[i].name = spriteData.m_strName; sprites[i].pivot = new Vector2(0.5f, 0.5f); sprites[i].rect = new Rect( frame.x, height - frame.y - frame.h, frame.w, frame.h); } Debug.Log("XD"); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.spritesheet = sprites; textureImporter.spritePixelsToUnits = 100; textureImporter.filterMode = FilterMode.Point; TextureImporterSettings tipSeting = new TextureImporterSettings(); textureImporter.ReadTextureSettings(tipSeting); textureImporter.SetTextureSettings(tipSeting); AssetDatabase.ImportAsset(atlasAssetsPath); } }}
效果图:
0 0
- TexturepackerGUI&Unity3d native2d Sprite
- Unity3D 游戏技术开发之Native2D特性研究
- [Unity3D]Unity3D 游戏开发之Unity3D Native2D特性研究
- Unity3D Texture to Sprite
- Unity3D -- 图形 -- Sprite Editor和Sprite Packer
- unity3d Sprite Packer如何用
- Unity3D Sprite(精灵)分割
- unity3d 更换sprite renderer中sprite的图片
- unity3d 更换sprite renderer中sprite的图片
- Unity3d的Sprite Packer用法介绍
- Unity3d的Sprite Packer用法介绍
- Unity3d的Sprite Packer用法介绍
- Unity3D Sprite Manager 2D动画
- TexturePackerGUI软件的使用
- cocos2d+TexturePackerGUI动画制作
- cocos2d+TexturePackerGUI动画制作
- Sprite
- sprite
- C语言第五课练习
- 主机上访问Virtual Box虚拟机上的Tomcat服务器
- PostgreSQL Why checkpointer impact performance so much ? - 3
- 小胖
- ROS(11):莓派Raspberry Pi 小车组装,安装电机和超声波
- TexturepackerGUI&Unity3d native2d Sprite
- sscanf() 的作用
- 软件测试—接口测试简介
- Ubuntu安装后无法连接无线网络
- Java并发编程:Thread类的使用
- Linux简单的文本处理
- HTML中的table里面的元素排序
- 使用VelocityTracker获取在屏幕滑动的速度
- 第七周 项目2-建立链队算法库