Search Unity

One click game setup

Discussion in 'Multiplayer' started by Survivorman198, Nov 28, 2014.

  1. Survivorman198

    Survivorman198

    Joined:
    Nov 21, 2014
    Posts:
    25
    I am making a game and there needs to be a way to set up a server and join a server without so much scripting. I hope unity can do something like that like with 4.6 and the gui system.

    Or is there a JavaScript tutorial out there. any scripts or links would be grate.

    This thred will be a place to make your scripts and help thousands of noobs like my self. :rolleyes:
     
  2. Survivorman198

    Survivorman198

    Joined:
    Nov 21, 2014
    Posts:
    25
    Remember survival of the fittest

    Not my game name at all:D
     
  3. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Good luck
     
  4. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Please mods don't ban this guy! I love his threads!
     
    tobiass likes this.
  5. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    What?
     
  6. EmmaEwert

    EmmaEwert

    Joined:
    Mar 13, 2014
    Posts:
    30
    Hosting
    Code (CSharp):
    1. void Host(int players, int port, string room, string comment) {
    2.     Network.InitializeServer(players, port, !Network.HavePublicAddress());
    3.     MasterServer.RegisterHost("Main game name", room, comment);
    4. }
    Joining
    Code (CSharp):
    1. void Update() {
    2.     HostData[] hostList = MasterServer.PollHostList();
    3.     if (hostList.Length > 0) {
    4.         this.buttons.ForEach(button => Destroy(button.gameObject));
    5.         this.buttons = new List<Button>();
    6.         foreach (HostData hostData in hostList) {
    7.             HostData _hostData = hostData; // For onClick listener delegate
    8.             Button button = Instantiate<Button>(this.buttonPrefab);
    9.             button.transform.SetParent(this.serverListTransform);
    10.             button.GetComponentInChildren<Text>().text =
    11.                 "Join " + hostData.gameName;
    12.             button.onClick.AddListener(() => Network.Connect(_hostData));
    13.             this.buttons.Add(button);
    14.         }
    15.         MasterServer.ClearHostList();
    16.     }
    17. }
    Refreshing
    Code (CSharp):
    1. void Refresh() {
    2.     MasterServer.RequestHostList("Main game name");
    3. }
    Add a layout group (probably vertical) to this.serverListTransform, and add Host and Refresh buttons that call Host() and Refresh(), respectively.
     
    Last edited: Dec 21, 2014
    elmar1028 likes this.