在这里插入代码片UnityEditor将CSV数据转成.asset
Dll需求:
https://download.youkuaiyun.com/download/qq_37369706/12114130
首先创建数据类:
上代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GeneralWordLibrarys : ScriptableObject
{
public List<GeneralWordLibraryItem> GeneralWords = new List<GeneralWordLibraryItem>();
}
[System.Serializable]
public class GeneralWordLibraryItem
{
public int id;
public string keys ;
public string Answer ;
}
创建继承EditorWindow的编辑类:
上代码
using LumenWorks.Framework.IO.Csv;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class ReadCsv : EditorWindow
{
static string readPath = string.Empty;
static string savePath = string.Empty;
static string assetName = string.Empty;
static string csvName = string.Empty;
[MenuItem("CX/ExcelReader/ToAsset")]
static void AddWindow()
{
savePath = "Assets/Resources/Config";
readPath = Application.streamingAssetsPath;
//创建窗口
Rect rect = new Rect(0, 0, 500, 500);
ReadCsv window = (ReadCsv)EditorWindow.GetWindowWithRect(typeof(ReadCsv), rect, true, "ReadCsv");
window.Show();
}
void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.LabelField("CSVReader");
EditorGUILayout.Space();
savePath = EditorGUILayout.TextField("Asset保存路径:", savePath);
readPath = EditorGUILayout.TextField("CSV读取路径:", readPath);
assetName = EditorGUILayout.TextField("Asset文件名:", assetName);
csvName = EditorGUILayout.TextField("CSV文件名:", csvName);
EditorGUILayout.Space();
if (GUILayout.Button("ReadCSV"))
{
if (string.IsNullOrEmpty(assetName))
{
EditorUtility.DisplayDialog("ExcelReader", string.Format("生成失败:文件名为空", ""), "OK");
return;
}
else if (string.IsNullOrEmpty(savePath))
{
EditorUtility.DisplayDialog("ExcelReader", string.Format("生成失败:存储路径为空", ""), "OK");
return;
}
else if (string.IsNullOrEmpty(readPath))
{
EditorUtility.DisplayDialog("ExcelReader", string.Format("生成失败:读取路径为空", ""), "OK");
return;
}
using (CsvReader csv = new CsvReader(new StreamReader(readPath + "/" + csvName + ".csv"), true))
{
// GeneralWordLibrarys generalWordLibrarys = new GeneralWordLibrarys();// ScriptableObject.CreateInstance<GeneralWordLibrarys>();
GeneralWordLibrarys generalWordLibrarys = ScriptableObject.CreateInstance<GeneralWordLibrarys>();
GeneralWordLibraryItem generalWordLibraryItem;
int fieldCount = csv.FieldCount;
string[] headers = csv.GetFieldHeaders();
while (csv.ReadNextRecord())
{
generalWordLibraryItem = new GeneralWordLibraryItem();
for (int i = 0; i < fieldCount; i++)
{
switch (i)
{
case 0:
generalWordLibraryItem.id = int.Parse(csv[i]);
break;
case 1:
generalWordLibraryItem.keys = csv[i];
break;
case 2:
generalWordLibraryItem.Answer = csv[i];
break;
default:
break;
}
}
generalWordLibrarys.GeneralWords.Add(generalWordLibraryItem);
}
for (int i = 0; i < generalWordLibrarys.GeneralWords.Count; i++)
{
Debug.Log(generalWordLibrarys.GeneralWords[i].id+" Key:"+ generalWordLibrarys.GeneralWords[i].keys+" Answer:"+ generalWordLibrarys.GeneralWords[i].Answer);
}
AssetDatabase.CreateAsset(generalWordLibrarys, savePath + "/" + assetName + ".asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
}
切记:存放的文件夹一定要再Resources文件夹内。否则:
数据打印:
生成成功:
总结:.asset是unity的亲儿子,读取速度以及便利度上领先其他格式,譬如json xml 提高了不是一点半点。
加载asset数据:
GeneralWordLibrarys generalWordLibrarys = Resources.Load<GeneralWordLibrarys >("GeneralWordLibrarys ");