Get if Editor is in main or vm instance?

I am looking for a way for my editor to see whether its an additional instance or the main window, what would be an easy way?

Simple explained, because mppm starts an new instance with the current open scenes, and our start scene is always empty, it runs a startup sequence. but this is not duplicated to the extra instance, so i need to manually trigger it from the empty scene, but i do not want to trigger the sequence again on my main instance.

I also use 1.3, but I want to give my users the options to run it with Playmode scenario or default mode.

Tags is one way, but i’d like to reduce the amount of setup needed for others that use our package.

Simply just looking for a simple way to do a check – if extra instance do code…

I did find this, is this the reliable way, or is there an better one you have ready?

string[] args = Environment.GetCommandLineArgs();
bool isVirtualClone = Array.Exists(args, arg => arg == "--virtual-project-clone");

Thanks!

I use tags. I have an editor script that prefills the MPPM tag list (I had to use reflection though) in Project Settings with these tags: Server, Host, Client, Offline

It’s then up to the user, as it should be, to set the correct tags to the VPs. But at least the tags already exist.

I have an editor script on a MonoBehaviour in may startup scene that, every time the startup scene opens, it sets that (currently active) scene as the playmodeStartScene.

This is great generally because I can edit any additive scene by itself and hit the playmode button and I won’t be entering a partial scene where all sorts of content is missing to make it functional.

There’s also a tool menu that simply sets the start scene to null for cases where you do want to enter playmode only in the current scene, eg a demo scene of an asset.

My startup sequence is always followed the same way, title screen, launch screen, main menu. Except in develop mode/editor the pre-menu scenes exit after the first frame. This guarantees that the flow of exection is always the same while not affecting the startup time.

Upon entering the main menu, the same script that starts networking through a button would also be triggered if MPPM is active and has the appropriate tags set. Essentially the MPPM tags just “execute buttons” that are already in the main menu (or not, whether a button exists is optional).

So every time I hit play I go through title, launch, menu, online scenes and I’m in the networked game in a fraction of a second. All content is additively loaded - there’s the startup scene which never ever changes and contains all global objects. I do not use single-scene loading anywhere and in fact I have a SceneManagerAPI script that throws an exception if any code anywhere ever where to call LoadScene with single mode.

So far it’s been the best way of working with Unity ever! :slight_smile:

I Prefer to not use tags at all since I don’t need to, I like to be as unintrusive as I can ^^ the users can still setup their tags and such is my idea. also I would have to write code like you say, to auto populate that list, but also handle when the user accedently unflags it, may not be much but more then needed :slight_smile:

So currently this works for what we do, later I will really implement it properly.
This only runs the startup on the clone, and I dont have to add to tags.

public class MPPM_Booter : MonoBehaviour
{
    [SerializeField] Profile targetProfile;

#if UNITY_EDITOR
    [InitializeOnLoadMethod]
    ... populating targetProfile before playmode.
#endif

    void Start()
    {
        string[] args = Environment.GetCommandLineArgs();
        bool isVirtualClone = Array.Exists(args, arg => arg == "--virtual-project-clone");

        if (!isVirtualClone) return;

        SceneManager.Initialize();
        Profile.SetProfile(targetProfile);
        SceneManager.app.Restart();
        Application.runInBackground = true;
    }
}

But what I dont like is arg == “–virtual-project-clone”, as I am not confident in knowing this wont be changed in other versions.

I also tried is

if (UnityEditor.MPE.ProcessService.level == UnityEditor.MPE.ProcessLevel.Secondary)

Sadly the new window is still main, this would be perfect if set as second with an proxy like

Application.EditorLevel

First i did use 1.3 new Scenario with a boot scene, but since we start a boot scene before playmode anyways, it was not needed, but i still wanted to support them using that aswell.

This however I have not seen, where would that be? or are you referensing your workflow ? :slight_smile:

Yes, that’s my own menu item.

As to MPPM since 99% of the code is internal it’s either your solution or reflection. Both could change at any time, but likely won’t.

Hi @nuwn, we’ve identified this issue and are currently working on a new API to determine whether the running editor is the main one or a clone. Thank you for bringing this to our attention, should have an update soon.

2 Likes

Hi @Jia-Unity Any update on that API and where i could find it ? :slight_smile:

CurrentPlayer.IsMainEditor under the Unity.Multiplayer.Playmode namespace.

2 Likes