1.打开unity,在其中创建一个gameobject,创建一个test脚本挂在上面,用于点击测试向服务端发消息的测试
test.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour {
void Start () {
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
SendReauest();
}
}
private void SendReauest()
{
//构造参数
var data = new Dictionary<byte, object>();
//构造参数
data.Add(1,100);
data.Add(2,"这个是参数");
//发送
PhotonEngine.peer.OpCustom(1,data,true);
//协议 前端后端
PhotonEngine.peer.OpCustom(2,data,true);
PhotonEngine.peer.OpCustom(3,data,true);
PhotonEngine.peer.OpCustom(4,data,true);
PhotonEngine.peer.OpCustom(5,data,true);
}
}
2.在服务端的ClientPeer中,接受客户端发来的消息,输出到日志中,并且返回code,为客户端发送过来的code,返回参数,为客户端发送过来的参数。
3.在客户端的PhotonEngine中处理服务器返回的响应:
4.最后效果如下图所示:
客户端
服务端