Up自制Unity預(yù)制腳本dataStore.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
public class dataStore:MonoBehaviour
{
? ? public List<string> goSave = new List<string>();
? ? public List<string> goLoad = new List<string>();
? ? public string path = @"e:\testSave.txt";
? ? void Start()
? ? {
? ? ? ??
? ? }
? ? void Update()
? ? {
? ? ? ? if (Input.GetKeyDown(KeyCode.F8))
? ? ? ? {
? ? ? ? ? ? collectData();
? ? ? ? ? ? saveData();
? ? ? ? ? ? //存盤操作
? ? ? ? }
? ? ? ? if (Input.GetKeyDown(KeyCode.F9))
? ? ? ? {
? ? ? ? ? ? goLoad.Clear();
? ? ? ? ? ? loadData();
? ? ? ? ? ? resetPicPosition();? ? ? ?
? ? ? ? ? ? //讀盤操作
? ? ? ? }
? ? }
? ? void collectData()
? ? {
? ? ? ? //收集圖片位置以及旋轉(zhuǎn)和是否有重力的數(shù)據(jù)
? ? ? ? int picNum = transform.childCount;
? ? ? ? goSave.Clear();
? ? ? ? for (int i = 0; i < picNum; i++)
? ? ? ? {
? ? ? ? ? ? goSave.Add(transform.GetChild(i).transform.name+";"+ transform.GetChild(i).transform.position+";"+ transform.GetChild(i).transform.eulerAngles+";"+ transform.GetChild(i).GetComponent<Rigidbody>().useGravity);? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? }
? ? }
? ? void saveData()
? ? {? ? ? ??
? ? ? ? //存盤操作
? ? ? ? if (File.Exists(path))
? ? ? ? {
? ? ? ? ? ? File.Delete(path);
? ? ? ? }
? ? ? ? using(FileStream fs_write = File.Create(path))
? ? ? ? {
? ? ? ? ? ? StreamWriter sw = new StreamWriter(fs_write, Encoding.UTF8);
? ? ? ? ? ? int picNum = transform.childCount;
? ? ? ? ? ? for(int i=0; i<picNum; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sw.WriteLine(goSave[i]);
? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? sw.Close();
? ? ? ? }
? ? }
? ? void loadData()
? ? {
? ? ? ? //讀盤操作
? ? ? ? FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
? ? ? ? StreamReader sr = new StreamReader(fs);
? ? ? ? string str = "";
? ? ? ? while (str != null)
? ? ? ? {
? ? ? ? ? ? str = sr.ReadLine();
? ? ? ? ? ?
? ? ? ? ? ? if (str != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? goLoad.Add(str);
? ? ? ? ? ? }
? ? ? ? ? ? else?
? ? ? ? ? ? {
? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? sr.Close();
? ? }
? ? void resetPicPosition()
? ? {
? ? ? ? //重置圖片位置為保存的位置
? ? ? ? foreach(string i in goLoad)
? ? ? ? {
? ? ? ? ? ? string[] split = i.Split(new char[] { ';' });
? ? ? ? ? ? transform.Find(split[0]).position = StringToVector3(split[1]);
? ? ? ? ? ? transform.Find(split[0]).eulerAngles = StringToVector3(split[2]);
? ? ? ? ? ? transform.Find(split[0]).GetComponent<Rigidbody>().useGravity = bool.Parse(split[3]);
? ? ? ? }? ? ? ??
? ? }
? ? public static Vector3 StringToVector3(string temp)
? ? {
? ? ? ? //字符串轉(zhuǎn)為向量
? ? ? ? if (temp.StartsWith("(") && temp.EndsWith(")"))
? ? ? ? {
? ? ? ? ? ? temp = temp.Substring(1, temp.Length - 2);
? ? ? ? }? ? ? ? ? ? ? ??
? ? ? ? string[] sArray = temp.Split(',');
? ? ? ? ? ? ? ??
? ? ? ? Vector3 result = new Vector3(float.Parse(sArray[0]), float.Parse(sArray[1]), float.Parse(sArray[2]));
? ? ? ? return result;
? ? }
}