Search Unity

MainMenu to Multiplayer Map

Discussion in 'Multiplayer' started by iEntity, Mar 21, 2015.

  1. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    So in my multiplayer game, you start out at the main menu. If you click the button to join a specific map, you go there. I have put that in there by using a simple Application.LoadLevel("Map1");

    But when I load the level it just sits there at the standbyCamera and doesn't Join/Create a room like it usually does.

    Now when I start at the map scene it does the usual and creates/joins a room...

    Where am I going wrong??
     
  2. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    You should try to isolate Menu scene and try with empty scene with just a script that does Application.LoadLevel to your map and that's it and see if it has something to do with the menu scene.
     
  3. hazeyy

    hazeyy

    Joined:
    Feb 24, 2015
    Posts:
    4
    Well it sounds like your level is loading because your camera loads. It must be something with your network script that starts whenever your scene is created.
     
  4. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LevelLoader : MonoBehaviour {
    6.  
    7.     public string[] supportedNetworkLevels = new string[] {"Start", "Menu", "Map1"};
    8.     public string disconnectedLevel = "Menu";
    9.     [HideInInspector]
    10.     public int lastLevelPrefix = 0;
    11.  
    12.     void Awake()
    13.     {
    14.         if(Application.loadedLevelName == "Start")
    15.         {
    16.               Application.LoadLevel("Menu");
    17.         }
    18.         // Network level loading is done in a separate channel.
    19.         DontDestroyOnLoad(this);
    20.         GetComponent<NetworkView>().group = 1;
    21.         Application.LoadLevel(disconnectedLevel);
    22.     }
    23.     void OnGUI()
    24.     {
    25.         GUI.Label(new Rect(100, 0, 200, 200), "Prefix: " + lastLevelPrefix);
    26.     }
    27.     public void Load(string level)
    28.     {
    29.         GetComponent<NetworkView>().RPC("LoadLevel", RPCMode.AllBuffered, level, Random.Range(0,9999));
    30.     }
    31.     [RPC]
    32.     void LoadLevel (string level, int levelPrefix)
    33.     {
    34.         MasterServer.UnregisterHost();
    35.         lastLevelPrefix = levelPrefix;
    36.         // There is no reason to send any more data over the network on the default channel,
    37.         // because we are about to load the level, thus all those objects will get deleted anyway
    38.         Network.SetSendingEnabled(0, false);
    39.  
    40.         // We need to stop receiving because first the level must be loaded first.
    41.         // Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
    42.         Network.isMessageQueueRunning = false;
    43.  
    44.         // All network views loaded from a level will get a prefix into their NetworkViewID.
    45.         // This will prevent old updates from clients leaking into a newly created scene.
    46.         Network.SetLevelPrefix(levelPrefix);
    47.         Application.LoadLevel(level);
    48.  
    49.         StartCoroutine(DelayLoad(level));
    50.     }
    51.     IEnumerator DelayLoad(string level)
    52.     {
    53.         yield return new WaitForSeconds(0.05f);
    54.         //This is when the new map has been loaded
    55.  
    56.         // Allow receiving data again
    57.         Network.isMessageQueueRunning = true;
    58.         // Now the level has been loaded and we can start sending out data to clients
    59.         Network.SetSendingEnabled(0, true);
    60.  
    61.         GameObject[] gos = FindObjectsOfType(typeof(GameObject)) as GameObject[];
    62.         for (int i = 0; i < gos.Length; i++)
    63.         {
    64.             gos[i].SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
    65.         }
    66.     }
    67.     void OnDisconnectedFromServer ()
    68.     {
    69.         Application.LoadLevel(disconnectedLevel);
    70.     }
    71. }
    72.  
    73.  
    Create an empty scene named Start, and have only one object in there with this script attached (and a network view component). Then go to build settings and make sure that the Start scene is the first to load, and make sure to start the game from the Start scene. The disconnected scene, put your Menu scene name there. Just make sure you dont load the Start scene by accident.

    To load the map over the network just call this script and do Load("MyMapName"), make sure that the map you load is in the supported network levels.