在第一回中”一看就会的PhotonServer使用入门(一)“讲了服务端的使用。
在第二回中”一看就会的PhotonServer使用入门(二)“讲了客户端的使用。
而这回,就讲用Unity做为客户端来与 PhotonServer 通信的使用。
其实和第二回中差不多,如下所示:
using UnityEngine;
using ExitGames.Client.Photon;
using System.Collections.Generic;
using System.Collections;
public class D类 : MonoBehaviour, IPhotonPeerListener //
{
private PhotonPeer peer;
private bool isConnected = false; //
void Start()
{
peer = new PhotonPeer(this, ConnectionProtocol.Tcp);
peer.Connect("服务端地址:端口", "服务端名(Application Name)");
}
void Update()
{
peer.Service();
}
public void DeBugReturn(DebugLeyel level, string message) //日志回调,日志输出
{
}
public void OnEvent(EventData eventData) //
{
}
public void OnOperationResponse(OperationResponse operationResponse) //得到服务端响应时被调用
{
Dictionary<byte, object> dic = operationResponse.Parameters; //
}
public void OnStatusChanged(StatusCode statusCode) //连接状态改变时被调用
{
isConnected = (statusCode == StatusCode.Connect); //为true表示连接成功
}
void Send() //
{
if(!isConnected) return;
Dictionary<byte, object> dic = new Dictionary<byte, object>(); //该Dictionary用于存储参数
byte operationCode = 0; //操作码,是byte类型,允许-128~127之间
dic.Add(operationCode, "消息一");
operationCode = 1;
dic.Add(operationCode, "消息二");
operationCode = 0;
peer.OpCustom(operationCode, dic, true); //向服务端发起请求,参数true表示要确保发到服务端
}
}
不难看出,除了多了个 MonoBehaviour 以外,其它都大径相同,至少在使用上几乎没有变化。
待补充。