Unity3D 客户端编程

Photon Server 和 Unity3D 数据交互:

Photon Server 服务端编程

Unity3D 客户端编程

VS2017 之 MYSQL实体数据模型

1:打开unity新建新项目,并引入Photon3Unity3D.dll到plugins文件中。

 

 

2、新建一个空物体,添加两个脚本文件。

 

 

 3、编辑Photon Engine。

  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 using ExitGames.Client.Photon;
  5 using System;
  6 
  7 public class PhotonEngine : MonoBehaviour, IPhotonPeerListener
  8 {
  9     //单例模式
 10     private static PhotonEngine instance;
 11     private static PhotonPeer peer;
 12 
 13     public static PhotonPeer Peer
 14     {
 15         get
 16         {
 17             return peer;
 18         }
 19     }
 20 
 21     private void Awake()
 22     {
 23         if (instance == null)
 24         {
 25             instance = this;
 26             //不需要销毁
 27             DontDestroyOnLoad(this.gameObject);
 28         }
 29         else if (instance != this)
 30         {
 31             Destroy(this.gameObject);
 32             return;
 33         }
 34     }
 35     private void Start()
 36     {
 37         try
 38         {
 39             //PhotonPeer实例化时需要监听类和连接协议。所以该类继承IPhotonPeerListener接口实现监听
 40             peer = new PhotonPeer(this, ConnectionProtocol.Udp);
 41             peer.Connect("122.237.104.105:5055", "Mygame1");
 42         }
 43         catch (Exception e)
 44         {
 45             Debug.Log(e.ToString());
 46         }
 47     }
 48 
 49     private void Update()
 50     {
 51         //时刻发送请求
 52         peer.Service();
 53     }
 54 
 55     private void OnDestroy()
 56     {
 57         if (peer != null && peer.PeerState == PeerStateValue.Connected)
 58         {
 59             peer.Disconnect();
 60         }
 61     }
 62 
 63     public void DebugReturn(DebugLevel level, string message)
 64     {
 65         
 66     }
 67 
 68     public void OnEvent(EventData eventData)
 69     {
 70         switch (eventData.Code)
 71         {
 72             case 1:
 73                 Dictionary<byte, object> di = eventData.Parameters;
 74                 object i;
 75                 di.TryGetValue(2, out i);
 76                 Debug.Log("收到服务器消息" + i.ToString());
 77                 break;
 78             case 2:
 79                 break;
 80             default:
 81                 break;
 82         }
 83 
 84     }
 85 
 86     public void OnOperationResponse(OperationResponse operationResponse)
 87     {
 88         switch (operationResponse.OperationCode)
 89         {
 90             case 1:
 91                 Debug.Log("收到服务器响应!。。。。。");
 92                 Dictionary<byte, object> d = operationResponse.Parameters;
 93                 object i, j;
 94                 d.TryGetValue(1, out i);
 95                 d.TryGetValue(2, out j);
 96                 Debug.Log("收到服务器返回" + i.ToString() + j.ToString());
 97                 break;
 98             default:
 99                 break;
100         }
101     }
102 
103     public void OnStatusChanged(StatusCode statusCode)
104     {
105         Debug.Log(statusCode);
106     }
107 }

4、text类用来发送请求。

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using UnityEngine;
 5 
 6 public class text : MonoBehaviour {
 7 
 8 
 9     private void Update()
10     {
11         if (Input.GetMouseButtonDown(0))
12         {
13             SendRequest();
14         }
15     }
16 
17     private void SendRequest()
18     {
19         Dictionary<byte, object> date = new Dictionary<byte, object>();
20         date.Add(1, 1000);
21         date.Add(2,"sdgsdgdsfg");
22         PhotonEngine.Peer.OpCustom(1, date, true);
23     }
24 }

5、运行结果。先启动服务器在启动客户端,鼠标左点击发送请求。

服务端:

 

 

客户端:

 

 下载地址:https://gitee.com/today6/unity

posted @ 2019-09-05 01:13  无名之士  阅读(1143)  评论(0编辑  收藏  举报