Skip to content

Instantly share code, notes, and snippets.

@gzlock
Last active March 11, 2019 15:06
Show Gist options
  • Save gzlock/84daf94f73f41afd62678c5c404f75ac to your computer and use it in GitHub Desktop.
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
/**
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");
}
}
@gzlock
Copy link
Author

gzlock commented Mar 11, 2019

1
2
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编辑器里开始调试场景时就会自动工作。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment