87 lines
3.6 KiB
C#
87 lines
3.6 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
public class TechTreeTools
|
|
{
|
|
// 定义菜单项的路径
|
|
private const string MENU_ITEM_PATH = "填表脚本/批量设置科技树类型";
|
|
|
|
// 定义要查找的父对象的场景路径
|
|
private const string PARENT_OBJECT_PATH = "UICanvas/TechTreePanel/TechTree";
|
|
|
|
// 关键方法:添加一个菜单项,点击后会调用此方法
|
|
[MenuItem(MENU_ITEM_PATH)]
|
|
public static void SetClimbingTechTypeForChildren()
|
|
{
|
|
// 1. 查找父对象
|
|
GameObject parentObject = GameObject.Find(PARENT_OBJECT_PATH);
|
|
|
|
// 2. 容错处理:如果没找到父对象
|
|
if (parentObject == null)
|
|
{
|
|
EditorUtility.DisplayDialog("错误", $"在当前场景中找不到指定的游戏对象路径: \n\n{PARENT_OBJECT_PATH}\n\n请检查对象名称和层级是否正确。", "好的");
|
|
return;
|
|
}
|
|
|
|
int modifiedCount = 0;
|
|
Transform parentTransform = parentObject.transform;
|
|
|
|
// 3. 遍历所有直接子对象
|
|
foreach (Transform childTransform in parentTransform) // 直接遍历transform就是遍历直接子对象
|
|
{
|
|
string name = childTransform.name;
|
|
int techNum = int.Parse(name.Substring(14));
|
|
// 4. 尝试获取 TechTypeMono 组件
|
|
TechTypeMono techMono = childTransform.GetComponent<TechTypeMono>();
|
|
|
|
// 5. 如果组件存在,则修改其属性
|
|
if (techMono != null)
|
|
{
|
|
// 关键!在修改前记录对象状态,以便可以撤销
|
|
Undo.RecordObject(techMono, "Set TechType to Climbing");
|
|
|
|
// 设置新的值
|
|
techMono.TechType = techNum switch
|
|
{
|
|
1 => TechType.Climbing,
|
|
11 => TechType.Meditation,
|
|
12 => TechType.Mining,
|
|
111 => TechType.Philosophy,
|
|
121 => TechType.Smithery,
|
|
2 => TechType.Organization,
|
|
21 => TechType.Strategy,
|
|
22 => TechType.Farming,
|
|
211 => TechType.Diplomacy,
|
|
221 => TechType.Construction,
|
|
3 => TechType.Riding,
|
|
31 => TechType.FreeSpirit,
|
|
32 => TechType.Roads,
|
|
311 => TechType.Chivalry,
|
|
321 => TechType.Trade,
|
|
4 => TechType.Hunting,
|
|
41 => TechType.Forestry,
|
|
42 => TechType.Archery,
|
|
411 => TechType.Mathematics,
|
|
421 => TechType.Spiritualism,
|
|
5 => TechType.Fishing,
|
|
51 => TechType.Ramming,
|
|
52 => TechType.Sailing,
|
|
511 => TechType.Aquatism,
|
|
521 => TechType.Navigation,
|
|
_ => TechType.None
|
|
};
|
|
|
|
// 标记该组件为“脏”,确保更改会被保存到场景中
|
|
EditorUtility.SetDirty(techMono);
|
|
|
|
modifiedCount++;
|
|
}
|
|
}
|
|
|
|
// 6. 在控制台打印结果,提供反馈
|
|
Debug.Log($"<color=green>任务完成:</color> 共修改了 <color=yellow>{modifiedCount}</color> 个对象的 TechTypeMono.TechType 为 <color=cyan>Climbing</color>。路径: {PARENT_OBJECT_PATH}");
|
|
|
|
// (可选)也可以弹窗提示
|
|
EditorUtility.DisplayDialog("任务完成", $"成功修改了 {modifiedCount} 个科技对象的类型为 Climbing。", "好的");
|
|
}
|
|
} |