-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class EditorDraw : MonoBehaviour | ||
{ | ||
// Start is called before the first frame update | ||
void Start() | ||
{ | ||
|
||
} | ||
|
||
// Update is called once per frame | ||
void Update() | ||
{ | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class SceneLoader : MonoBehaviour | ||
{ | ||
// Start is called before the first frame update | ||
void Start() | ||
{ | ||
|
||
} | ||
|
||
// Update is called once per frame | ||
void Update() | ||
{ | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace BKK.EditorSceneManagement | ||
{ | ||
[CreateAssetMenu(menuName = "BKK/Editor Scene Management/", fileName = "SceneLoaderData")] | ||
public class SceneLoaderData : ScriptableObject | ||
{ | ||
public string relativeFolderPath = "Assets/"; | ||
|
||
public List<SceneData> sceneDataList = new List<SceneData>(); | ||
|
||
public LoadSceneMode loadSceneMode = LoadSceneMode.Single; | ||
|
||
public bool autoLoadSceneOnPlay = false; | ||
|
||
public int targetSceneEnumIndex = 0; | ||
|
||
/// <summary> | ||
/// Scene Loader Data를 생성한다. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static SceneLoaderData CreateData() | ||
{ | ||
ScriptableObject instance = CreateInstance(typeof(SceneLoaderData)); | ||
|
||
string absoluteDataPath = Application.dataPath + SceneLoaderPath.defaultDataFolderPath; | ||
|
||
if (!Directory.Exists(absoluteDataPath)) | ||
{ | ||
Directory.CreateDirectory(absoluteDataPath); | ||
} | ||
|
||
AssetDatabase.CreateAsset(instance, SceneLoaderPath.defaultDataFilePath); | ||
|
||
return Resources.Load<SceneLoaderData>(SceneLoaderPath.defaultResourcesFilePath); | ||
} | ||
|
||
/// <summary> | ||
/// Scene Loader Data를 가져온다. | ||
/// 없을 경우 CreateData()로 생성해서 가져온다. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static SceneLoaderData GetData() | ||
{ | ||
SceneLoaderData data = Resources.Load<SceneLoaderData>(SceneLoaderPath.defaultResourcesFilePath); | ||
|
||
return data ? data : CreateData(); | ||
} | ||
|
||
/// <summary> | ||
/// 플레이시 자동 로드할 Scene의 경로를 가져온다. | ||
/// </summary> | ||
/// <returns></returns> | ||
public string GetAutoLoadTargetScenePath() | ||
{ | ||
if (sceneDataList.Count == 0) return string.Empty; | ||
|
||
return sceneDataList[targetSceneEnumIndex].scenePath; | ||
} | ||
|
||
/// <summary> | ||
/// 지정한 폴더 내에 모든 Scene 에셋을 각각 SceneData로 저장하여 sceneDataList에 저장한다. | ||
/// </summary> | ||
public void GetScenePathInFolder() | ||
{ | ||
string[] scenePaths = Directory.GetFiles(Application.dataPath + relativeFolderPath.Substring(6),"*.unity"); | ||
|
||
sceneDataList.Clear(); | ||
|
||
for (int i = 0; i < scenePaths.Length; i++) | ||
{ | ||
string sceneName = scenePaths[i].Substring(scenePaths[i].LastIndexOf('/') + 1); | ||
string scenePath = scenePaths[i].Substring(scenePaths[i].IndexOf("Assets", StringComparison.Ordinal)); | ||
sceneDataList.Add(new SceneData(sceneName, scenePath)); | ||
} | ||
} | ||
} | ||
|
||
[System.Serializable] | ||
public class SceneData | ||
{ | ||
public string sceneName = ""; | ||
|
||
public string scenePath = ""; | ||
|
||
public SceneData(string name, string path) | ||
{ | ||
sceneName = name; | ||
scenePath = path; | ||
} | ||
} | ||
|
||
public static class SceneLoaderPath | ||
{ | ||
public const string defaultDataFilePath = "Assets/Resources/SceneLoader/SceneLoaderData.asset"; | ||
public const string defaultDataFolderPath = "/Resources/SceneLoader/"; | ||
public const string defaultResourcesFilePath = "SceneLoader/SceneLoaderData"; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
using System; | ||
using UnityEditor; | ||
using UnityEditor.SceneManagement; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace BKK.EditorSceneManagement | ||
{ | ||
public class SceneLoaderWindow : EditorWindow | ||
{ | ||
private SceneLoaderData sceneLoaderData; | ||
|
||
private static readonly string windowTitle = "Scene Loader"; | ||
private readonly string pathLabel = "Path: "; | ||
private readonly string chooseSceneFolderLabel = "Choose Scene Folder"; | ||
private readonly string autoLoadOnPlayLabel = "Auto Load On Play"; | ||
private readonly string targetSceneLabel = "Target Scene"; | ||
private readonly string loadSceneModeLabel = "Load Scene Mode"; | ||
private readonly string noSceneInPathLabel = "No Scene in Path."; | ||
|
||
private GUIStyle warningTextStyle; | ||
|
||
/// <summary> | ||
/// Scene Loader Window 메뉴 클릭시 윈도우를 연다. | ||
/// </summary> | ||
[MenuItem("BKK/Editor Scene Management/Scene Loader")] | ||
private static void Init() | ||
{ | ||
SceneLoaderWindow window = GetWindow<SceneLoaderWindow>(); | ||
window.titleContent = new GUIContent(windowTitle); | ||
} | ||
|
||
/// <summary> | ||
/// InitializeOnLoadMethod에 의해 유니티 에디터 로드 중에 호출된다. | ||
/// 플레이 모드 변경시 호출할 LoadDefaultScene 메서드를 등록한다. | ||
/// </summary> | ||
[InitializeOnLoadMethod] | ||
private static void InitAutoLoadScene() | ||
{ | ||
EditorApplication.playModeStateChanged += LoadDefaultScene; | ||
} | ||
|
||
/// <summary> | ||
/// Edit | ||
/// </summary> | ||
/// <param name="state"></param> | ||
private static void LoadDefaultScene(PlayModeStateChange state) | ||
{ | ||
// Edit 모드 나갈때 현재 수정 상태를 저장한다. | ||
if (state == PlayModeStateChange.ExitingEditMode) | ||
{ | ||
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo(); | ||
} | ||
|
||
// 플레이 모드에 진입할때 autoLoadSceneOnPlay가 True이면 목표 Scene을 로드한다. | ||
if (state == PlayModeStateChange.EnteredPlayMode) | ||
{ | ||
SceneLoaderData sceneLoaderData = SceneLoaderData.GetData(); | ||
|
||
if(!sceneLoaderData.autoLoadSceneOnPlay) return; | ||
|
||
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(sceneLoaderData.loadSceneMode); | ||
|
||
EditorSceneManager.LoadSceneAsyncInPlayMode(sceneLoaderData.GetAutoLoadTargetScenePath(), loadSceneParameters); | ||
} | ||
} | ||
|
||
private void OnEnable() | ||
{ | ||
// Scene Loader Data의 존재 여부 확인. 없으면 생성. | ||
sceneLoaderData = Resources.Load<SceneLoaderData>(SceneLoaderPath.defaultResourcesFilePath); | ||
|
||
if (sceneLoaderData == null) | ||
{ | ||
sceneLoaderData = SceneLoaderData.CreateData(); | ||
} | ||
|
||
sceneLoaderData.GetScenePathInFolder();// 폴더 내 Scene 에셋들의 경로를 가져온다. | ||
|
||
// 폴더에 Scene 에셋이 없을때 출력할 안내문 스타일. | ||
if (warningTextStyle == null) | ||
warningTextStyle = new GUIStyle | ||
{ | ||
normal = | ||
{ | ||
textColor = Color.yellow, | ||
}, | ||
}; | ||
} | ||
|
||
private void OnGUI() | ||
{ | ||
Draw(); | ||
} | ||
|
||
private void Draw() | ||
{ | ||
sceneLoaderData.GetScenePathInFolder(); | ||
|
||
if(GUILayout.Button(chooseSceneFolderLabel)) | ||
{ | ||
string newPath = GetFolderPath(); | ||
|
||
if (!string.IsNullOrEmpty(newPath)) | ||
sceneLoaderData.relativeFolderPath = newPath; | ||
} | ||
|
||
GUILayout.Label(pathLabel + sceneLoaderData.relativeFolderPath); | ||
|
||
if (sceneLoaderData.sceneDataList.Count == 0) | ||
{ | ||
EditorGUILayout.LabelField(noSceneInPathLabel, warningTextStyle); | ||
return; | ||
} | ||
|
||
DrawSceneList(); | ||
DrawPlayModeMenu(); | ||
} | ||
|
||
private void DrawSceneList() | ||
{ | ||
DrawLine(); | ||
|
||
for (int i = 0; i < sceneLoaderData.sceneDataList.Count; i++) | ||
{ | ||
string sceneName = sceneLoaderData.sceneDataList[i].sceneName; | ||
|
||
if (GUILayout.Button(sceneName)) | ||
{ | ||
EditorSceneManager.OpenScene(sceneLoaderData.sceneDataList[i].scenePath); | ||
} | ||
} | ||
DrawLine(); | ||
} | ||
|
||
private void DrawPlayModeMenu() | ||
{ | ||
sceneLoaderData.autoLoadSceneOnPlay = EditorGUILayout.Toggle(autoLoadOnPlayLabel, sceneLoaderData.autoLoadSceneOnPlay); | ||
|
||
if (sceneLoaderData.autoLoadSceneOnPlay) | ||
{ | ||
string[] optionList = new string[sceneLoaderData.sceneDataList.Count]; | ||
|
||
for (int i = 0; i < sceneLoaderData.sceneDataList.Count; i++) | ||
{ | ||
optionList.SetValue(sceneLoaderData.sceneDataList[i].sceneName, i); | ||
} | ||
|
||
sceneLoaderData.targetSceneEnumIndex = EditorGUILayout.Popup(targetSceneLabel, sceneLoaderData.targetSceneEnumIndex, optionList); | ||
|
||
sceneLoaderData.loadSceneMode = (LoadSceneMode)EditorGUILayout.EnumPopup(loadSceneModeLabel, sceneLoaderData.loadSceneMode); | ||
} | ||
} | ||
|
||
private void DrawLine() | ||
{ | ||
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); | ||
} | ||
|
||
/// <summary> | ||
/// 폴더 경로 지정 패널을 열고 지정한 경로를 리턴한다. | ||
/// </summary> | ||
/// <returns></returns> | ||
private string GetFolderPath() | ||
{ | ||
string absolutePath = EditorUtility.OpenFolderPanel(chooseSceneFolderLabel, Application.dataPath, ""); | ||
|
||
if (string.IsNullOrEmpty(absolutePath)) return string.Empty; | ||
|
||
return absolutePath.Substring(absolutePath.IndexOf("Assets", StringComparison.Ordinal)); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.