using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.SceneManagement; public class ScenePostProcessor : PostProcessor { private Scene Scene { get; set; } private Scene ClonedScene { get; set; } protected override string ResourcesBaseDir => SCENE_POSTPROCESS_DIR; protected override string PostProcessFolderName => Path.GetFileNameWithoutExtension(Scene.path.Replace("/", "_")); protected override Object AssetToPostProcess => AssetDatabase.LoadAssetAtPath(Scene.path); protected override string OutputAssetPath => $"{ProcessedResourceDir}/{Scene.name}_{SCENE_POST_PROCESS_NAME}.unity"; protected override void Init(PostProcessInstructor postProcessInstructor) { base.Init(postProcessInstructor); Scene = postProcessInstructor.gameObject.scene; } protected override PostProcessInstructor Clone() { var sceneCount = SceneManager.sceneCount; for (int i = 0; i < sceneCount; i++) { var scene = SceneManager.GetSceneAt(i); if (scene.name.Contains(SCENE_POST_PROCESS_NAME)) EditorSceneManager.CloseScene(scene, true); } AssetDatabase.CopyAsset(Scene.path, OutputAssetPath); AssetDatabase.Refresh(); ClonedScene = EditorSceneManager.OpenScene(OutputAssetPath, OpenSceneMode.Additive); EditorSceneManager.MarkAllScenesDirty(); var result = FindInstructorInScene(ClonedScene); return result; } protected override void AfterAll(bool destroyAfterPostProcess) { EditorSceneManager.SaveOpenScenes(); EditorUtil.AddPathToAddressable(ProcessedResourceDir, SCENE_ADDRESSABLE_GROUP); if (destroyAfterPostProcess) EditorSceneManager.CloseScene(ClonedScene, true); EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath(ClonedScene.path)); } }