1. 程式人生 > >U3D儲存物體位置資訊,讀取物體位置資訊

U3D儲存物體位置資訊,讀取物體位置資訊

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading;
using System.IO;
using System;
using System.Text;
using UnityEngine.UI;

public class pos : MonoBehaviour {
    public Transform[] obj;
    private Vector3 o1;
    private Vector3 o2;
    private Vector3 o3;
    private Vector3 o4;

    
    // Use this for initialization
    void Start () {
        // WriteFileByLine("E:\\", "my_newfile1.txt", "");
        Screen.SetResolution(1920,1080,true);
        if (File.Exists("D:\\my_newfile1.txt"))
            Read("D:\\my_newfile1.txt");
    }
	
	// Update is called once per frame
	void Update () {
        o1 = obj[0].position;
       
        o2 = obj[1].position;
        
        o3 = obj[2].position;
        
        o4 = obj[3].position;
        
    }
    public void WriteFileByLine(string file_path, string file_name, string str_info)
    {
        StreamWriter sw;
        if (!File.Exists(file_path + "//" + file_name))
        {
            sw = File.CreateText(file_path + "//" + file_name);//建立一個用於寫入 UTF-8 編碼的文字
            Debug.Log("檔案建立成功!");
        }
        else
        {
              sw = File.AppendText(file_path + "//" + file_name);//開啟現有 UTF-8 編碼文字檔案以進行讀取
            // Debug.Log(sw.);
           
        }
        sw.WriteLine("1"+":"+str_info);//以行為單位寫入字串
        sw.Close();
        sw.Dispose();//檔案流釋放
    }
    public void save()
    {
        WriteFileByLine("D:\\", "my_newfile1.txt", o1.ToString());
        WriteFileByLine("D:\\", "my_newfile1.txt", o2.ToString());
        WriteFileByLine("D:\\", "my_newfile1.txt", o3.ToString());
        WriteFileByLine("D:\\", "my_newfile1.txt", o4.ToString());
    }
    private void Read(string path)
    {
        StreamReader sr = new StreamReader(path, Encoding.Default);
        string line;

        for (int i = 0; i < 4; i++)
        {
            line = sr.ReadLine();
            //Debug.Log(line + "line");
            string[] s = line.Split(':');
            switch (i)
            {
                case 0:
                    obj[0].position = Parse(s[1]);
                    //Debug.Log(s[1] + "s[1]");
                    break;
                case 1:
                    obj[1].position = Parse(s[1]);
                    break;
                case 2:
                    obj[2].position = Parse(s[1]);
                    //Debug.Log(s[1] + "s[2]");
                    break;
                case 3:
                    obj[3].position = Parse(s[1]);
                    //Debug.Log(s[1] + "s[2]");
                    break;
            }
        }
    }

    public static Vector3 Parse(string s)
    {
        s = s.Replace("(", "").Replace(")", "");
        string[] name = s.Split(',');
        return new Vector3(float.Parse(name[0]), float.Parse(name[1]), float.Parse(name[2]));
    }
}