Files
test/Assets/Editor/SceneLoaderEditor.cs
T

75 lines
2.3 KiB
C#

using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.IO;
using System.Linq;
public class QuickSceneWindow : EditorWindow
{
[MenuItem("GameObject/Quick Open Adaptive Scene", false, 1)]
public static void ShowWindow()
{
var window = GetWindow<QuickSceneWindow>("Quick Scene");
// Kích thước cửa sổ
float width = 250;
float height = 300;
// Lấy kích thước màn hình Unity Editor
float screenWidth = EditorGUIUtility.GetMainWindowPosition().width;
float screenHeight = EditorGUIUtility.GetMainWindowPosition().height;
// Tính toán vị trí để căn giữa
float x = (screenWidth - width) / 2;
float y = (screenHeight - height) / 2;
// Đặt vị trí và kích thước cho cửa sổ
window.position = new Rect(x, y, width, height);
window.Show();
}
void OnGUI()
{
EditorGUILayout.LabelField("Available Scenes:", EditorStyles.boldLabel);
EditorGUILayout.Space();
var scenes = EditorBuildSettings.scenes;
string currentScenePath = EditorSceneManager.GetActiveScene().path;
if (scenes.Length == 0)
{
EditorGUILayout.HelpBox("No Scenes in Build Settings", MessageType.Warning);
return;
}
// Lọc ra các scene không phải scene hiện tại
var availableScenes = scenes.Where(scene =>
scene.enabled &&
!string.Equals(scene.path, currentScenePath, System.StringComparison.OrdinalIgnoreCase)
);
if (!availableScenes.Any())
{
EditorGUILayout.HelpBox("No other scenes available", MessageType.Info);
return;
}
foreach (var scene in availableScenes)
{
string sceneName = Path.GetFileNameWithoutExtension(scene.path);
if (GUILayout.Button(sceneName, GUILayout.Height(30)))
{
OpenSceneAdditively(scene.path);
}
}
}
private void OpenSceneAdditively(string path)
{
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
{
EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
Debug.Log($"Opened scene additively: {path}");
this.Close();
}
}
}