NLDClient-yudde/ProjectNLD/Assets/Editor/LevelEditor/LevelEditorHelper.cs

403 lines
16 KiB
C#
Raw Normal View History

2024-01-16 18:37:02 +08:00
using System;
2023-11-19 22:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
2025-12-11 12:04:06 +08:00
using System.Globalization;
2023-12-22 19:00:27 +08:00
using System.IO;
2024-01-17 13:27:51 +08:00
using System.Linq;
2025-12-11 12:04:06 +08:00
using System.Text;
using Framework;
2025-04-28 15:03:17 +08:00
using Framework.Utils;
2024-01-19 16:37:20 +08:00
using Gameplay;
2025-12-11 12:04:06 +08:00
using Gameplay.Level;
2024-02-27 13:14:28 +08:00
using TGS;
2023-11-19 22:57:17 +08:00
using UnityEditor;
2024-01-09 15:48:55 +08:00
using UnityEditor.SceneManagement;
2023-11-19 22:57:17 +08:00
using UnityEngine;
2024-01-19 16:37:20 +08:00
using UnityEngine.EventSystems;
2025-12-11 12:04:06 +08:00
using PhxhSDK.Phxh;
2024-01-19 16:37:20 +08:00
using Object = UnityEngine.Object;
2023-11-19 22:57:17 +08:00
public static class LevelEditorHelper
{
2023-12-20 17:19:51 +08:00
private const string LEVEL_TEMPLATE_PATH = "Assets/Scenes/Maps/Template/MapDefault/MapDefault.unity";
2025-08-26 13:26:01 +08:00
private const string TERRAIN_LEVEL_TEMPLATE_PATH = "Assets/Scenes/Maps/Template/MapDefault/TerrainMapDefault.unity";
2025-02-11 15:03:22 +08:00
private const string LEGION_LEVEL_TEMPLATE_PATH = "Assets/Scenes/Maps/Template/MapDefault/LegionMapDefault.unity";
2023-12-22 19:00:27 +08:00
2024-01-09 15:48:55 +08:00
private const string MAP_FOLDER = "Assets/Scenes/Maps";
2025-02-28 13:26:03 +08:00
[MenuItem("Assets/*Create/地图场景/创建标准地图")]
2023-11-19 22:57:17 +08:00
private static void CreateLevel()
{
2023-12-22 19:00:27 +08:00
var currDir = EditorUtil.GetCurrentAssetDirectory().Replace("\\", "/");
var dirName = Path.GetFileName(currDir);
var count = AssetDatabase.FindAssets("t:SceneAsset", new[] { currDir }).Length + 1;
var savePath = EditorUtil.GetAssetUniquePath($"{currDir}/{dirName}_{count}.unity", out var assetName);
AssetDatabase.CopyAsset(LEVEL_TEMPLATE_PATH, savePath);
var newScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(savePath);
2023-11-20 12:25:41 +08:00
EditorGUIUtility.PingObject(newScene);
2025-02-11 15:03:22 +08:00
}
2025-08-26 13:26:01 +08:00
[MenuItem("Assets/*Create/地图场景/创建标准地图(新)")]
private static void CreateTerrainLevel()
{
var currDir = EditorUtil.GetCurrentAssetDirectory().Replace("\\", "/");
var dirName = Path.GetFileName(currDir);
var count = AssetDatabase.FindAssets("t:SceneAsset", new[] { currDir }).Length + 1;
var savePath = EditorUtil.GetAssetUniquePath($"{currDir}/{dirName}_{count}.unity", out var assetName);
AssetDatabase.CopyAsset(TERRAIN_LEVEL_TEMPLATE_PATH, savePath);
var newScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(savePath);
EditorGUIUtility.PingObject(newScene);
}
2025-02-28 13:26:03 +08:00
[MenuItem("Assets/*Create/地图场景/创建军团地图")]
2025-02-11 15:03:22 +08:00
private static void CreateLegionLevel()
{
var currDir = EditorUtil.GetCurrentAssetDirectory().Replace("\\", "/");
var dirName = Path.GetFileName(currDir);
var count = AssetDatabase.FindAssets("t:SceneAsset", new[] { currDir }).Length + 1;
2025-08-25 17:34:39 +08:00
var savePath = EditorUtil.GetAssetUniquePath($"{currDir}/{dirName}_Legion_{count}.unity", out var assetName);
2025-02-11 15:03:22 +08:00
AssetDatabase.CopyAsset(LEGION_LEVEL_TEMPLATE_PATH, savePath);
var newScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(savePath);
EditorGUIUtility.PingObject(newScene);
2023-11-19 22:57:17 +08:00
}
2024-01-09 15:48:55 +08:00
2025-12-11 12:04:06 +08:00
[MenuItem("Tools/*关卡数据工具/关卡任务导出")]
private static void ExportLevelData()
{
// 选择导出路径
var savePath = EditorUtility.SaveFilePanel("导出关卡任务数据", Application.dataPath, "LevelTasks.csv", "csv");
if (string.IsNullOrEmpty(savePath))
return;
var sb = new StringBuilder();
// 表头
sb.AppendLine(string.Join(",",
Csv("关卡json"),
Csv("任务ID"),
Csv("任务条件"),
Csv("任务文本key"),
Csv("任务文本"),
Csv("任务注释"),
Csv("是否为胜利条件"),
Csv("是否为失败条件"),
Csv("是否为1星条件"),
Csv("是否为2星条件"),
Csv("是否为3星条件")));
var levelCfgList = EditorTableManager.instance.tables.Level.DataList;
try
{
var levelCount = levelCfgList.Count;
for (var levelIndex = 0; levelIndex < levelCount; levelIndex++)
{
var cfg = levelCfgList[levelIndex];
// 更新进度条
var progress = (levelIndex + 1) / (float)levelCount;
EditorUtility.DisplayProgressBar("导出关卡任务数据",
$"正在处理关卡: {cfg.LevelData} ({levelIndex + 1}/{levelCount})", progress);
// cfg.LevelData 为关卡数据名(不带扩展名)
var levelJsonName = cfg.LevelData;
var levelPath = string.Format(Constants.LEVEL_CONFIG_FORMAT_PATH, levelJsonName);
if (!File.Exists(levelPath))
{
Debug.LogWarning($"关卡数据不存在: {levelPath}");
continue;
}
LevelData levelData = null;
try
{
levelData = JsonHelper.LoadJson<LevelData>(levelPath);
}
catch (Exception e)
{
Debug.LogError($"解析关卡数据失败: {levelPath}\n{e}");
continue;
}
if (levelData == null || levelData.taskInfos == null || levelData.taskInfos.Count == 0)
continue;
foreach (var task in levelData.taskInfos)
{
if (task == null || task.taskCondition == null)
continue;
var taskId = task.taskID;
var condition = task.taskCondition;
// 任务条件ConditionType(参数1,参数2,...)
var condTypeName = condition.conditionType.ToString();
var args = condition.Args();
string condWithArgs;
if (args != null && args.Count > 0)
{
var argStr = string.Join(",", args.Select(a => a.ToString(CultureInfo.InvariantCulture)));
condWithArgs = $"{condTypeName}({argStr})";
}
else
{
condWithArgs = condTypeName;
}
// 文本 key如果有手写替换 keytaskDesc则使用没有就留空
string textKey = string.IsNullOrEmpty(task.taskDesc) ? string.Empty : task.taskDesc;
// 文本内容:仅当有有效 key 时,从 StringConfig 里拿 value否则留空
string textValue = string.Empty;
if (!string.IsNullOrEmpty(textKey))
{
try
{
var dataString = EditorTableManager.instance.tables.StringConfig.GetOrDefault(textKey);
textValue = dataString?.Value ?? string.Empty;
}
catch
{
// 忽略文本表中不存在的 key
}
}
// 是否为胜利/失败/星级条件
var isSuccess = levelData.successTaskID == taskId;
var isFail = levelData.failTaskID == taskId;
var isStar1 = levelData.starTaskIDs != null && levelData.starTaskIDs.Count > 0 &&
levelData.starTaskIDs[0] == taskId;
var isStar2 = levelData.starTaskIDs != null && levelData.starTaskIDs.Count > 1 &&
levelData.starTaskIDs[1] == taskId;
var isStar3 = levelData.starTaskIDs != null && levelData.starTaskIDs.Count > 2 &&
levelData.starTaskIDs[2] == taskId;
sb.AppendLine(string.Join(",",
Csv(levelJsonName),
Csv(taskId.ToString()),
Csv(condWithArgs),
Csv(textKey),
Csv(textValue),
Csv(task.taskNote ?? string.Empty),
Csv(isSuccess ? "1" : "0"),
Csv(isFail ? "1" : "0"),
Csv(isStar1 ? "1" : "0"),
Csv(isStar2 ? "1" : "0"),
Csv(isStar3 ? "1" : "0")));
}
}
}
finally
{
EditorUtility.ClearProgressBar();
}
File.WriteAllText(savePath, sb.ToString(), Encoding.UTF8);
AssetDatabase.Refresh();
EditorUtility.RevealInFinder(savePath);
}
/// <summary>
/// 简单 CSV 转义:用双引号包裹,并转义内部双引号。
/// </summary>
private static string Csv(string value)
{
if (string.IsNullOrEmpty(value))
return "\"\"";
value = value.Replace("\"", "\"\"");
return $"\"{value}\"";
}
2024-02-27 13:14:28 +08:00
private static string[] GetAllMapScenes(bool includeTemplate = false)
2024-01-17 13:27:51 +08:00
{
var guids = AssetDatabase.FindAssets("t:SceneAsset", new[] { MAP_FOLDER });
2024-02-27 13:14:28 +08:00
return guids.Select(AssetDatabase.GUIDToAssetPath).Where(path => includeTemplate || !path.Contains("Template")).ToArray();
2024-01-17 13:27:51 +08:00
}
2024-01-19 16:37:20 +08:00
private static string[] GetAllPostProcessedMapScenes()
{
var mapScenes = GetAllMapScenes();
var result = mapScenes.Select(PostProcessData.EditorInstance.EditorGetPostProcessedPath).ToArray();
2024-01-19 16:37:20 +08:00
return result;
}
2024-01-30 12:35:54 +08:00
2024-01-17 13:27:51 +08:00
private static List<string> GetModifiedScenes()
{
var scenePaths = GetAllMapScenes();
return scenePaths.Where(path => PostProcessData.EditorInstance.ShouldPostProcess(path)).ToList();
2024-01-17 13:27:51 +08:00
}
2024-01-16 19:58:36 +08:00
//[MenuItem("Tools/地图场景/设置所有场景后处理指导")]
2024-01-16 18:37:02 +08:00
private static void SetAllMapInstructor()
2024-01-09 15:48:55 +08:00
{
2024-01-17 13:27:51 +08:00
var scenePaths = GetAllMapScenes();
foreach (var scenePath in scenePaths)
2024-01-09 15:48:55 +08:00
{
var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
2024-01-16 19:58:36 +08:00
EditorSceneManager.MarkSceneDirty(scene);
2024-01-16 18:37:02 +08:00
var instructor = PostProcessor.FindInstructorInScene(scene);
if (instructor == null)
{
continue;
}
2024-01-17 13:27:51 +08:00
2024-01-16 19:58:36 +08:00
EditorUtility.SetDirty(instructor);
2024-01-17 13:44:26 +08:00
var groundObject = instructor.combinedObjectsRoot.Find("GroundObject");
2024-01-16 18:37:02 +08:00
if (groundObject)
{
2024-01-17 13:44:26 +08:00
instructor.rootsToCombine.Add(groundObject);
2024-01-16 18:37:02 +08:00
}
2024-01-17 13:44:26 +08:00
var fog = instructor.combinedObjectsRoot.Find("Fog");
2024-01-16 18:37:02 +08:00
if (fog)
{
instructor.fog = fog.gameObject;
}
2024-01-17 13:27:51 +08:00
2024-01-16 18:37:02 +08:00
EditorSceneManager.SaveScene(scene);
2024-01-09 15:48:55 +08:00
EditorSceneManager.CloseScene(scene, true);
}
}
2024-01-30 12:35:54 +08:00
2024-01-19 16:40:05 +08:00
//[MenuItem("Tools/地图场景/删除所有地图EventSystem")]
2024-01-19 16:37:20 +08:00
private static void DeleteAllEventSystem()
{
var scenePaths = GetAllMapScenes();
foreach (var scenePath in scenePaths)
{
var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
EditorSceneManager.MarkSceneDirty(scene);
2025-04-28 15:03:17 +08:00
var eventSystem = FrameWorkUtils.GetSceneRootComponent<EventSystem>(scene);
2024-01-30 12:35:54 +08:00
if (eventSystem)
2024-01-19 16:37:20 +08:00
Object.DestroyImmediate(eventSystem.gameObject);
EditorSceneManager.SaveScene(scene);
EditorSceneManager.CloseScene(scene, true);
}
2024-01-17 13:27:51 +08:00
2024-01-19 16:37:20 +08:00
var postProcessedMapScene = GetAllPostProcessedMapScenes();
foreach (var scenePath in postProcessedMapScene)
{
var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
EditorSceneManager.MarkSceneDirty(scene);
2025-04-28 15:03:17 +08:00
var eventSystem = FrameWorkUtils.GetSceneRootComponent<EventSystem>(scene);
2024-01-30 12:35:54 +08:00
if (eventSystem)
2024-01-19 16:37:20 +08:00
Object.DestroyImmediate(eventSystem.gameObject);
EditorSceneManager.SaveScene(scene);
EditorSceneManager.CloseScene(scene, true);
}
2024-01-30 12:35:54 +08:00
2024-01-19 16:40:05 +08:00
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
PostProcessData.EditorInstance.ReCalculateHashCode();
2024-01-19 16:37:20 +08:00
}
2024-01-30 12:35:54 +08:00
2025-02-27 13:31:47 +08:00
[MenuItem("Tools/*地图场景/后处理所有场景", false, (int)NLDMenuID.MapPostProcess)]
2024-01-16 18:37:02 +08:00
private static void PostProcessAllMap()
{
2024-01-17 13:27:51 +08:00
var scenePaths = GetModifiedScenes();
if (EditorUtility.DisplayDialog("后处理所有地图场景", $"当前有修改的地图场景有:{scenePaths.Count}个,是否批量执行?",
"确认", "取消"))
2024-01-16 18:37:02 +08:00
{
2024-01-17 13:27:51 +08:00
int index = 0;
foreach (var scenePath in scenePaths)
2024-01-16 18:37:02 +08:00
{
2024-01-17 13:27:51 +08:00
EditorUtility.DisplayProgressBar("后处理所有地图场景", $"当前场景路径:{scenePath} {index + 1}/{scenePaths.Count}",
(index + 1) / (float)scenePaths.Count);
var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
try
{
ObjectPostProcessorManager.Instance.PostProcess(scene, true);
}
catch (Exception e)
{
Debug.LogError($"{e.Message} \r\n {e.StackTrace}");
}
finally
{
EditorSceneManager.CloseScene(scene, true);
}
index++;
2024-01-16 18:37:02 +08:00
}
2024-01-30 12:35:54 +08:00
2024-01-17 13:27:51 +08:00
EditorUtility.ClearProgressBar();
Debug.Log("执行所有地图后处理完成!");
2024-01-16 18:37:02 +08:00
}
}
2024-01-30 12:35:54 +08:00
2024-11-04 15:16:03 +08:00
// [MenuItem("Tools/Addressables/转移所有场景到组的根部")]
2024-01-30 12:35:54 +08:00
private static void MoveAllScenesToAddressableGroupRoot()
{
2024-02-27 15:07:59 +08:00
#if USE_ADDRESSABLES
2024-01-30 12:35:54 +08:00
var settings = AddressableAssetSettingsDefaultObject.Settings;
EditorUtility.SetDirty(settings);
List<AddressableAssetEntry> entries = new();
foreach (var group in settings.groups)
{
entries.Clear();
group.GatherAllAssets(entries, true, true, false, e => e.MainAssetType == typeof(SceneAsset) || e.IsFolder);
2024-01-30 14:25:25 +08:00
entries = entries.Where(e => e.MainAssetType == typeof(SceneAsset)).ToList();
2024-01-30 12:35:54 +08:00
if (entries.Count > 0)
{
EditorUtility.SetDirty(group);
foreach (var sceneEntry in entries)
{
settings.MoveEntry(sceneEntry, group);
}
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
2024-02-27 15:07:59 +08:00
#endif
2024-01-30 12:35:54 +08:00
}
2024-01-31 17:06:00 +08:00
2024-11-04 15:16:03 +08:00
// [MenuItem("Tools/Addressables/修改所有group下载信息")]
2024-01-31 17:06:00 +08:00
private static void ModifyAllGroupDownloadInfo()
{
2024-02-27 15:07:59 +08:00
#if USE_ADDRESSABLES
2024-01-31 17:06:00 +08:00
var settings = AddressableAssetSettingsDefaultObject.Settings;
EditorUtility.SetDirty(settings);
foreach (var group in settings.groups)
{
EditorUtility.SetDirty(group);
var schema = group.GetSchema<BundledAssetGroupSchema>();
if (schema)
{
EditorUtility.SetDirty(schema);
schema.RetryCount = 3;
2024-01-31 18:56:01 +08:00
schema.Timeout = 5;
2024-01-31 17:06:00 +08:00
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
2024-02-27 15:07:59 +08:00
#endif
2024-01-31 17:06:00 +08:00
}
2024-02-27 13:14:28 +08:00
private static string[] GetAllOriginalAndProcessedMap()
{
var originalMap = GetAllMapScenes(true);
var processedMap = GetAllPostProcessedMapScenes();
return originalMap.Concat(processedMap).ToArray();
}
2025-02-27 13:31:47 +08:00
[MenuItem("Tools/*地图场景/修改所有场景TGS边框厚度", false, (int)NLDMenuID.MapTGSThickness)]
2024-02-27 13:14:28 +08:00
private static void ModifyAllOriginalAndProcessedMapTGSThickness()
{
var maps = GetAllOriginalAndProcessedMap();
foreach (var map in maps)
{
var scene = EditorSceneManager.OpenScene(map, OpenSceneMode.Additive);
2025-04-28 15:03:17 +08:00
var tgs = FrameWorkUtils.GetSceneRootComponent<TerrainGridSystem>(scene);
2024-02-27 13:14:28 +08:00
if (tgs)
{
tgs.cellBorderThickness = 0.04f;
}
EditorSceneManager.SaveScene(scene);
EditorSceneManager.CloseScene(scene, true);
}
PostProcessData.EditorInstance.ReCalculateHashCode();
2024-02-27 13:14:28 +08:00
}
2023-12-22 19:00:27 +08:00
}