Photon Server and Unity3D data exchange:
Photon Server server-side programming
1: Open a new unity new project, and introduced into the plugins Photon3Unity3D.dll file.
2, create a new empty object, add two script files.
3, edit 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 is the Destroy ( the this .gameObject); 32 return ; 33 is } 34 is } 35 Private void the Start () 36 { 37 [ the try 38 is { 39 // need to monitor the connection and protocol type PhotonPeer instantiated. Therefore listener class inherits IPhotonPeerListener interface 40 Use the peer = new new PhotonPeer ( the this , ConnectionProtocol.Udp); 41 is 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 ( " receive message server " + 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 is Debug.Log ( " server response is received .....! " ); 92 the 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 class is used to send the request.
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, the operating results. Start the server start the client, the left mouse click to send the request.
Server:
Client: