解决不能保存的问题

This commit is contained in:
wuwenbo 2025-06-12 15:24:46 +08:00
parent 6d5222755e
commit c93842cacf

View File

@ -35,7 +35,7 @@ namespace Logic.Editor
private GUIStyle _whiteBoxStyle;
private MultilingualData _asset;
private Dictionary<string, uint> _zhStrDict = new Dictionary<string, uint>();
private uint _idIndex;
private int _showIndex = 0;
@ -60,7 +60,7 @@ namespace Logic.Editor
}
private void OnGUI()
{
if (!_asset)
@ -90,7 +90,7 @@ namespace Logic.Editor
GUI.skin.button.wordWrap = true;
_barPosition = EditorGUILayout.BeginScrollView(_barPosition);
EditorGUILayout.BeginHorizontal();
if (InspectorUtils.InspectorButtonWithTextWidth("保存"))
{
@ -98,6 +98,7 @@ namespace Logic.Editor
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
if (InspectorUtils.InspectorButtonWithTextWidth("清空"))
{
_asset.Items.Clear();
@ -113,16 +114,18 @@ namespace Logic.Editor
{
ExcelExportToAsset();
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
if (InspectorUtils.InspectorButtonWithTextWidth("添加字体组"))
{
_asset.FontGroups.Add(new MultilingualFontGroup());
}
EditorGUILayout.EndHorizontal();
var deleteSet = new HashSet<MultilingualFontGroup>();
for (int i = 0; i < _asset.FontGroups.Count; i++)
{
@ -130,7 +133,8 @@ namespace Logic.Editor
if (!ShowFontGroup(_asset.FontGroups[i])) continue;
deleteSet.Add(_asset.FontGroups[i]);
}
foreach (var deleteGroup in deleteSet)_asset.FontGroups.Remove(deleteGroup);
foreach (var deleteGroup in deleteSet) _asset.FontGroups.Remove(deleteGroup);
ShowAllMultilingualItem();
EditorGUILayout.EndScrollView();
}
@ -139,7 +143,7 @@ namespace Logic.Editor
{
int maxIndex = (_asset.Items.Count - 1) / 10;
_showIndex = Mathf.Clamp(_showIndex, 0, maxIndex);
EditorGUILayout.BeginHorizontal();
if (_showIndex > 0 && InspectorUtils.InspectorButtonWithTextWidth("上一页")) _showIndex--;
if (_showIndex < maxIndex && InspectorUtils.InspectorButtonWithTextWidth("下一页")) _showIndex++;
@ -159,11 +163,16 @@ namespace Logic.Editor
InspectorUtils.InspectorTextWidthRich($"<b>ID : {fontGroup.FontID}</b>");
if (InspectorUtils.InspectorButtonWithTextWidth("x")) isDelete = true;
EditorGUILayout.EndHorizontal();
fontGroup.ZHFont = (TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ZHFont, typeof(TMP_FontAsset), false);
fontGroup.TDZHFont = (TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.TDZHFont, typeof(TMP_FontAsset), false);
fontGroup.ENFont = (TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ENFont, typeof(TMP_FontAsset), false);
fontGroup.JPFont = (TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.JPFont, typeof(TMP_FontAsset), false);
fontGroup.KRFont = (TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.KRFont, typeof(TMP_FontAsset), false);
fontGroup.ZHFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ZHFont, typeof(TMP_FontAsset), false);
fontGroup.TDZHFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.TDZHFont, typeof(TMP_FontAsset), false);
fontGroup.ENFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.ENFont, typeof(TMP_FontAsset), false);
fontGroup.JPFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.JPFont, typeof(TMP_FontAsset), false);
fontGroup.KRFont =
(TMP_FontAsset)EditorGUILayout.ObjectField(fontGroup.KRFont, typeof(TMP_FontAsset), false);
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
return isDelete;
@ -183,7 +192,7 @@ namespace Logic.Editor
if (!string.IsNullOrEmpty(item.KR))
InspectorUtils.InspectorTextWidthRich($" <b>韩语:</b> {item.KR}");
var unicode = "";
foreach (var c in item.ZH) unicode+= $"{(int)c:X4} ";
foreach (var c in item.ZH) unicode += $"{(int)c:X4} ";
InspectorUtils.InspectorTextWidthRich($" <b>Unicode</b> {unicode}");
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
@ -220,28 +229,30 @@ namespace Logic.Editor
if (field.Length >= 2 && field.StartsWith("\"") && field.EndsWith("\""))
{
field = field.Substring(1, field.Length - 2) // 去除首尾引号
.Replace("\"\"", "\""); // 将转义引号还原为单个引号
.Replace("\"\"", "\""); // 将转义引号还原为单个引号
}
return field;
}
private void ExcelExportToAsset()
{
GetExcelData();
_asset.RefreshDict();
string context;
using (var reader = new StreamReader("../Tools/MultilingualTxt.txt", Encoding.Default, true))
{
context = reader.ReadToEnd();
}
var lines = context.Split("!@#$%");
foreach (string line in lines)
{
if (string.IsNullOrWhiteSpace(line)) continue; // 跳过空行
string[] cells = line.Split("%$#@!");
if (cells.Length == 0) continue;
var id = uint.Parse(cells[0]);
MultilingualItem item;
if (_asset.ItemDict.TryGetValue(id, out var value)) item = value;
@ -250,7 +261,7 @@ namespace Logic.Editor
item = new MultilingualItem();
_asset.Items.Add(item);
}
item.ID = id;
if (cells.Length >= 2) item.ZH = RemoveCsvQuotes(cells[1]);
if (cells.Length >= 3) item.TDZH = RemoveCsvQuotes(cells[2]);
@ -258,6 +269,10 @@ namespace Logic.Editor
if (cells.Length >= 5) item.JP = RemoveCsvQuotes(cells[4]);
if (cells.Length >= 6) item.KR = RemoveCsvQuotes(cells[5]);
}
EditorUtility.SetDirty(_asset);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
private void AssetExportToExcel()
@ -267,7 +282,7 @@ namespace Logic.Editor
foreach (var item in _asset.Items) _zhStrDict[item.ZH] = item.ID;
if (_asset.Items.Count != 0) _idIndex = _asset.Items[^1].ID + 1;
else _idIndex = 1;
var uiObj = GameObject.Find("UICanvas");
if (!uiObj)
{
@ -295,15 +310,16 @@ namespace Logic.Editor
_idIndex++;
}
}
var path = $"Assets/Resources/DataAssets/";
string[] assetPaths = Directory.GetFiles(path, "*.asset", SearchOption.AllDirectories);
foreach (var assetPath in assetPaths)
{
var asset = AssetDatabase.LoadAssetAtPath<ScriptableObject>(assetPath);
if (!asset) continue;
var targetAsset = GetExportAsset(asset);
TraverseObject(targetAsset);
ScriptableObject newAsset = Object.Instantiate(asset);
TraverseObject(newAsset);
SaveExportAsset(asset.name, newAsset);
}
_asset.RefreshDict();
@ -315,54 +331,58 @@ namespace Logic.Editor
item.ZH = kv.Key;
_asset.Items.Add(item);
}
// 排序 asset.items 保证id从小到大
_asset.Items = _asset.Items.OrderBy(i => i.ID).ToList();
string filePath = "../Tools/MultilingualTxt.txt";
if (!File.Exists(filePath))
{
using (File.Create(filePath)) { } // 立即释放句柄
using (File.Create(filePath))
{
} // 立即释放句柄
}
using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
{
StringBuilder sb = new StringBuilder();
foreach (var item in _asset.Items)
{
sb.Append($"{item.ID}%$#@!{item.ZH}%$#@!{item.TDZH}%$#@!{item.EN}%$#@!{item.JP}%$#@!{item.KR}!@#$%");
sb.Append(
$"{item.ID}%$#@!{item.ZH}%$#@!{item.TDZH}%$#@!{item.EN}%$#@!{item.JP}%$#@!{item.KR}!@#$%");
}
sw.Write(sb.ToString());
}
WriteToExcel();
EditorUtility.SetDirty(_asset);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
private ScriptableObject GetExportAsset(ScriptableObject origin)
private void SaveExportAsset(string name, ScriptableObject target)
{
if (origin == null) return null;
// 创建新实例并深拷贝数据
ScriptableObject newInstance = Object.Instantiate(origin);
if (target == null) return;
// 处理目标路径
string targetPath = $"Assets/Resources/Export/{origin.name}.asset";
string targetPath = $"Assets/Resources/Export/{name}.asset";
if (AssetDatabase.LoadAssetAtPath<ScriptableObject>(targetPath) != null)
{
AssetDatabase.DeleteAsset(targetPath);
}
// 保存新实例
AssetDatabase.CreateAsset(newInstance, targetPath);
AssetDatabase.CreateAsset(target, targetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return newInstance;
}
private void TraverseObject(object asset)
{
if (asset == null) return;
var fields = asset.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (var field in fields)
{
@ -384,10 +404,10 @@ namespace Logic.Editor
}
}
}
var son = field.GetValue(asset);
if (son == null) continue;
// 如果是集合(如 List<T>),遍历元素
if (son is IEnumerable enumerable && !(son is string))
{
@ -403,7 +423,7 @@ namespace Logic.Editor
}
}
}
public void WriteToExcel()
{
var pythonScript = $"../Tools/ExportStringToExcel.py";
@ -424,17 +444,17 @@ namespace Logic.Editor
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd(); // 获取错误信息
process.WaitForExit();
Debug.Log($"Exit Code: {process.ExitCode}"); // 打印退出码
Debug.Log($"Output: {output}");
Debug.Log($"Error: {error}"); // 打印错误信息
}
}
public void GetExcelData()
{
var pythonScript = $"../Tools/PrintExcelString.py";
ProcessStartInfo start = new ProcessStartInfo
{
FileName = "py",
@ -451,7 +471,7 @@ namespace Logic.Editor
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd(); // 获取错误信息
process.WaitForExit();
Debug.Log($"Exit Code: {process.ExitCode}"); // 打印退出码
Debug.Log($"Output: {output}");
Debug.Log($"Error: {error}"); // 打印错误信息