您的位置:首页 > 移动开发 > Unity3D

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

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

效果图如下。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐