首页 > 代码库 > 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格式的文本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。