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

237 lines
8.9 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;
2023-12-22 19:00:27 +08:00
using System.IO;
2024-01-17 13:27:51 +08:00
using System.Linq;
2025-04-28 15:03:17 +08:00
using Framework.Utils;
2024-01-19 16:37:20 +08:00
using Gameplay;
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;
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-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-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;
var savePath = EditorUtil.GetAssetUniquePath($"{currDir}/{dirName}_{count}.unity", out var assetName);
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
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
}