在遊戲中,經常會遇到將遊戲信息存儲起來,以便於策劃調配的工作。
利用csv表格行列的特性,可以將數據通過逗號隔開,從而實現表格生成。
具體函數爲
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Reflection;
using UnityEditor;
public class MapGameobjectCreat : MonoBehaviour {
static readonly string TableReaderFolder = "Assets/Tables";
// Use this for initialization
void Start () {
AssetDatabase.Refresh();
Directory.CreateDirectory(TableReaderFolder);
using (StreamWriter writer = File.CreateText(TableReaderFolder + "/"+gameObject.name+".csv"))
{
string csvStr = "障礙物ID,x座標,z座標\n";
foreach (Transform _tran in transform)
{
csvStr += _tran.name + ",";
csvStr += (float)_tran.position.x + ",";
csvStr += (float)_tran.position.z + ",";
csvStr += "\n";
}
writer.Write(csvStr);
}
AssetDatabase.Refresh();
Debug.Log(gameObject.name + "轉表完畢");
}
// Update is called once per frame
void Update () {
}
}
測試工程請見下載地址:https://download.csdn.net/download/Tel17610887670/12040003