mercredi 15 juin 2016

Unity : networking part II

I create a tiny connection screen in a nex scene. This scene has no network, only a canvas to show a background picture, and panel/buttons.
To be exhaustive, I ve created a gameobject named PLayerOverScene I talked about in previous article. I linked to this gameobject, the playerscript script. I will store in it, some information, like the ip asked by the player.


Important field for me, now is IP and connect.
I will not speak about login and password for the moment. I will use a rest repository, I will connect to, to validate user/password in a further article.

As always, I will create a c# script to handle click button on connect : ShimConnection.
I create a single simple function:

 public void checkLogin()

I added the script as component to the button.
I create a onclick event on buttonscript component.


For now, when you click on connect button, you will call checkLogin function.

Travel

Now what you want is to load the game scene, and to link to the networked scene from server.

At connect click, I store the Ip into PlayerOverScene:

GameObject playerOverScene = GameObject.Find("PlayerOverScene");
PlayerScript plScript = playerOverScene.GetComponent<PlayerScript>();
plScript.setIpToGo(IP.text);

First of all, you have to load the new scene (I have only two scenes, so for the moment, I will put hardcoded name of the scene).

     SceneManager.LoadScene("scenes/level1");

The game scene will be loaded... now we have to connect to server.
I Will modify this previous code :
public void Start()
    {
         GameObject playerOverScene = GameObject.Find("PlayerOverScene");
        if (playerOverScene == null)
        {
            bool result =this.StartServer();
        }else
        {
            this.StartClient();
        }   
}

To


public void Start()
    {
         GameObject playerOverScene = GameObject.Find("PlayerOverScene");
        if (playerOverScene == null)
        {
            bool result =this.StartServer();
        }else
        {
           PlayerScript ps = playerOverScene.GetComponent<PlayerScript>();
            if (ps)
            {
                this.networkAddress = ps.getIpToGo();

            }
            this.StartClient();
        }   
}

I grab back the Ip store into playerOverScene object to determine to modify internal variable (networkAdress).
I start client, I am connected (I hope) to the server.

Quite simple...


Next step, I will describe how to spawn the player ship...

Aucun commentaire:

Enregistrer un commentaire