完成配置表常量工具;

pull/1/head
zhangxl 2024-07-22 15:24:17 +08:00
parent 68274ad87c
commit 379cecbb29
6 changed files with 81 additions and 34 deletions

View File

@ -0,0 +1,5 @@
using UnityEngine;
public static class #CLASSNAME#
{
#CONSTCONTENT#
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f8aa6a7854744a2599fcb988dcba2871
timeCreated: 1721631976

View File

@ -39,8 +39,8 @@ namespace ZEditor
Bridging(ref sb, resPath); Bridging(ref sb, resPath);
var readAllText = File.ReadAllText(template); var readAllText = File.ReadAllText(template);
var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", "AssetConst"); var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", csName);
var loaderContent = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", "LoaderAssetConst"); var loaderContent = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", loaderCsName);
if (!File.Exists(path)) if (!File.Exists(path))
{ {
File.Create(path).Dispose(); File.Create(path).Dispose();

View File

@ -22,30 +22,6 @@ namespace ZEditor
Run(); Run();
string folderPath = @"Assets/DemoGame/GameRes/Config/JSON"; string folderPath = @"Assets/DemoGame/GameRes/Config/JSON";
// // 获取文件夹下所有文件的完整路径
// string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
//
// Tables tables;
// // 输出文件路径
// foreach (string file in files)
// {
// if (file.Contains(".meta"))
// {
// continue;
// }
//
// tables = new cfg.Tables(JX);
// }
// string gameConfDir = @"Assets/DemoGame/GameRes/Config/JSON"; // 替换为gen.bat中outputDataDir指向的目录
// var tables = new cfg.Tables(file => JSON.Parse(File.ReadAllText($"{gameConfDir}/{file}.json")));
// StringBuilder sb = new StringBuilder();
// foreach (var item in tables.TbItem.DataMap)
// {
// string name = $"{item.Key}_{item.Value.Name}";
// sb.AppendLine($"public const int {name} = {item.Key};");
// }
} }
static void Run() static void Run()
@ -78,6 +54,45 @@ namespace ZEditor
{ {
UnityEngine.Debug.LogError(errors); UnityEngine.Debug.LogError(errors);
} }
Debug.Log("配置文件生成成功,开始生成常量脚本");
// 自动生成脚本
GenConst();
}
private const string fileName = "ConfigConst";
private const string folderPath = "Assets/DemoGame/GameScript/Hotfix/Const";
public static string template = "Assets/DemoGame/GameRes/ConfigConstTemplate.txt";
// [MenuItem("Tool/单独生成配置表常量脚本")]
static void GenConst()
{
string path = $"{folderPath}/{fileName}.cs";
StringBuilder sb = new StringBuilder();
var tables = new cfg.Tables(JX);
// item_tbitem
foreach (var item in tables.TbItem.DataList)
{
sb.AppendLine($"public const int item_tbitem_{item.Id}_{item.Name}_{item.Desc} = {item.Id};");
}
// tbplayer
foreach (var playerData in tables.TBPlayer.DataList)
{
sb.AppendLine($"public const int tbplayer_{playerData.Id}_{playerData.Name}_{playerData.Desc} = {playerData.Id};");
}
var readAllText = File.ReadAllText(template);
var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/").Replace("#CLASSNAME#", fileName);
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
File.WriteAllText(path, content);
Debug.Log("生成配置表常量脚本完成!");
} }
private static JSONNode JX(string arg) private static JSONNode JX(string arg)
@ -95,14 +110,9 @@ namespace ZEditor
break; break;
} }
var textAsset = ResourcesLocalComponent.Instance.LoadAssetSync<TextAsset>(path); // var textAsset = ResourcesLocalComponent.Instance.LoadAssetSync<TextAsset>(path);
return JSON.Parse(textAsset.text); var textAsset = File.ReadAllText(path);
return JSON.Parse(textAsset);
} }
// [MenuItem("Tool/运行场景")]
// public static void Run()
// {
// EditorApplication.isPlaying = true;
// }
} }
} }

View File

@ -0,0 +1,18 @@
using UnityEngine;
public static class ConfigConst
{
public const int item_tbitem_10000__ = 10000;
public const int item_tbitem_10001__ = 10001;
public const int item_tbitem_10002__ = 10002;
public const int item_tbitem_10003__ = 10003;
public const int item_tbitem_10004__ = 10004;
public const int item_tbitem_10005__ = 10005;
public const int item_tbitem_10006__ = 10006;
public const int item_tbitem_10007__ = 10007;
public const int item_tbitem_10008__ = 10008;
public const int item_tbitem_10009__ = 10009;
public const int tbplayer_10000__ = 10000;
public const int tbplayer_10001__ = 10001;
public const int tbplayer_10002__ = 10002;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 77c106f464c5948499e3b8e1784df08c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: