码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-09-10 15:36:50      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   art   

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格式的文本

标签:style   blog   http   color   os   io   ar   for   art   

原文地址:http://www.cnblogs.com/123ing/p/3964316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!