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(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。