0
\$\begingroup\$

I've written some simple stuff to make a lobby for matchmaking. Now I want to pass some extra info besides the match name to the lobby. Something like Player's avatar and a number of players connected and what not. Thereto, I also want to pass information to the game from the players. For example, two players connect to a match and the character pick starts. For this I want to have the player's info regarding the characters they've picked and the character's weapons info (e.g. customized weapons info). I store all that in a json file and load from it to a class object. Is it ok to add these classes to, say, the player connection prefab that holds Network Identity or is it done somehow differently?

Can someone maybe help me with that in the context of Unity 2018 (i.e. give some advice or referrences to some relevant information)?

Appreciate all the help and thanks in advance.

\$\endgroup\$
  • \$\begingroup\$ Hello and welcome to gamedevStackExchange please ask one question instead of asking many question inside one question. \$\endgroup\$ – Seyed Morteza Kamali Oct 26 '18 at 7:47
1
\$\begingroup\$

you can send your information to other players inside lobby:

instead of sending avatar image , send avatar index

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class SendInformation : NetworkBehaviour {
    public class Soldier{
        public int avatarId{get;set;}
        public string playerName{get;set;}
        public bool isReady{get;set;}
    }
    public Soldier soldier;
    public Sprite[] Avatars;
    public Image image;

    void Start () {
        CmdSendData(0,"Jack",true);
    }

    [Command]
    void CmdSendData(int avatarId,string playerName,bool isReady){
    RpcSendData(avatarId,playerName,isReady);
    }

    [ClientRpc]
    void RpcSendData(int avatarId,string playerName,bool isReady){
        soldier.avatarId = avatarId;
        soldier.playerName = playerName;
        soldier.isReady = isReady;

        image = Avatars[soldier.avatarId];
    }
}

to get number of players connected use this:

var numOfConnectedPlayers = Network.connections.Length;

if you want to send json try this:

before using this script you should add Newtonsoft.Json.dll to your Plugins folder

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using Newtonsoft.Json;

public class JsonTest : NetworkBehaviour {

    public class Soldier{
    public string id {get;set;}
    public string Name {get;set;}
    public bool Level {get;set;}
    }
    public Soldier soldier;

    private void Start() {
    string jsonString = "{\"id\":1, \"Name\":\"Jack\", \"Level\":0}";
    CmdSendData(jsonString);
    }

    [Command]
    public void CmdSendData (string json) {
    RpcSendData(json);
    }

    [ClientRpc]
    public void RpcSendData (string json) {
        var information = JsonConvert.DeserializeObject<Soldier>(json);
        soldier.id = information.id;
        soldier.Level = information.Level;
        soldier.Name = information.Name;
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.