222 lines
8.0 KiB
C#
222 lines
8.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Gameplay;
|
|
using TGS;
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using Object = UnityEngine.Object;
|
|
|
|
public static class LevelEditorHelper
|
|
{
|
|
private const string LEVEL_TEMPLATE_PATH = "Assets/Scenes/Maps/Template/MapDefault/MapDefault.unity";
|
|
|
|
private const string MAP_FOLDER = "Assets/Scenes/Maps";
|
|
|
|
[MenuItem("Assets/创建地图场景")]
|
|
private static void CreateLevel()
|
|
{
|
|
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);
|
|
EditorGUIUtility.PingObject(newScene);
|
|
}
|
|
|
|
private static string[] GetAllMapScenes(bool includeTemplate = false)
|
|
{
|
|
var guids = AssetDatabase.FindAssets("t:SceneAsset", new[] { MAP_FOLDER });
|
|
return guids.Select(AssetDatabase.GUIDToAssetPath).Where(path => includeTemplate || !path.Contains("Template")).ToArray();
|
|
}
|
|
|
|
private static string[] GetAllPostProcessedMapScenes()
|
|
{
|
|
var mapScenes = GetAllMapScenes();
|
|
var result = mapScenes.Select(PostProcessData.Instance.GetPostProcessedPath).ToArray();
|
|
return result;
|
|
}
|
|
|
|
private static List<string> GetModifiedScenes()
|
|
{
|
|
var scenePaths = GetAllMapScenes();
|
|
return scenePaths.Where(path => PostProcessData.Instance.ShouldPostProcess(path)).ToList();
|
|
}
|
|
|
|
//[MenuItem("Tools/地图场景/设置所有场景后处理指导")]
|
|
private static void SetAllMapInstructor()
|
|
{
|
|
var scenePaths = GetAllMapScenes();
|
|
foreach (var scenePath in scenePaths)
|
|
{
|
|
var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
|
|
EditorSceneManager.MarkSceneDirty(scene);
|
|
var instructor = PostProcessor.FindInstructorInScene(scene);
|
|
if (instructor == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
EditorUtility.SetDirty(instructor);
|
|
var groundObject = instructor.combinedObjectsRoot.Find("GroundObject");
|
|
if (groundObject)
|
|
{
|
|
instructor.rootsToCombine.Add(groundObject);
|
|
}
|
|
|
|
var fog = instructor.combinedObjectsRoot.Find("Fog");
|
|
if (fog)
|
|
{
|
|
instructor.fog = fog.gameObject;
|
|
}
|
|
|
|
EditorSceneManager.SaveScene(scene);
|
|
EditorSceneManager.CloseScene(scene, true);
|
|
}
|
|
}
|
|
|
|
//[MenuItem("Tools/地图场景/删除所有地图EventSystem")]
|
|
private static void DeleteAllEventSystem()
|
|
{
|
|
var scenePaths = GetAllMapScenes();
|
|
foreach (var scenePath in scenePaths)
|
|
{
|
|
var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
|
|
EditorSceneManager.MarkSceneDirty(scene);
|
|
var eventSystem = CommonUtils.GetSceneRootComponent<EventSystem>(scene);
|
|
if (eventSystem)
|
|
Object.DestroyImmediate(eventSystem.gameObject);
|
|
EditorSceneManager.SaveScene(scene);
|
|
EditorSceneManager.CloseScene(scene, true);
|
|
}
|
|
|
|
var postProcessedMapScene = GetAllPostProcessedMapScenes();
|
|
foreach (var scenePath in postProcessedMapScene)
|
|
{
|
|
var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
|
|
EditorSceneManager.MarkSceneDirty(scene);
|
|
var eventSystem = CommonUtils.GetSceneRootComponent<EventSystem>(scene);
|
|
if (eventSystem)
|
|
Object.DestroyImmediate(eventSystem.gameObject);
|
|
EditorSceneManager.SaveScene(scene);
|
|
EditorSceneManager.CloseScene(scene, true);
|
|
}
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
PostProcessData.Instance.ReCalculateHashCode();
|
|
}
|
|
|
|
[MenuItem("Tools/地图场景/后处理所有场景")]
|
|
private static void PostProcessAllMap()
|
|
{
|
|
var scenePaths = GetModifiedScenes();
|
|
|
|
if (EditorUtility.DisplayDialog("后处理所有地图场景", $"当前有修改的地图场景有:{scenePaths.Count}个,是否批量执行?",
|
|
"确认", "取消"))
|
|
{
|
|
int index = 0;
|
|
foreach (var scenePath in scenePaths)
|
|
{
|
|
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++;
|
|
}
|
|
|
|
EditorUtility.ClearProgressBar();
|
|
Debug.Log("执行所有地图后处理完成!");
|
|
}
|
|
}
|
|
|
|
// [MenuItem("Tools/Addressables/转移所有场景到组的根部")]
|
|
private static void MoveAllScenesToAddressableGroupRoot()
|
|
{
|
|
#if USE_ADDRESSABLES
|
|
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);
|
|
entries = entries.Where(e => e.MainAssetType == typeof(SceneAsset)).ToList();
|
|
if (entries.Count > 0)
|
|
{
|
|
EditorUtility.SetDirty(group);
|
|
foreach (var sceneEntry in entries)
|
|
{
|
|
settings.MoveEntry(sceneEntry, group);
|
|
}
|
|
}
|
|
}
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
#endif
|
|
}
|
|
|
|
// [MenuItem("Tools/Addressables/修改所有group下载信息")]
|
|
private static void ModifyAllGroupDownloadInfo()
|
|
{
|
|
#if USE_ADDRESSABLES
|
|
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;
|
|
schema.Timeout = 5;
|
|
}
|
|
}
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
#endif
|
|
}
|
|
|
|
private static string[] GetAllOriginalAndProcessedMap()
|
|
{
|
|
var originalMap = GetAllMapScenes(true);
|
|
var processedMap = GetAllPostProcessedMapScenes();
|
|
return originalMap.Concat(processedMap).ToArray();
|
|
}
|
|
|
|
[MenuItem("Tools/地图场景/修改所有场景TGS边框厚度")]
|
|
private static void ModifyAllOriginalAndProcessedMapTGSThickness()
|
|
{
|
|
var maps = GetAllOriginalAndProcessedMap();
|
|
foreach (var map in maps)
|
|
{
|
|
var scene = EditorSceneManager.OpenScene(map, OpenSceneMode.Additive);
|
|
var tgs = CommonUtils.GetSceneRootComponent<TerrainGridSystem>(scene);
|
|
if (tgs)
|
|
{
|
|
tgs.cellBorderThickness = 0.04f;
|
|
}
|
|
EditorSceneManager.SaveScene(scene);
|
|
EditorSceneManager.CloseScene(scene, true);
|
|
}
|
|
PostProcessData.Instance.ReCalculateHashCode();
|
|
}
|
|
} |