Last active
March 11, 2019 15:06
-
-
Save gzlock/84daf94f73f41afd62678c5c404f75ac to your computer and use it in GitHub Desktop.
Load the initial scene when the Unity3d editor mode state change to play
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Please checkout the first comment. | |
请查看第一个留言。 | |
**/ | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
[InitializeOnLoad] | |
public class AutoLoadScene | |
{ | |
static AutoLoadScene() | |
{ | |
//EditorApplication.update += Update; | |
EditorApplication.playModeStateChanged += StateChanged; | |
} | |
private static void StateChanged(PlayModeStateChange playModeStateChange) | |
{ | |
if (playModeStateChange == PlayModeStateChange.EnteredPlayMode) | |
{ | |
var scene = SceneManager.GetActiveScene(); | |
if (scene.buildIndex != 0) | |
{ | |
SceneManager.LoadScene(0, LoadSceneMode.Additive); | |
} | |
} | |
} | |
static void Update() | |
{ | |
Debug.Log("Updating"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add your initial scene to 'Build Settings' and let it to be the first one.
Just put this script in the assets folder, it will work automatically when you start the play mode in Unity3d Editor.
添加你的初始化场景到"Build Settings"并确保序列为0。
只需要将这个代码脚本文件放入到项目的assets文件夹,当你在Unity3d编辑器里开始调试场景时就会自动工作。