TH1/Unity/Assets/Editor/TechToTechAtomDataAssetFiller.cs
2026-06-10 11:58:18 +08:00

162 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using Logic.Action;
using Unity.VisualScripting;
public static class DeepCopier
{
/// <summary>
/// 对一个Unity可序列化的对象进行深拷贝
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="source">源对象</p>
/// <returns>一个全新的对象副本</returns>
public static T DeepCopy<T>(T source)
{
// 1. 将对象序列化为JSON字符串
string json = JsonUtility.ToJson(source);
// 2. 将JSON字符串反序列化为一个全新的对象
return JsonUtility.FromJson<T>(json);
}
}
public class TechToTechAtomDataAssetFiller
{
[MenuItem("填表脚本/Tech所有Action转化为TechAtom")]
public static void FillMissingCivSprites()
{
string assetPath = "Assets/BundleResources/DataAssets/TechDataAssets.asset";
var asset = AssetDatabase.LoadAssetAtPath<TechDataAssets>(assetPath);
string actionAssetPath = "Assets/BundleResources/DataAssets/ActionDataAssets.asset";
var actionAsset = AssetDatabase.LoadAssetAtPath<ActionDataAssets>(actionAssetPath);
if (asset == null)
{
Debug.LogError("❌ 找不到 UnitTypeDataAssets.asset请确认路径正确");
return;
}
int addCount = 0;
foreach (var techInfo in asset.TechList)
{
var actionList = techInfo.GetActionList();
foreach (var action in actionList)
{
if (action.ActionType is CommonActionType.UnitSkill or CommonActionType.UnitAction
or CommonActionType.TrainUnit or CommonActionType.BuildWonder or CommonActionType.StartWonder
or CommonActionType.Gain or CommonActionType.Build or CommonActionType.GridMisc)
{
//Debug.Log(action.SkillType);
var str = "";
if(action.ActionType is CommonActionType.UnitSkill)
str = "UnitSkill" + action.SkillType.ToString();
if(action.ActionType is CommonActionType.UnitAction)
str = action.UnitActionType.ToString();
if(action.ActionType is CommonActionType.TrainUnit)
str = "TrainUnit" + action.UnitType.ToString();
if(action.ActionType is CommonActionType.StartWonder)
str = "StartWonder" + action.WonderType.ToString();
if(action.ActionType is CommonActionType.BuildWonder)
str = "BuildWonder" + action.WonderType.ToString();
if(action.ActionType is CommonActionType.Gain)
str = "Gain" + action.ResourceType.ToString();
if(action.ActionType is CommonActionType.Build)
str = "Build" + action.ResourceType.ToString();
if(action.ActionType is CommonActionType.GridMisc)
str = action.GridMiscActionType.ToString();
var has = false;
foreach(var techAtom in asset.TechAtomList)
if (techAtom.TechAtom.ToString() == str)
{
has = true;
break;
}
if (!has)
{
var info = new TechAtomInfo();
if (TechAtom.TryParse(str, out info.TechAtom))
{
foreach (var actionInfo in actionAsset.ActionList)
{
if (actionInfo.ActionId == action)
{
info.TechAtomName = actionInfo.ActionName;
info.Desc = actionInfo.NeedTechDesc ? actionInfo.TechDesc : actionInfo.Desc;
}
}
asset.TechAtomList.Add(info);
addCount++;
}
}
else
{
foreach(var techAtomInfo in asset.TechAtomList)
if (techAtomInfo.TechAtom.ToString() == str)
{
foreach (var actionInfo in actionAsset.ActionList)
{
if (actionInfo.ActionId == action)
{
if(techAtomInfo.IconContainer.Icon == null)
techAtomInfo.IconContainer.Icon = actionInfo.Icon;
if (actionInfo.VarientIcon && !techAtomInfo.IconContainer.IsVarient)
{
techAtomInfo.IconContainer.IsVarient = actionInfo.VarientIcon;
techAtomInfo.IconContainer.IconList.Clear();
foreach (var t in actionInfo.IconList)
{
EmpireGridSpInfoPack newItem = DeepCopier.DeepCopy(t);
techAtomInfo.IconContainer.IconList.Add(newItem);
}
}
}
}
}
//asset.TechAtomList.Add(info);
}
if (TechAtom.TryParse(str, out TechAtom techAtom2))
{
bool alreadyHas = false;
foreach(var techAtom in techInfo.TechAtomList)
if (techAtom == techAtom2)
{
alreadyHas = true;
break;
}
if (!alreadyHas)
{
techInfo.TechAtomList.Add(techAtom2);
}
}
}
}
}
asset.TechAtomList.Sort((a, b) => a.TechAtom.CompareTo(b.TechAtom));
EditorUtility.SetDirty(asset);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log($"✅ 补全完成:共添加了 {addCount} 个 TechAtom 条目。");
}
}