x


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?

more ▼

asked May 11 '10 at 07:16 PM

Eric 1 gravatar image

Eric 1
83 4 4 10

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 11 '10 at 07:29 PM

cob gravatar image

cob
127 1 1 4

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 MonoBehaviour in a new, clean Unity scene.

Thanks again.

May 11 '10 at 08:57 PM Eric 1

oh, sorry... BuildAssetBundles... I meant to rename it to something generic but missed it. :)

May 12 '10 at 12:54 PM cob
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3314
x1662
x34

asked: May 11 '10 at 07:16 PM

Seen: 2496 times

Last Updated: May 11 '10 at 07:16 PM