Search Unity

Networking: using the smartphone as a controller

Discussion in 'Multiplayer' started by ben-rasooli, Jul 19, 2014.

  1. ben-rasooli

    ben-rasooli

    Joined:
    May 1, 2014
    Posts:
    40
    mobile+controller.jpg
    Hi

    As you can see in the picture I'm trying to have different views/scenes/contents on each device. The host is showing one view and the other device showing another view with some buttons in it. The player controls the game using the smart phone.

    Most of the tutorials I've seen so far are all about having the same scene/view showing in all devices. Is it possible to implement such a design in Unity?
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,068
    Of course it's possible. In worst case, you'd detect the device for control and enable another branch of GOs in the (same) scene.
     
    ben-rasooli likes this.
  3. ben-rasooli

    ben-rasooli

    Joined:
    May 1, 2014
    Posts:
    40
    So this is what I did just in case anyone else is interested to know:
    I assume you already know the basic of networking in unity, if not then go ahead and read either this book (https://www.assetstore.unity3d.com/en/#!/content/234) or the unity documentation manual.

    I made a single scene with two cameras in it. Then chose one camera for viewing the game through(server) and the other camera for viewing my controller elements(client). I also used RPC to call functions on the server.

    More detail:
    Create a single scene with two cameras in it.
    To set one camera for server and the other camera for the client you can do this:

    //reference holders for cameras
    public Camera serverCamera;
    public Camera clientCamera;

    And of course you need to drag and drop each camera to these two filed in the unity editor.

    //set each camera
    void OnConnectedToServer(){
    serverCamera.enabled = false;
    clientCamera.enabled = true;
    }

    Then you can have different GUI for each camera by doing so:

    void OnGUI(){
    if (Network.peerType == NetworkPeerType.Disconnected){
    //some code here
    }
    else{
    if (Network.peerType == NetworkPeerType.Connecting){
    GUILayout.Label("Connecting...");
    }
    // differentiation between server and client
    else if (Network.isServer){
    //define your server GUI here.
    }
    else if (Network.isClient){
    //define your client GUI here
    }
    }
    }
     
    Liczyhrabia and Exurgent like this.
  4. Liczyhrabia

    Liczyhrabia

    Joined:
    May 15, 2017
    Posts:
    8
    Hi,

    I'm already trying to create similar app with one significant difference - i want to have dynamic part of smartphone interface which changes in depend on current game situation. Is that still possible using the technology that You used?

    And, if something changed, are RPCs still the best way for doing that kind of stuff?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This 3 year old thread only has example code from the obsolete legacy networking system. A new networking system was introduced in Unity 5.1 back in 2014. You should start a new thread.