add 添加excel生成数据结构并缓存本地数据

pull/1/head
zxl 2024-10-30 17:16:56 +08:00
parent 055fbc3aeb
commit 33e8e73d2c
39 changed files with 822 additions and 1146 deletions

View File

@ -0,0 +1,14 @@
using UnityEngine;
using System.Collections.Generic;
[System.Serializable]
public class #CLASSNAME#
{
#CONSTCONTENT#
}
[System.Serializable]
public class #CLASSNAME#s
{
public List<#CLASSNAME#> #LISTNAME# = new List<#CLASSNAME#>();
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d34c7b17da4c75c48979f4719bdd9e21
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -329,7 +329,7 @@ GameObject:
- component: {fileID: 446002385128238495} - component: {fileID: 446002385128238495}
- component: {fileID: 8769046888857752082} - component: {fileID: 8769046888857752082}
m_Layer: 5 m_Layer: 5
m_Name: UIHome m_Name: UIMain
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0

View File

@ -0,0 +1,375 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using Newtonsoft.Json;
using Sirenix.OdinInspector.Editor;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
namespace ZEditor
{
public class ExcelEditor : Editor //OdinEditorWindow
{
private static string folderPath = $"{Application.streamingAssetsPath}/Excel/";
private static string genPath = $"{Application.streamingAssetsPath}/ExcelData/";
private static string genCSPath = $"{Application.dataPath}/DemoGame/GameScript/Hotfix/Generate/Excel2CS/";
private static string templatePath = $"{Application.dataPath}/DemoGame/GameRes/ExcelTemplate.txt";
[MenuItem("Tool/加载表格数据")]
static void Load()
{
string path = $"{Application.streamingAssetsPath}/ExcelData/Test.json";
var allText = File.ReadAllText(path);
var testData = JsonConvert.DeserializeObject<TestDatas>(allText);
Debug.Log($"{testData.TestList[0].Id} , {testData.TestList[0].Subject} , {testData.TestList[0].JX} ");
}
[MenuItem("Tool/表格数据生成")]
static void Gen()
{
DirectoryInfo info = new DirectoryInfo(folderPath);
var fileInfos = info.GetFiles();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Name.Contains(".meta"))
continue;
// Debug.Log($"{fileInfo.FullName} , {fileInfo.Name}");
string fileName = fileInfo.Name.Replace(".xlsx", "");
LoadExcelAndCreateCS(fileInfo.FullName, fileName);
LoadExcelAndCreateData(fileInfo.FullName, fileName);
}
}
/// <summary>
/// 加载excel
/// </summary>
/// <param name="path"></param>
static void LoadExcelAndCreateCS(string path, string fileName)
{
//查找整个文档
Workbook book = new Workbook(path);
//获取一个工作表
Worksheet sheet = book.Worksheets[0];
List<string> zhuShi = new List<string>();
List<string> name = new List<string>();
List<string> typeStr = new List<string>();
List<Type> type = new List<Type>();
for (var i = 0; i < sheet.Cells.Rows.Count; i++)
{
for (var j = 0; j < sheet.Cells.Columns.Count; j++)
{
var cellsColumn = sheet.Cells[i + 2, j + 2].Value;
if (cellsColumn == null)
continue;
string str = cellsColumn.ToString();
if (i == 0)
zhuShi.Add(str);
else if (i == 1)
name.Add(str);
else if (i == 2)
{
typeStr.Add(str);
type.Add(GetType(str));
}
else
break;
}
}
var text = File.ReadAllText(templatePath);
var newName = fileName + "Data";
text = text.Replace("#CLASSNAME#", newName);
StringBuilder sb = new StringBuilder();
for (var i = 0; i < zhuShi.Count; i++)
{
sb.AppendLine("/// <summary>");
sb.AppendLine($"/// {zhuShi[i]}");
sb.AppendLine("/// </summary>");
string str = string.Empty;
if (type[i] == typeof(string[]) || type[i] == typeof(float[]) || type[i] == typeof(bool[]))
{
var s = typeStr[i].Replace("[]", "");
str = $"public List<{s}> {name[i]} = new List<{s}>();";
}
else
{
str = $"public {typeStr[i]} {name[i]};";
}
sb.AppendLine(str);
}
text = text.Replace("#CONSTCONTENT#", sb.ToString());
text = text.Replace("#LISTNAME#", $"{fileName}List");
Debug.Log(text);
string filePath = genCSPath + "/" + newName + ".cs";
if (!File.Exists(filePath))
{
File.Create(filePath).Dispose();
}
File.WriteAllText(filePath, text);
}
static void LoadExcelAndCreateData(string path, string fileName)
{
//查找整个文档
Workbook book = new Workbook(path);
//获取一个工作表
Worksheet sheet = book.Worksheets[0];
Debug.Log($"{sheet.Cells.Rows.Count}行 , {sheet.Cells.Columns.Count}列");
// List<string> zhuShi = new List<string>();
// List<string> name = new List<string>();
// List<string> type = new List<string>();
DataTable table = new DataTable();
// table.Columns
for (var i = 0; i < sheet.Cells.Rows.Count + 2; i++)
{
List<object> list = new List<object>();
for (var j = 0; j < sheet.Cells.Columns.Count; j++)
{
if (i == 4)
{
string name = sheet.Cells[3, j + 2].Value.ToString();
var typeName = sheet.Cells[4, j + 2].Value.ToString();
Type type = GetType(typeName);
// Debug.Log($"{name} {typeName} {type}");
table.Columns.Add(name, type);
}
else if (i > 4)
{
var typeName = sheet.Cells[4, j + 2].Value.ToString();
Type type = GetType(typeName);
var cellsColumn = sheet.Cells[i, j + 2].Value;
var cons = cellsColumn.ToString();
if (type == typeof(string))
{
list.Add(cons);
}
else if (type == typeof(int))
{
int id = int.Parse(cons);
list.Add(id);
}
else if (type == typeof(bool))
{
var b = bool.Parse(cons);
list.Add(b);
}
else if (type == typeof(float))
{
var b = float.Parse(cons);
list.Add(b);
}
// array
else if (type == typeof(string[]))
{
var huanList = ZhuanHuanList(cons);
list.Add(huanList.ToArray());
}
else if (type == typeof(float[]))
{
var huanList = ZhuanHuanList(cons);
var tmp = new List<float>();
foreach (var s in huanList)
{
var f = float.Parse(s);
tmp.Add(f);
}
list.Add(tmp.ToArray());
}
else if (type == typeof(int[]))
{
var huanList = ZhuanHuanList(cons);
var tmp = new List<int>();
foreach (var s in huanList)
{
var f = int.Parse(s);
tmp.Add(f);
}
list.Add(tmp.ToArray());
}
else if (type == typeof(bool[]))
{
var huanList = ZhuanHuanList(cons);
var tmp = new List<bool>();
foreach (var s in huanList)
{
var f = bool.Parse(s);
tmp.Add(f);
}
list.Add(tmp.ToArray());
}
// Vector3 暂时不添加,因为不受支持,后续想办法处理一下
// else if (type == typeof(Vector3) || type == typeof(Vector3[]))
// {
// var huanList = ZhuanHuanList_Vector3(cons);
// if (huanList.Count > 1)
// {
// list.Add(huanList.ToArray());
// }
// else if (huanList.Count == 1)
// {
// list.Add(huanList[0]);
// }
// }
else
{
list.Add(cellsColumn);
}
}
}
if (i > 4)
{
var objects = list.ToArray();
table.Rows.Add(objects);
}
}
var json = JsonConvert.SerializeObject(table);
string newStr = "{" + $"\"{fileName}List\":" + json + "}";
Debug.Log(newStr);
string filePath = genPath + "/" + fileName + ".json";
if (!File.Exists(filePath))
{
File.Create(filePath).Dispose();
}
File.WriteAllText(filePath, newStr);
AssetDatabase.Refresh();
}
static List<string> ZhuanHuanList(string cons)
{
if (cons.Contains(";"))
{
cons = cons.Replace("\n", "");
var strings = cons.Split(";");
var tmp = strings.ToList();
tmp.RemoveAt(tmp.Count - 1);
return tmp;
}
else if (cons.Contains(","))
{
cons = cons.Replace("\n", "");
var strings = cons.Split(",");
var tmp = strings.ToList();
tmp.RemoveAt(tmp.Count - 1);
return tmp;
}
else
{
List<string> tmp = new List<string>();
tmp.Add(cons);
return tmp;
}
}
static List<Vector3> ZhuanHuanList_Vector3(string cons)
{
List<Vector3> vecs = new List<Vector3>();
if (cons.Contains(";"))
{
cons = cons.Replace("\n", "");
var strings = cons.Split(";");
var tmp = strings.ToList();
tmp.RemoveAt(tmp.Count - 1);
foreach (var s in tmp)
{
var vec = GenVec(s);
vecs.Add(vec);
}
return vecs;
}
else
{
var vec = GenVec(cons);
vecs.Add(vec);
return vecs;
}
Vector3 GenVec(string str)
{
str = str.Replace("\n", "");
var strings = str.Split(",");
List<float> tmp = new List<float>();
foreach (var s in strings)
{
var f = float.Parse(s);
tmp.Add(f);
}
Vector3 vec = new Vector3(tmp[0], tmp[1], tmp[2]);
return vec;
}
}
static Type GetType(string name)
{
switch (name)
{
case "int":
return typeof(int);
break;
case "int[]":
return typeof(int[]);
break;
case "bool":
return typeof(bool);
break;
case "bool[]":
return typeof(bool[]);
break;
case "string":
return typeof(string);
break;
case "Vector3":
return typeof(Vector3);
break;
case "string[]":
return typeof(string[]);
break;
case "float":
return typeof(float);
break;
case "float[]":
return typeof(float[]);
break;
case "Vector3[]":
return typeof(Vector3[]);
break;
default:
throw new ArgumentException();
break;
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c0f7a762595341c9903ed816bdab0e7b
timeCreated: 1730191864

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8e614b932c9847d584e22e35f1ca60ef
timeCreated: 1730272067

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
namespace ZC
{
public class ExcelManager : MonoBehaviour
{
private void Awake()
{
// string path = $"{Application.streamingAssetsPath}/ExcelData/Test.json";
// var allText = File.ReadAllText(path);
// var testData = JsonConvert.DeserializeObject<TestDatas>(allText);
// Debug.Log($"{testData.List[0].Id} , {testData.List[0].Subject} , {testData.List[0].JX} ");
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f50b5ee5d5e84f6780c7b440e7e6cf18
timeCreated: 1730272083

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f2034d71b6d48cb45906d162e187b30a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 523c9e0b7b2d4e546b15d22e4da8cb94
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 24edd0931679a9143970e82c3aa74c8b
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 5ca8614a0f234bc4b92385c81ef8373e
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 3a6ff3f1f11d48a4fb36f6e8345a93fc
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 4659b9a410a024d4095dcdf07dcd6dde
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: dbd2eb7d7c7d2f9458aa798021e3df75
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: ed6352d482e91154e9867805268c1d14
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: fc1d335ee96eb7149a1289b6c3d0edb7
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: faf8bfe7354222c408d25a7e4b890fe8
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: e567bc0216ae1c546ad69d0095179fe5
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 01bda0cc5c6460649975d6f0ddaef086
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9bb1b25d2442f264194b151548bac54e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: dffa197b1a69704408cf9e2b16162c0e guid: 11e3dd5e103955b47b0fab3132237b6d
PrefabImporter: DefaultImporter:
externalObjects: {} externalObjects: {}
userData: userData:
assetBundleName: assetBundleName:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d30d01f0bd7bf294e91bfa5b8004da10
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1 @@
{"TestList":[{"Id":1,"Subject":"题目1","Option":["A、选项A","B、选项B"],"TrueOption":["A"],"Desc":"无","JX":"无"},{"Id":2,"Subject":"题目2","Option":["A、选项A","B、选项B"],"TrueOption":["B"],"Desc":"无","JX":"无"},{"Id":3,"Subject":"题目3","Option":["A、选项A","B、选项B"],"TrueOption":["A"],"Desc":"无","JX":"无"},{"Id":4,"Subject":"题目4","Option":["A、选项A","B、选项B"],"TrueOption":["B"],"Desc":"无","JX":"无"}]}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ba90345dd6336734c8f90a93c7b98338
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: