首页 > 代码库 > unity3D克隆50个游戏对象并输出Json格式的文本

unity3D克隆50个游戏对象并输出Json格式的文本

http://wenku.baidu.com/link?url=tl8q_aj1n-U267XkKtSZISaw6jKJ1woh4CJkDUr1AwEzllSAv7P0r7cawXXSyDVXNf6mjKZaXr15XiX3tKL8xCrMcxtGKpE2P3fSDnvFEdG

using UnityEngine;using System.Collections;using LitJson;using System.Text;using System.IO;public class Prefabs : MonoBehaviour{    public GameObject cube;    public GameObject[] CubePrefabs;    public int i;    public int j = 0;    // Use this for initialization    void Start()    {        //CubePrefabs = new GameObject[50];        GameCopy();    }    // Update is called once per frame    void Update()    {    }    void GameCopy()    {        CubePrefabs = new GameObject[50];        string filepath = Application.dataPath + @"/StreamingAssets/json.txt";        FileInfo t = new FileInfo(filepath);        if (!File.Exists(filepath))        {            File.Delete(filepath);        }        StreamWriter sw = t.CreateText();for (i = 0; i < 50; i++)        {            CubePrefabs[j] = Instantiate(cube, new Vector3(i * 1.5f, 0, 0), Quaternion.identity) as GameObject;            //Debug.Log("Position" + CubePrefabs[j].transform.position);            //Debug.Log("Rotation" +CubePrefabs[j].transform.rotation);            //Debug.Log("Scale" + CubePrefabs[j].transform.localScale);            j++;        }        foreach (GameObject obj in CubePrefabs)        {            StringBuilder sb = new StringBuilder();            JsonWriter writer = new JsonWriter(sb);            writer.WriteObjectStart();            writer.WritePropertyName("name");            writer.Write(obj.name);            writer.WritePropertyName("Position");            writer.WriteArrayStart();            writer.WriteObjectStart();            writer.WritePropertyName("X");            writer.Write(obj.transform.position.x);            writer.WritePropertyName("Y");            writer.Write(obj.transform.position.y);            writer.WritePropertyName("Z");            writer.Write(obj.transform.position.z);            writer.WriteObjectEnd();            writer.WriteArrayEnd();            writer.WritePropertyName("Rotation");            writer.WriteArrayStart();            writer.WriteObjectStart();            writer.WritePropertyName("X");            writer.Write(obj.transform.rotation.x);            writer.WritePropertyName("Y");writer.Write(obj.transform.rotation.y);            writer.WritePropertyName("Z");            writer.Write(obj.transform.rotation.z);            writer.WriteObjectEnd();            writer.WriteArrayEnd();            writer.WritePropertyName("Scale");            writer.WriteArrayStart();            writer.WriteObjectStart();            writer.WritePropertyName("X");            writer.Write(obj.transform.localScale.x);            writer.WritePropertyName("Y");            writer.Write(obj.transform.localScale.y);            writer.WritePropertyName("Z");            writer.Write(obj.transform.localScale.z);            writer.WriteObjectEnd();            writer.WriteArrayEnd();            writer.WriteObjectEnd();            Debug.Log(sb);            sw.WriteLine(sb.ToString());        }        sw.Close();        sw.Dispose();    }

 

unity3D克隆50个游戏对象并输出Json格式的文本