jison解析Cube信息 存入又读取出来
就只挂这个脚本:所有脚本中
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 [System.Serializable]//可修改的数据 5 public class Objectcube { 6 public Vector3 pos; 7 public Vector3 qua; 8 public Vector3 sca; 9 public string name; 10 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 [System.Serializable] 5 public class ListObject { 6 public List<Objectcube> list = new List<Objectcube>(); 7 8 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEditor; 5 using System.IO; 6 7 public class bianji { 8 [MenuItem("tool/chun")]//不运行的时候点击 让cube的信息存如json里面 9 public static void chun() 10 { 11 string path = Application.dataPath + "/json.txt"; 12 GameObject[] go = Selection.gameObjects; 13 ListObject objlist = new ListObject(); 14 for (int i = 0; i < go.Length; i++) 15 { 16 Objectcube g = new Objectcube(); 17 g.pos = go[i].transform.position; 18 g.qua = go[i].transform.eulerAngles; 19 g.sca = go[i].transform.localScale; 20 g.name = go[i].name; 21 objlist.list.Add(g); 22 } 23 string str = JsonUtility.ToJson(objlist);//存入Json文档 24 25 File.WriteAllText(path,str); 26 AssetDatabase.Refresh(); 27 } 28 29 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using System.IO; 4 using UnityEngine; 5 using System; 6 public class duqu : MonoBehaviour { 7 8 // Use this for initialization 9 void Start () { 10 string path = Application.dataPath + "/json.txt"; 11 string s = File.ReadAllText(path); 12 ListObject obj=JsonUtility.FromJson<ListObject>(s);//读取json里面的文档 13 for (int i = 0; i < obj.list.Count; i++) 14 { 15 GameObject go = GameObject.CreatePrimitive((PrimitiveType)Enum.Parse(typeof(PrimitiveType),obj.list[i].name)); 16 go.transform.position = obj.list[i].pos; 17 go.transform.eulerAngles = obj.list[i].qua; 18 go.transform.localScale = obj.list[i].sca; 19 } 20 21 } 22 }