728x90
1. 노가다 방식의 씬 이동 방식 생성
처음에는 직접 씬의 이름을 하나하나 작성하는 방식으로 코딩을 했습니다. 하지만 이 방법은 너무 비효율적이었기 때문에 더 나은 방법을 찾아봤습니다.
using UnityEditor;
using UnityEditor.SceneManagement;
public static class QuickSceneChangeEditor
{
[MenuItem("Scene/GameScene")]
private static void OpenGameScene() => OpenScene("Assets/Scenes/GameScene.unity");
[MenuItem("Scene/TitleScene")]
private static void OpenTitleScene() => OpenScene("Assets/Scenes/TitleScene.unity");
[MenuItem("Scene/IntroScene")]
private static void OpenIntroScene() => OpenScene("Assets/Scenes/IntroScene.unity");
private static void OpenScene(string scenePath)
{
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
{
EditorSceneManager.OpenScene(scenePath);
}
}
}
2. 노가다 방식의 스크립트 생성 자동화
위에서 작성한 씬 이동 스크립트를 자동으로 생성해주면 좋겠다고 생각했습니다. 이전에는 파일을 탐색하는 방식을 사용했는데, 이제는 Unity에서 빌드한 씬들의 이름을 자동으로 가져오는 방식으로 변경했습니다.
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public static class QuickSceneSelector
{
private const string scriptPath = "Assets/Editor/SceneChangeEditor.cs";
[MenuItem("Scene/Reload Scene")]
public static void GenerateSceneMenu()
{
// Build Settings에서 씬 목록 가져오기
var scenes = EditorBuildSettings.scenes;
if (scenes.Length == 0)
{
Debug.LogWarning("Build Settings에 등록된 씬이 없습니다.");
return;
}
// 자동 생성할 스크립트 내용
string scriptContent = @"
using UnityEditor;
using UnityEditor.SceneManagement;
public static class QuickSceneChangeEditor
{
";
// 각 씬을 기반으로 MenuItem 추가
foreach (var scene in scenes)
{
if (scene.enabled)
{
string sceneName = Path.GetFileNameWithoutExtension(scene.path);
scriptContent += $@"
[MenuItem(""Scene/{sceneName}"")]
private static void Open{sceneName}() => OpenScene(""{scene.path}"");
";
}
}
// 공통 Scene 로드 함수 추가
scriptContent += @"
private static void OpenScene(string scenePath)
{
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
{
EditorSceneManager.OpenScene(scenePath);
}
}
}";
// 스크립트 파일 저장
File.WriteAllText(scriptPath, scriptContent);
AssetDatabase.Refresh();
Debug.Log($"씬 변경 스크립트 생성 완료: {scriptPath}");
}
}
3. Editor 창을 이용한 씬 자동 생성 방식
위 방식은 씬을 추가할 때마다 에셋의 데이터베이스를 리셋하거나 스크립트를 새로 생성해야 한다는 단점이 있습니다. 그래서 리로드 시간이 길어지는 게 싫어서, 윈도우 창을 띄워 바로바로 씬을 이동할 수 있도록 했습니다.
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public class QuickSceneSelector : EditorWindow
{
[MenuItem("Scene/Select Scene")]
private static void Init()
{
QuickSceneSelector window = GetWindow<QuickSceneSelector>();
window.titleContent = new GUIContent("Scene Selector");
window.Show();
}
private void OnGUI()
{
GUILayout.Label("Select a Scene", EditorStyles.boldLabel);
// EditorBuildSettings에 등록된 씬들을 가져온다
foreach (var scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
// 씬의 이름만 가져온다.
string sceneName = System.IO.Path.GetFileNameWithoutExtension(scene.path);
if (GUILayout.Button(sceneName))
{
OpenScene(scene.path);
}
}
}
}
private static void OpenScene(string scenePath)
{
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
{
EditorSceneManager.OpenScene(scenePath);
}
}
}
최종 결과
이제 씬이동 및 전환이 더욱 효율적이게 되었습니다
728x90
'2025 > Unity' 카테고리의 다른 글
Unity - Parallax 배경 효과를 구현하는 2가지 방법 (0) | 2025.02.11 |
---|---|
Unity - 타이핑 효과 컬러 적용 (0) | 2025.02.11 |
Unity urp 흑백화면 만드는 2가지 방법 (0) | 2025.02.10 |
Unity - 중복없는 랜덤 스폰 시스템 <Grid> (0) | 2025.02.06 |
2025년도 코드 규칙 정하기 c# (0) | 2025.02.05 |