zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    在LinuxMint 17 MATE中安装NVIDIA显卡驱动
    如何在VeryCD中下载资源
    创建多个Dialog时,namespace冲突问题的解决 -- 基于QT 5.2
    Qt 5.2中编译加载MySQL数据库驱动问题的总结
    Python入门 -- 001
    Qt 入门 ---- 布局管理
    Qt 入门 ---- 如何在程序窗口显示图片?
    Redis 教程笔记
    Python pip 报错
    Python手动安装 package
  • 原文地址:https://www.cnblogs.com/123ing/p/3964316.html
Copyright © 2011-2022 走看看