- Home /
Programmatically Start Play Mode in the Editor
Is there a way to programatically start the play mode in the Unity editor in an editor script?
Answer by cob · May 11, 2010 at 07:29 PM
To launch from a menu item you would
[MenuItem("Play/PlayMe")]
public static void BuildAssetBundles()
{
EditorApplication.isPlaying = true;
EditorApplication.NewScene();
GameObject go = new GameObject("PlayMode");
go.AddComponent<PlaySomeMonoBehaviour>().WhatToDo =
PlaySomeMonoBehaviour.PlayMe;
}
In the PlaySomeMonoBehaviour class
public const string PlayMe = "PlayMe";
public class PlaySomeMonoBehaviour : MonoBehaviour {
public string WhatToDo = PlayMe;
void Start() { switch (WhatToDo) { case PlayMe: StartCoroutine((YetSomeOtherClass.DoSomething())); break; } }
YetSomeOtherClass is just another MonoBehaviour manipulating GameObjects.
That is the special sauce. Thanks. It is interesting that you named your sample class BuildAssetBundles...since I am trying to load AssetBundles to verify they are correct, and I cannot seen to do that in edit mode. Thanks!
Just to clarify for anyone else, doing:
EditorApplication.isPlaying = true;
Starts the play mode. The other code is a way to launch a specific $$anonymous$$onoBehaviour in a new, clean Unity scene.
Thanks again.
oh, sorry... BuildAssetBundles... I meant to rename it to something generic but missed it. :)
Answer by karsnen · May 21, 2019 at 05:53 PM
Yes dropping it here for anyone for an alternative that is awesome. http://wiki.unity3d.com/index.php/SceneAutoLoader