unity3D学习【功能实现】之十一:通过Json读取地图01
2016-08-17 10:28
537 查看
首先需要用到一款软件Tiledmap网上都可以下载的到,主要用它来画图,然后导出为json格式。注意要把原图片和json在同一文件夹,生成json的时候不能再改变原图的文件夹位置了。
我生成的是24*24像素的图片。所以在unity的 project-里的 Resources的tiled.png图片也Multiple切成24*24。
那么图片材料和json准备完毕。
接下来就是接受json数据,然后根据接受的数据读取图片块的各个位置组成一张地图,原理就是这样的。
1,首先建立一个tiledmap.cs类来接受json数据,也就是解析json
这个自己写的话太累,要对应的。http://sandbox.runjs.cn/show/nhuozgst
网上网站很多,直接生成c#类,可以直接使用。不过有些生成会有问题,像
public Properties properties { get; set; }这个就重复生成,你需要删除一个。
visible这个应该是bool类型的,生成的时候好像是string类型的,要出错,手动改回来
2,然后建个空的gaeobject挂一个GMmanager脚本,用来管理和生成地图。
那么主要代码就在 CreateMap这个函数里面了。别忘了把LitJson.dll加进来并引用下。
3,CreateMap函数如下
思路是这样的。先把图块都加载进来,放到temsprites组里,不过都在内存中,还没办法显示出来。
然后通过 tiledmap map = JsonMapper.ToObject(tmp);所有的数据都在map这个对象里面了。在是通过一个循环
把图片都挂载到GameObject go的image组建里,确切的说应该是image组建的sprite变量里。也就是image.sprite。
注意layers[0],因为用tiledmap生成的图层只有一层,然后它生成的layers是个数组,所以我直接用0了。如果有多个图层的话用for或者foreach都可以。
go.transform.SetParent (objparent.transform, false);这个是设置一个父对象,好控制你整张图的位置
申明一个public GameObject objparent
af98
;我是在canvas下建的Panel(改名为Image)下的Panel(改名为map),把map拖到objparent,所以map为父对象了。Panel
效果图如下。
我生成的是24*24像素的图片。所以在unity的 project-里的 Resources的tiled.png图片也Multiple切成24*24。
那么图片材料和json准备完毕。
接下来就是接受json数据,然后根据接受的数据读取图片块的各个位置组成一张地图,原理就是这样的。
1,首先建立一个tiledmap.cs类来接受json数据,也就是解析json
using UnityEngine; using System.Collections.Generic; public class LayersItem { public List <int > data { get; set; } public int height { get; set; } public string name { get; set; } public int opacity { get; set; } public string type { get; set; } public bool visible { get; set; } public int width { get; set; } public int x { get; set; } public int y { get; set; } } public class Properties { } public class TilesetsItem { public int firstgid { get; set; } public string image { get; set; } public int imageheight { get; set; } public int imagewidth { get; set; } public int margin { get; set; } public string name { get; set; } public Properties properties { get; set; } public int spacing { get; set; } public int tileheight { get; set; } public int tilewidth { get; set; } } public class tiledmap { public int height { get; set; } public List <LayersItem > layers { get; set; } public string orientation { get; set; } public Properties properties { get; set; } public string renderorder { get; set; } public int tileheight { get; set; } public List <TilesetsItem > tilesets { get; set; } public int tilewidth { get; set; } public int version { get; set; } public int width { get; set; } }
这个自己写的话太累,要对应的。http://sandbox.runjs.cn/show/nhuozgst
网上网站很多,直接生成c#类,可以直接使用。不过有些生成会有问题,像
public Properties properties { get; set; }这个就重复生成,你需要删除一个。
visible这个应该是bool类型的,生成的时候好像是string类型的,要出错,手动改回来
2,然后建个空的gaeobject挂一个GMmanager脚本,用来管理和生成地图。
using LitJson; public class GMmanager : MonoBehaviour { public GameObject objparent; void Start () { CreateMap (); }
那么主要代码就在 CreateMap这个函数里面了。别忘了把LitJson.dll加进来并引用下。
3,CreateMap函数如下
Sprite[] temsprites = Resources.LoadAll<Sprite> ("tiled"); UnityEngine.TextAsset text = Resources.Load ("myJson")as TextAsset; string tmp = text.text; //JsonData jsonData3 = JsonMapper.ToObject(tmp); tiledmap map = JsonMapper.ToObject<tiledmap>(tmp); int tw = map.tilewidth;//图块大小24像素,下面一样 int th = map.tileheight; int sw = map.width; int sh = map.height;//这个我记得是25块,这样25*24=600,因为我想做的是800*600的,那800可以无限//扩大 //Debug.Log (map.layers[0].data.Count); int x = 0,y=0; for(int i=0;i<map.layers[0].data.Count;i++){ int imageID = map.layers [0].data [i]; if (imageID != 0) { imageID -= 1; GameObject go = new GameObject (imageID.ToString (), typeof(RectTransform)); go.transform.SetParent (objparent.transform, false); Image image = go.AddComponent<Image> (); image.rectTransform.pivot = Vector2.zero; image.rectTransform.sizeDelta = new UnityEngine.Vector2 (tw,th); image.sprite = temsprites [imageID]; image.transform.localPosition = new Vector2 (x * tw, (sh - y - 1) * th); } x++; if(x>=sw){ //图片换行 x = 0; y++; } }
思路是这样的。先把图块都加载进来,放到temsprites组里,不过都在内存中,还没办法显示出来。
然后通过 tiledmap map = JsonMapper.ToObject(tmp);所有的数据都在map这个对象里面了。在是通过一个循环
for(int i=0;i<map.layers[0].data.Count;i++)
把图片都挂载到GameObject go的image组建里,确切的说应该是image组建的sprite变量里。也就是image.sprite。
注意layers[0],因为用tiledmap生成的图层只有一层,然后它生成的layers是个数组,所以我直接用0了。如果有多个图层的话用for或者foreach都可以。
go.transform.SetParent (objparent.transform, false);这个是设置一个父对象,好控制你整张图的位置
申明一个public GameObject objparent
af98
;我是在canvas下建的Panel(改名为Image)下的Panel(改名为map),把map拖到objparent,所以map为父对象了。Panel
效果图如下。
相关文章推荐
- 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能
- Unity3d之ScrollView实现图片浏览切换功能----折磨的学习
- 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能 base64加密
- 【Java EE 学习 76 下】【数据采集系统第八天】【通过AOP实现日志管理】【日志管理功能分析和初步实现】
- 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能
- 2013--01--01新年的第一天乖乖来教室学习 新的一年加油! 1 修改 HTTP 输出 (myModule) 2 URL重写 3 实现本网站图片保护功能
- Android多媒体学习十一:实现仿百度图片查看功能
- ios 本地通知(UILocalNotification)的学习和使用(通过本地通知实现提醒功能)
- 【iOS开发-54】案例学习:通过UIScrollView的缩放图片功能练习代理模式的具体实现
- json学习笔记_01_json语法基本格式并s会用java读取json
- Unity3d之ScrollView实现图片浏览切换功能----折磨的学习
- yii 学习笔记十一、通过模型实现数据添加
- [转贴]JAVA 百度地图SDK地图学习——实现定位功能
- 本地通知(UILocalNotification)的学习和使用(通过本地通知实现提醒功能)
- 【第7篇】通过json-lib去读取百度api地图的地方信息
- Android多媒体学习十一:实现仿百度图片查看功能
- Android多媒体学习十一:实现仿百度图片查看功能
- 通过codehaus来实现json写入文件和读取文件成json对象
- 百度地图SDK地图学习——实现定位功能
- 【学习ios之路:UI系列】点击更换头像实现从相册读取照片和拍照两种功能