微信登录

配置 - Unity简单客户端 - Photon3Unity3D.dll

作用:Unity客户端

客户端引入的JDK

C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\lib\Photon3Unity3D.dll
在Scenes同级创建文件夹Plugins(插件,不可以修改Plugins这个名字)放入Photon3Unity3D.dll

PhotonEngine.cs是集目标服务器地址、请求发送、请求接收、事件接收于一体的文件

创建空物体(测试)

命名PhotonEngine,加上脚本
修改MyGame1

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ExitGames.Client.Photon;
  5. public class PhotonEngine : MonoBehaviour,IPhotonPeerListener
  6. {
  7. public static PhotonPeer Peer
  8. {
  9. get
  10. {
  11. return peer;
  12. }
  13. }
  14. private static PhotonEngine Instance;//创建一个物体
  15. private static PhotonPeer peer;//做成一个字段
  16. //Instance判断有没有这个,有的话删除,没有就赋值,PhotonEngine只需要一个就可以了
  17. void Awake()
  18. {
  19. if (Instance == null)
  20. {
  21. Instance = this;
  22. }
  23. else if(Instance != this)
  24. {
  25. Destroy(this.gameObject);return;
  26. }
  27. }
  28. // Start is called before the first frame update
  29. void Start()
  30. {
  31. //通过Listener接收服务器端的响应
  32. peer = new PhotonPeer(this,ConnectionProtocol.Udp);//有监听器、有协议
  33. //ip地址、应用名字
  34. //ip:小托盘PhotonServer查到的第一个是本机地址,第二个是路由器局域网地址,第三个是外网
  35. //C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64\PhotonServer.config
  36. //ip后的端口号:是选择PhotonServer.config的Udp的端口号
  37. //应用名字:选择PhotonServer.config的Application无s的Name
  38. peer.Connect("10.211.55.5:5055", "MyGame1");
  39. }
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. //判断是否连接
  44. //if (peer.PeerState == PeerStateValue.Connected)
  45. //{
  46. peer.Service();//需要一直调用
  47. //}
  48. }
  49. //如果Unity关闭时可以断开连接
  50. void OnDestory()
  51. {
  52. if(peer != null && peer.PeerState == PeerStateValue.Connected)
  53. {
  54. peer.Disconnect();
  55. }
  56. }
  57. //以下4个是IPhotonPeerListener
  58. public void DebugReturn(DebugLevel level, string message)
  59. {
  60. throw new System.NotImplementedException();
  61. }
  62. //服务器端直接想客户端发送event事件信息
  63. public void OnEvent(EventData eventData)
  64. {
  65. throw new System.NotImplementedException();
  66. }
  67. //客户端发送到服务器端,传回来的数据类似Ajax
  68. public void OnOperationResponse(OperationResponse operationResponse)
  69. {
  70. switch (operationResponse.OperationCode)//通过OperationCode区分请求
  71. {
  72. case 1:
  73. Debug.Log("客户端收到了服务器端的响应OpCode1");
  74. Dictionary<byte, object> data = operationResponse.Parameters;
  75. object intValue;
  76. data.TryGetValue(1, out intValue);
  77. object stringValue;
  78. data.TryGetValue(2, out stringValue);
  79. Debug.Log("服务器收到回复:"+ intValue.ToString() + stringValue.ToString());
  80. break;
  81. case 2:
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. //连接state发生不一样时做的5种
  88. public void OnStatusChanged(StatusCode statusCode)
  89. {
  90. Debug.Log(statusCode);
  91. }
  92. }

脚本text.cs,点击鼠标OpCustom发送测试

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class text : MonoBehaviour
  5. {
  6. // Update is called once per frame
  7. void Update()
  8. {
  9. if (Input.GetMouseButtonDown(0))
  10. {
  11. SendRequest();
  12. }
  13. }
  14. void SendRequest()
  15. {
  16. Dictionary<byte, object> data = new Dictionary<byte, object>();
  17. data.Add(1, 100);//加数据
  18. data.Add(2, "aaBB中文");//加数据
  19. PhotonEngine.Peer.OpCustom(1,data, true);//调用OpCustom发送:1是case码,数据,一定要发送到
  20. Debug.Log("客户端发送了一个请求");
  21. }
  22. }

创建空物体(不测试)

  1. PhotonEngine.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ExitGames.Client.Photon;
  5. using Common;
  6. using Common.Tools;
  7. public class PhotonEngine : MonoBehaviour, IPhotonPeerListener
  8. {
  9. public static PhotonPeer Peer
  10. {
  11. get
  12. {
  13. return peer;
  14. }
  15. }
  16. public static PhotonEngine Instance;//创建一个物体
  17. private static PhotonPeer peer;//做成一个字段
  18. public static string username;//名字信息
  19. //
  20. //Photon连接
  21. //
  22. //Instance判断有没有这个,有的话删除,没有就赋值,PhotonEngine只需要一个就可以了
  23. void Awake()
  24. {
  25. if (Instance == null)
  26. {
  27. Instance = this;
  28. DontDestroyOnLoad(this.gameObject);
  29. }
  30. else if (Instance != this)
  31. {
  32. Destroy(this.gameObject); return;
  33. }
  34. }
  35. // 连接Photon
  36. void Start()
  37. {
  38. //通过Listener接收服务器端的响应
  39. peer = new PhotonPeer(this, ConnectionProtocol.Udp);//有监听器、有协议
  40. //ip地址、应用名字
  41. //ip:小托盘PhotonServer查到的第一个是本机地址,第二个是路由器局域网地址,第三个是外网
  42. //C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64\PhotonServer.config
  43. //ip后的端口号:是选择PhotonServer.config的Udp的端口号
  44. //应用名字:选择PhotonServer.config的Application无s的Name
  45. peer.Connect("10.211.55.5:5055", "Tetris");
  46. }
  47. // Update is called once per frame
  48. void Update()
  49. {
  50. //判断是否连接
  51. peer.Service();//需要一直调-
  52. }
  53. //如果Unity关闭时可以断开连接
  54. void OnDestroy()
  55. {
  56. if (peer != null && peer.PeerState == PeerStateValue.Connected)
  57. {
  58. peer.Disconnect();
  59. }
  60. }
  61. //以下4个是IPhotonPeerListener
  62. public void DebugReturn(DebugLevel level, string message)
  63. {
  64. throw new System.NotImplementedException();
  65. }
  66. //连接state发生不一样时做的5种
  67. public void OnStatusChanged(StatusCode statusCode)
  68. {
  69. Debug.Log(statusCode);
  70. }
  71. //
  72. //请求request
  73. //
  74. private Dictionary<OperationCode, BaseRequest> RequestDict = new Dictionary<OperationCode, BaseRequest>();//普通请求字典
  75. //请求-收-客户端发送到服务器端,传回来的数据类似Ajax
  76. public void OnOperationResponse(OperationResponse operationResponse)
  77. {
  78. //处理分发
  79. OperationCode opCode = (OperationCode)operationResponse.OperationCode;//获得code是谁
  80. BaseRequest request = null;
  81. bool temp = RequestDict.TryGetValue(opCode, out request);
  82. if (temp)
  83. {
  84. request.OnOperationResponse(operationResponse);
  85. }
  86. else
  87. {
  88. Debug.Log("没找到对应的响应处理对象");
  89. }
  90. }
  91. //请求-request-添加移除
  92. public void AddRequest(BaseRequest request)
  93. {
  94. RequestDict.Add(request.OpCode, request);
  95. }
  96. public void RemoveRequest(BaseRequest request)
  97. {
  98. RequestDict.Remove(request.OpCode);
  99. }
  100. //
  101. //事件Event
  102. //
  103. private Dictionary<EventCode, BaseEvent> EventDict = new Dictionary<EventCode, BaseEvent>();//事件字典
  104. //服务器端直接向客户端发送event事件信息
  105. public void OnEvent(EventData eventData)
  106. {
  107. EventCode code = (EventCode)eventData.Code;
  108. BaseEvent e = DictTool.GetValue<EventCode, BaseEvent>(EventDict, code);
  109. e.OnEvent(eventData);
  110. }
  111. //事件-接收-增加事件码
  112. public void AddEvent(BaseEvent e)
  113. {
  114. EventDict.Add(e.EventCode, e);//字典加
  115. }
  116. //事件-接收-取消事件码
  117. public void RemoveEvent(BaseEvent e)
  118. {
  119. EventDict.Remove(e.EventCode);//字典减
  120. }
  121. }
配置 - Unity简单客户端 - Photon3Unity3D.dll