コード例 #1
0
        /// <summary>
        /// Triggers when the server sends a messasge. The message is obtained through e.data which is an IndexedDictionary<string, object>.
        /// For this example, data["command"] specifies the type of the command.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnData(object sender, MessageEventArgs e)
        {
            print("message recieved");
            IndexedDictionary <string, object> data = (IndexedDictionary <string, object>)e.data;

            data.ToList().ForEach(x => print(x.Key + " -> " + x.Value));
            if (data.ContainsKey("command"))
            {
                switch (data["command"].ToString())
                {
                // For this example project, player data is sent when the server is ready to start the game
                case "playerData":
                {
                    // Server always send primitives as strings so variables should be parsed
                    float            x                = float.Parse(data["x"].ToString());
                    float            y                = float.Parse(data["y"].ToString());
                    int              color            = int.Parse(data["color"].ToString());
                    string           name             = data["name"].ToString();
                    PlayerController playerController = Instantiate(playerPrefab
                                                                    , new Vector3(x, y, 0f)
                                                                    , Quaternion.identity
                                                                    , playerParentTransform)
                                                        .GetComponent <PlayerController>();
                    playerController.netHand = this;
                    playerController.SetColor(color);
                    playerController.SetName(name);

                    if (data["id"].ToString() != client.id)
                    {
                        localPlayer = playerController;
                    }
                    else
                    {
                        remotePlayer = playerController;
                        remotePlayer.interactable = false;
                    }
                    textHelper.text = "";
                    messageHand.Ready();
                }
                break;

                case "message":
                {
                    string message = data["message"].ToString();
                    GetPlayerByID(data["id"].ToString()).AddMessage(message);
                }
                break;

                case "oppLeft":
                {
                    OnApplicationQuit();
                    SceneManager.LoadScene("ExampleScene");
                }
                break;

                // For this example, server sends the private game ID to the client who created the private game for other people
                //		to connect the game
                case "privateGameID":
                {
                    textHelper.text = "Private Game ID is " + data["privateGameID"] + " and it is copied to clipboard";
                    TextEditor textEditor = new TextEditor
                    {
                        text = data["privateGameID"].ToString()
                    };
                    textEditor.SelectAll();
                    textEditor.Copy();
                }
                break;
                }
            }
        }