UnityEditor JSONへのCSVデータ
のDLLの需要:
CSV
JSON
をダウンロードする:
推奨アドレスhttps://www.nuget.org/
データカテゴリを作成します。
コードを指示
#region 问题项
/// <summary>
/// 问题项
/// </summary>
public class QuestionConfig
{
/// <summary>
/// id
/// </summary>
public int ID { get; set; }
/// <summary>
/// 大纲视图 OutlineView
/// </summary>
public int Outline { get; set; }
/// <summary>
/// 问题
/// </summary>
public string QuestionWords { get; set; }
/// <summary>
/// 回答
/// </summary>
public string AnswerWords { get; set; }
/// <summary>
/// 主支线 QuestionType
/// </summary>
public int QuestionTp { get; set; }
/// <summary>
/// 关键词
/// </summary>
public List<string> KeyWords { get; set; } = new List<string>();
/// <summary>
/// 引导内容
/// </summary>
public string GuideContent { get; set; }
}
public class QuestionConfigChart
{
public List<QuestionConfig> _QuestionConfigsChart { get; set; } = new List<QuestionConfig>();
}
エディタを読む:
直接コードに
using Excel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEditor;
using UnityEngine;
using LumenWorks.Framework.IO.Csv;
using Newtonsoft.Json;
/// <summary>
/// @author: DaHu
/// @date: 2020-01-16 15:30:06
/// @brief: Excela
/// @describe: 读取CSV
/// </summary>
/// portName=任务卡ID 8119090300000016
public class ReadExcel : EditorWindow
{
static string readPath = string.Empty;
static string savePath = string.Empty;
static string sheetName = string.Empty;
static string excelName = string.Empty;
static QuestionConfigChart chart = new QuestionConfigChart();
[MenuItem("CX/ExcelReader/ToJson")]
static void AddWindow()
{
savePath = Application.streamingAssetsPath;
readPath = Application.streamingAssetsPath;
//创建窗口
Rect rect = new Rect(0, 0, 500, 500);
ReadExcel window = (ReadExcel)EditorWindow.GetWindowWithRect(typeof(ReadExcel), rect, true, "ExcelReader");
window.Show();
}
void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.LabelField("ExcelReader");
EditorGUILayout.Space();
savePath = EditorGUILayout.TextField("Json保存路径:", savePath);
readPath = EditorGUILayout.TextField("Excel读取路径:", readPath);
sheetName = EditorGUILayout.TextField("Json文件名:", sheetName);
excelName = EditorGUILayout.TextField("Excel文件名:", excelName);
EditorGUILayout.Space();
if (GUILayout.Button("ReadExcel"))
{
if (string.IsNullOrEmpty(sheetName))
{
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 + "/" + excelName + ".csv"), true))
{
QuestionConfig questionConfig;
int fieldCount = csv.FieldCount;
string[] headers = csv.GetFieldHeaders();
while (csv.ReadNextRecord())
{
questionConfig = new QuestionConfig();
for (int i = 0; i < fieldCount; i++)
{
switch (i)
{
case 0:
questionConfig.ID = int.Parse(csv[i]);
break;
case 1:
questionConfig.Outline = int.Parse(csv[i]);
break;
case 2:
questionConfig.QuestionWords = csv[i];
break;
case 3:
questionConfig.AnswerWords = csv[i];
break;
case 4:
questionConfig.QuestionTp = int.Parse(csv[i]);
break;
case 5:
string[] strs = csv[i].Split('&');
for (int j = 0; j < strs.Length; j++)
{
questionConfig.KeyWords.Add(strs[j]);
}
break;
case 6:
questionConfig.GuideContent = csv[i];
break;
default:
break;
}
}
chart._QuestionConfigsChart.Add(questionConfig);
}
string strss = JsonConvert.SerializeObject(chart);
if (!File.Exists(savePath + "/" + sheetName + "json"))
{
File.Create(savePath + "/" + sheetName + "json");
}
File.WriteAllText(savePath + "/" + sheetName + ".json", strss);
}
}
}
}
完全なショーを作成します:
オーバー!!!