유니티 Attribute - 에디터 플레이 후 씬 자동 저장 (Unity Auto Saving In Editor)
InitializeOnLoad를 이용하면 Play 실행 시 에디터의 코드를 실행할 수 있다.
아래의 스크립트를 누르고 Play를 눌러보자.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
public class AutoSaveSceneBeforePlay : MonoBehaviour
{
static AutoSaveSceneBeforePlay()
{
Debug.Log("Up and running");
}
}
이제 playModeStateChanged에 이벤트를 추가해보자.
플레이 버튼을 누르고 종료하면 아래와 같은 로그를 볼 수 있다.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
public class AutoSaveSceneBeforePlay : MonoBehaviour
{
static AutoSaveSceneBeforePlay()
{
EditorApplication.playModeStateChanged += saveCurrentScene;
}
private static void saveCurrentScene(PlayModeStateChange state)
{
Debug.Log(state);
}
}
Play 버튼을 눌러 Editor의 State가 변경될 때마다 로그가 호출되고 있다.
이제 저장하는 코드를 작성해보자.
현재 에디터를 실행하면 Scene을 저장하려고 한다.
이것은 EditorApplication의 isPlaying과 isPlayingOrWillChangePlaymode로 판단할 수 있다.
isPlaying은 에디터가 재생 모드인지 알 수 있는 bool 값이다.
isPlayingOrWillChangePlaymode은 에디터가 현재 재생 모드 또는 재생 모드로 전환하려는지의 여부다.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
public class AutoSaveSceneBeforePlay : MonoBehaviour
{
static AutoSaveSceneBeforePlay()
{
EditorApplication.playModeStateChanged += saveCurrentScene;
}
private static void saveCurrentScene(PlayModeStateChange state)
{
Debug.Log("state : " + state
+ "/ isPlaying : " + EditorApplication.isPlaying
+ "/ isPlayingOrWillChangePlaymode : " + EditorApplication.isPlayingOrWillChangePlaymode);
}
}
에디터를 실행하고 종료하면 아래와 같은 로그를 볼 수 있다.
따라서 현재 실행중이 아니고(isPlaying == false)
Playmode로 전환하려고 할 때(isPlayingOrWillChangePlaymode == true) 현재의 씬을 저장한다.
현재 씬은 GetActiveScene으로 구할 수 있다.
if (EditorApplication.isPlaying == false
&& EditorApplication.isPlayingOrWillChangePlaymode == true)
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
최종 코드는 아래와 같다.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
public class AutoSaveSceneBeforePlay : MonoBehaviour
{
static AutoSaveSceneBeforePlay()
{
EditorApplication.playModeStateChanged += saveCurrentScene;
}
private static void saveCurrentScene(PlayModeStateChange state)
{
Debug.Log("state : " + state
+ "/ isPlaying : " + EditorApplication.isPlaying
+ "/ isPlayingOrWillChangePlaymode : " + EditorApplication.isPlayingOrWillChangePlaymode);
if (EditorApplication.isPlaying == false
&& EditorApplication.isPlayingOrWillChangePlaymode == true)
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
}
}
아래와 같이 재생 버튼을 누르면 TestScenes의 *이 사라지는 것을 볼 수 있다.
Unity Plus:
Easy 2D, 3D, VR, & AR software for cross-platform development of games and mobile apps. - Unity Store
Have a 2D, 3D, VR, or AR project that needs cross-platform functionality? We can help. Take a look at the easy-to-use Unity Plus real-time dev platform!
store.unity.com
Unity Pro:
Unity Pro
The complete solutions for professionals to create and operate.
unity.com
Unity 프리미엄 학습:
Unity Learn
Advance your Unity skills with live sessions and over 750 hours of on-demand learning content designed for creators at every skill level.
unity.com