2024-01-15 20:25:49 +08:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEditor.SceneManagement;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
|
|
|
|
public class ScenePostProcessor : PostProcessor
|
|
|
|
|
{
|
2025-05-08 11:59:40 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 原场景
|
|
|
|
|
/// </summary>
|
2024-01-15 20:25:49 +08:00
|
|
|
private Scene Scene { get; set; }
|
|
|
|
|
|
2025-05-08 11:59:40 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 后处理场景
|
|
|
|
|
/// </summary>
|
2024-01-15 20:25:49 +08:00
|
|
|
private Scene ClonedScene { get; set; }
|
|
|
|
|
|
2025-05-08 11:59:40 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 后处理场景根路径
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const string SCENE_POSTPROCESS_DIR = "Assets/Art_Out/AutoGen/PostProcessScenes";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 场景后处理根文件路径
|
|
|
|
|
/// </summary>
|
2024-01-15 20:25:49 +08:00
|
|
|
protected override string ResourcesBaseDir => SCENE_POSTPROCESS_DIR;
|
2025-05-08 11:59:40 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 后处理场景父路径
|
|
|
|
|
/// </summary>
|
2025-08-06 18:43:47 +08:00
|
|
|
protected override string PostProcessFolderName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string[] results = scenePath.Split('/');
|
|
|
|
|
string result = results[^1];
|
|
|
|
|
|
|
|
|
|
return result.Split('.')[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-08 11:59:40 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 原场景资源
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override Object AssetToPostProcess => AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 后处理场景路径
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override string OutputAssetPath => $"{ProcessedResourceDir}/{sceneName}_{SCENE_POST_PROCESS_NAME}.unity";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 原场景路径
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string scenePath;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 原场景名字
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string sceneName;
|
2024-01-15 20:25:49 +08:00
|
|
|
|
|
|
|
|
protected override void Init(PostProcessInstructor postProcessInstructor)
|
|
|
|
|
{
|
|
|
|
|
base.Init(postProcessInstructor);
|
|
|
|
|
Scene = postProcessInstructor.gameObject.scene;
|
2025-05-08 11:59:40 +08:00
|
|
|
// 保存场景名字及路径
|
|
|
|
|
scenePath = Scene.path;
|
|
|
|
|
sceneName = Scene.name;
|
2024-01-15 20:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override PostProcessInstructor Clone()
|
|
|
|
|
{
|
2025-07-15 19:36:35 +08:00
|
|
|
int sceneCount = SceneManager.sceneCount;
|
|
|
|
|
for (int i = 0; i < sceneCount; ++i)
|
2024-01-15 20:25:49 +08:00
|
|
|
{
|
2025-07-15 19:36:35 +08:00
|
|
|
Scene scene = SceneManager.GetSceneAt(i);
|
2024-01-15 20:25:49 +08:00
|
|
|
if (scene.name.Contains(SCENE_POST_PROCESS_NAME))
|
|
|
|
|
EditorSceneManager.CloseScene(scene, true);
|
|
|
|
|
}
|
2025-05-08 11:59:40 +08:00
|
|
|
|
2024-01-16 18:37:02 +08:00
|
|
|
AssetDatabase.CopyAsset(Scene.path, OutputAssetPath);
|
2024-01-15 20:25:49 +08:00
|
|
|
AssetDatabase.Refresh();
|
2024-01-16 18:37:02 +08:00
|
|
|
ClonedScene = EditorSceneManager.OpenScene(OutputAssetPath, OpenSceneMode.Additive);
|
2024-01-15 20:25:49 +08:00
|
|
|
EditorSceneManager.MarkAllScenesDirty();
|
2025-07-15 19:36:35 +08:00
|
|
|
PostProcessInstructor result = FindInstructorInScene(ClonedScene);
|
2025-05-08 11:59:40 +08:00
|
|
|
|
|
|
|
|
// 关闭原始场景
|
|
|
|
|
CloseOriginalScene();
|
|
|
|
|
|
2024-01-15 20:25:49 +08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-08 11:59:40 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭原始场景
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void CloseOriginalScene()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (SceneManager.sceneCount > 1)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"关闭原始场景: {Scene.name}");
|
|
|
|
|
EditorSceneManager.CloseScene(Scene, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("只有一个场景打开,无法关闭原始场景,需要至少保留一个场景。");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"关闭原始场景时出错: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 20:25:49 +08:00
|
|
|
protected override void AfterAll(bool destroyAfterPostProcess)
|
|
|
|
|
{
|
|
|
|
|
EditorSceneManager.SaveOpenScenes();
|
|
|
|
|
EditorUtil.AddPathToAddressable(ProcessedResourceDir, SCENE_ADDRESSABLE_GROUP);
|
|
|
|
|
if (destroyAfterPostProcess)
|
|
|
|
|
EditorSceneManager.CloseScene(ClonedScene, true);
|
|
|
|
|
EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<SceneAsset>(ClonedScene.path));
|
|
|
|
|
}
|
|
|
|
|
}
|