今天翻看了一下之前的博客,发现写了好几篇关于加载json的文章,今天正好有时间就把之前的几篇文章的内容总结一下,然后在添加一些新内容。 在unity里边可以用的加载json的插件有:JsonUtility(unity自带)、LitJson、Newtonsoft等。插件各有优势,我就不一一列举了,我习惯使用unity自带的json插件,目前足够使用。 在创建json文件的时候有两种方法,第一种是手写,第二种是代码生成。 不推荐第一种方法,但是万一用到了一定要注意不要用windows自带的笔记本打开json文件,用windows自带的笔记本保存文件的时候它会默认在文件的开头加入文件的类型,而且是隐藏的。这样在加载json文件的时候会一直报错,用unity自带的插件不会提示错误在哪,用别的插件会提示。出现这个问题后,用notepad++打开,然后在编码上改变一下格式,用记事本保存的utf-8是utf-8-bom,我们改成utf-8就没问题了。 第二种方法是用代码创建json文件,在创建过程中需要注意几个问题:用于创建json的子类需要序列化,属性需要初始化。后边我会用这种方法举例说明。 最后一点,无论什么平台均使用UnityWebRequest加载,这样无论哪个平台,都可以加载出json数据。 下面进行举例说明: 一、代码创建json文件:
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class CreateJson : MonoBehaviour
{
private void Start()
{
ModelInfoRoot modelInfoRoot = new ModelInfoRoot();
if (transform.childCount > 0)
{
for (int i = 0; i < transform.childCount; i++)
{
var vchild = transform.GetChild(i);
ModelInfo info = new ModelInfo();
info.Name = vchild.name;
info.localPos = vchild.localPosition;
info.localRos = vchild.localRotation.eulerAngles;
info.localScale = vchild.localScale;
modelInfoRoot.ModelInfos.Add(info);
}
}
var vbasePath = Path.Combine(Application.streamingAssetsPath, "Config");
DirectoryInfo dir = new DirectoryInfo(vbasePath);
if (!dir.Exists)
dir.Create();
var vconfigPath = Path.Combine(vbasePath, "ModelInfo.json");
FileInfo fileInfo = new FileInfo(vconfigPath);
if (fileInfo.Exists)
fileInfo.Delete();
StreamWriter writer = fileInfo.CreateText();
writer.Write(JsonUtility.ToJson(modelInfoRoot));
writer.Flush();
writer.Dispose();
writer.Close();
AssetDatabase.Refresh();
}
}
[Serializable]
public class ModelInfo
{
public string Name = null;
public Vector3 localPos = Vector3.zero;
public Vector3 localRos = Vector3.zero;
public Vector3 localScale = Vector3.zero;
}
public class ModelInfoRoot
{
public List<ModelInfo> ModelInfos = new List<ModelInfo>();
}
1.1生成的json数据
{
"ModelInfos": [
{
"Name": "Cube",
"localPos": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"localRos": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"localScale": {
"x": 1.0,
"y": 1.0,
"z": 1.0
}
},
{
"Name": "Cube (1)",
"localPos": {
"x": 100.0,
"y": 0.0,
"z": 0.0
},
"localRos": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"localScale": {
"x": 1.0,
"y": 1.0,
"z": 1.0
}
},
{
"Name": "Cube (2)",
"localPos": {
"x": 0.0,
"y": 0.0,
"z": 100.0
},
"localRos": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"localScale": {
"x": 1.0,
"y": 1.0,
"z": 1.0
}
}
]
}
二、加载json文件
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class LoadJson : MonoBehaviour
{
[SerializeField]
private GameObject cube = null;
private void Start()
{
StartCoroutine(IELoadJson());
}
IEnumerator IELoadJson()
{
var vpath = Path.Combine(Path.Combine(Application.streamingAssetsPath, "Config"), "ModelInfo.json");
var www = UnityWebRequest.Get(vpath);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.DataProcessingError || www.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log(www.error);
}
else
{
var vhandler = www.downloadHandler;
ModelInfoRoot modelInfoRoot = JsonUtility.FromJson<ModelInfoRoot>(vhandler.text);
if (modelInfoRoot.ModelInfos.Count > 0)
{
foreach (var v in modelInfoRoot.ModelInfos)
{
var vcube = Instantiate(cube, transform);
vcube.name = v.Name;
vcube.transform.localPosition = v.localPos;
vcube.transform.localEulerAngles = v.localRos;
vcube.transform.localScale = v.localScale;
}
}
}
}
}
|