Photon Server与unity客户端连接

来源:互联网 发布:日本水知道答案知乎 编辑:程序博客网 时间:2024/06/18 16:08

Photon Server与unity客户端连接

  • 1.将lib文件夹下Photon3Unity3D.dll动态链接库引入unity中Plugins文件夹
  • 2.在unity中创建Photon连接游戏对象并挂载单例脚本:
using UnityEngine;public class PhotonEngine : MonoBehaviour{    private static PhotonEngine _instance;    void Awake() {        if (_instance == null) {            _instance = this;            DontDestroyOnLoad(gameObject);        }        else if (_instance != this) {            Destroy(gameObject);            return;        }    }}
  • 3.unity客户端与Photon服务器端连接测试,unity脚本代码如下:
using UnityEngine;using ExitGames.Client.Photon;using System;public class PhotonEngine : MonoBehaviour,IPhotonPeerListener{    private static PhotonEngine _instance;    public PhotonPeer peer;    public void DebugReturn(DebugLevel level, string message) {    }    public void OnEvent(EventData eventData) {    }    public void OnOperationResponse(OperationResponse operationResponse) {    }    public void OnStatusChanged(StatusCode statusCode) {        Debug.Log(statusCode);    }    void Awake() {        if (_instance == null) {            _instance = this;            DontDestroyOnLoad(gameObject);        }        else if (_instance != this) {            Destroy(gameObject);            return;        }    }    void Start() {        // 创建PhotonPeer对象,参数为监听和协议,采用本实例作为监听者接收服务器响应,实现IPhotonPeerListener接口        peer = new PhotonPeer(this,ConnectionProtocol.Udp);        // 利用ip与端口号,以及应用名建立连接        peer.Connect("127.0.0.1:5055", "MyGame1");    }    void Update() {        // 无论peer处于什么状态,都要调用Service方法        peer.Service();    }    void OnDestroy() {        if (peer != null & peer.PeerState == PeerStateValue.Connected) {            peer.Disconnect();        }    }}
  • 4.请求、响应与事件:
// 客户端发起请求    void Update () {        if (Input.GetMouseButtonDown(0)) {            SendRequest();        }    }    private void SendRequest() {        Dictionary<byte, object> data = new Dictionary<byte, object>();        data.Add(1,"1号请求:好好学习天天向上");        PhotonEngine.Peer.OpCustom(1, data, true);    }
// 服务器端ClientPeer// 处理客户端请求并响应,发送一次事件protected override void OnOperationRequest(OperationRequest operationRequest,SendParameters sendParameters)        {            // OperationCode请求操作码,用于区分不同消息类型            switch (operationRequest.OperationCode) {                case (byte)DataKeyTest.TestKeyOne:                    // 接受请求                    ReceiveRequest(operationRequest);                    // 服务器响应                    OperationResponse operationResponse = new OperationResponse((byte)DataKeyTest.TestKeyOne);                    ExecuteResponse(operationResponse, sendParameters);                    // 发送事件                    Dictionary<byte, object> data = new Dictionary<byte, object>();                    data.Add(1,"发送事件");                    EventData eventData = new EventData(1,data);                    SendEvent(eventData,new SendParameters());                    MyGameServer.log.Info("事件已发送。。。");                    break;                case (byte)DataKeyTest.TestKeyTwo:                    break;                default:                    break;            }        }        // 接受请求        private void ReceiveRequest(OperationRequest operationRequest) {            Dictionary<byte, object> data = operationRequest.Parameters;            object value = null;             data.TryGetValue((byte)DataKeyTest.TestKeyOne,out value);            MyGameServer.log.Info(DataKeyTest.TestKeyOne+"类型请求参数:" +value+"已接收");        }        // 响应        private void ExecuteResponse(OperationResponse operationResponse, SendParameters sendParameters) {            Dictionary<byte, object> data = new Dictionary<byte, object>();            data.Add((byte)DataKeyTest.TestKeyOne, "服务器发送1号响应至客户端.txt");            operationResponse.SetParameters(data);            SendOperationResponse(operationResponse, sendParameters);            MyGameServer.log.Info(DataKeyTest.TestKeyOne + "消息已响应");        }
// 客户端收到响应与事件    public void OnOperationResponse(OperationResponse operationResponse) {        switch (operationResponse.OperationCode) {            case 1:                Dictionary<byte,object> data = operationResponse.Parameters;                object obj = null;                data.TryGetValue(1,out obj);                Debug.Log("收到1号响应:"+obj.ToString());                break;            case 2:                Debug.Log("收到2号响应");                break;            default:                break;        }    }        public void OnEvent(EventData eventData) {        switch (eventData.Code)        {            case 1:                Dictionary<byte,object> data = eventData.Parameters;                object obj = null;                data.TryGetValue(1,out obj);                Debug.Log(obj.ToString());                break;            default:                break;        }    }
原创粉丝点击