前一章客户端与服务端连接成功,现在需要前后端进行数据传递。

 

一、前端发送消息。在项目Scripts目录中新建脚本 TestSer.cs。并挂载到相机上

Photon Server初识(六) --- 客户端与服务端消息传递

 

 

二、客户端发送数据给服务端。编辑客户端代码 TestSer.cs。让鼠标点击时发送消息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestSer : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //如果鼠标点击了
        if (Input.GetMouseButtonDown(0))
        {
            SendRequest();
        }
    }

    void SendRequest()
    {
        Debug.Log("点击了------");
        
        //包装需要发送的数据
        Dictionary<byte, object> data = new Dictionary<byte, object>();
        data.Add(1, 120);
        data.Add(2, "clent to ser");

        //向服务端发送数据,发送code是1,服务端需要用到
        PhotonManager.Peer.OpCustom(1, data, true);
    }
}
View Code

相关文章: