Unity3D下配置Photon服務器應用並測試:
工具:Unity 5.6.4f1、Photon_v4-0-29-11263、VS2015
一、准備工作
1、安裝PhotonServer服務器:https://www.photonengine.com/en-US/sdks#onpremiseserver (Photon-OnPremise-Server-SDK_v4-0-29-11263版本,下載需要帳號,自己注冊)。
2、安裝完成后開啟測試,開啟MmoDemo節點進行測試,v4版本已經默認取消default節點。
注意!注意!注意!:開啟節點測試前注意關閉酷狗音樂播放器!!!(酷狗會占用Photon的端口....被坑了好久.....日志信息也不好使...愛音樂有錯??(笑哭))
3、測試沒問題即可。
二、創建自己的Photon應用並部署到服務器
1、在VS2015中新建VS類庫項目,項目名為服務器名字(我的為:PongGameServer)
2、引入Photon的三個類庫文件:在解壓目錄下的lib文件夾下
注意引入后文件的復制本地屬性要設置為True,如果為False,則設置嵌入互操作類型為False即可更改為True
3、更改項目應用程序目標框架為.Net Framework4.5,框架版本低會報錯
4、在Photon目錄deploy文件夾下創建對應的服務器文件夾,文件夾內再創建bin文件夾,存放生成的類庫文件
5、更改項目的輸出路徑為上一步創建的bin文件夾目錄
6、生成,測試以上操作是否正確(即bin目錄下是否正確生成編譯后的類庫文件)
生成以下文件即可
7、PongGameServer.cs文件
1 using Photon.SocketServer; 2 3 namespace PongGameServer 4 { 5 public class PongGameApplication : ApplicationBase //繼承接口,重寫繼承方法,注釋不必要的異常拋出代碼 6 { 7 protected override PeerBase CreatePeer(InitRequest initRequest) 8 { 9 PongGamePeer pongGamePeer = new PongGamePeer(initRequest); //創建自己的Peer類對象並返回 10 return pongGamePeer; 11 } 12 13 protected override void Setup() 14 { 15 //throw new NotImplementedException(); 16 } 17 18 protected override void TearDown() 19 { 20 //throw new NotImplementedException(); 21 } 22 } 23 }
8、新建PongGamePeer類,PongGamePeer.cs文件
1 using Photon.SocketServer; 2 using PhotonHostRuntimeInterfaces; 3 4 namespace PongGameServer 5 { 6 //構建自己的Peer類,繼承PeerBase類即可,注釋不必要的異常拋出代碼 7 class PongGamePeer:PeerBase 8 { 9 public PongGamePeer(InitRequest initRequest) : base(initRequest) 10 { 11 } 12 13 public PongGamePeer(IRpcProtocol protocol, IPhotonPeer unmanagedPeer) : base(protocol, unmanagedPeer) 14 { 15 } 16 17 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) 18 { 19 //throw new NotImplementedException(); 20 } 21 22 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) 23 { 24 //throw new NotImplementedException(); 25 } 26 } 27 }
9、打開PhotonServer.config文件,添加服務器的相關配置信息(配置文件在解壓文件的對應操作平台的文件夾下,我的在bin_Win64下)
在配置文件末尾添加如下應用節點配置(每個節點的作用就不贅述了,可以查閱相關資料了解):
1 <!-- ************自建服務器節點************ --> 2 <MyGameServer 3 MaxMessageSize="512000" 4 MaxQueuedDataPerPeer="512000" 5 PerPeerMaxReliableDataInTransit="51200" 6 PerPeerTransmitRateLimitKBSec="256" 7 PerPeerTransmitRatePeriodMilliseconds="200" 8 MinimumTimeout="5000" 9 MaximumTimeout="30000" 10 DisplayName="MyGame Server" 11 > 12 13 <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. --> 14 <!-- Port 5055 is Photon's default for UDP connections. --> 15 <UDPListeners> 16 <UDPListener 17 IPAddress="0.0.0.0" 18 Port="5055" 19 OverrideApplication="PongGameServer"> 20 </UDPListener> 21 </UDPListeners> 22 23 <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. --> 24 <!-- Port 4530 is Photon's default for TCP connecttions. --> 25 <!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) --> 26 <TCPListeners> 27 <TCPListener 28 IPAddress="0.0.0.0" 29 Port="4530" 30 PolicyFile="Policy\assets\socket-policy.xml" 31 InactivityTimeout="10000" 32 OverrideApplication="PongGameServer" 33 > 34 </TCPListener> 35 </TCPListeners> 36 37 <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943) --> 38 <PolicyFileListeners> 39 <!-- multiple Listeners allowed for different ports --> 40 <PolicyFileListener 41 IPAddress="0.0.0.0" 42 Port="843" 43 PolicyFile="Policy\assets\socket-policy.xml" 44 InactivityTimeout="10000"> 45 </PolicyFileListener> 46 <PolicyFileListener 47 IPAddress="0.0.0.0" 48 Port="943" 49 PolicyFile="Policy\assets\socket-policy-silverlight.xml" 50 InactivityTimeout="10000"> 51 </PolicyFileListener> 52 </PolicyFileListeners> 53 54 <!-- WebSocket (and Flash-Fallback) compatible listener --> 55 <WebSocketListeners> 56 <WebSocketListener 57 IPAddress="0.0.0.0" 58 Port="9090" 59 DisableNagle="true" 60 InactivityTimeout="10000" 61 OverrideApplication="PongGameServer"> 62 </WebSocketListener> 63 </WebSocketListeners> 64 65 <!-- Defines the Photon Runtime Assembly to use. --> 66 <Runtime 67 Assembly="PhotonHostRuntime, Culture=neutral" 68 Type="PhotonHostRuntime.PhotonDomainManager" 69 UnhandledExceptionPolicy="Ignore"> 70 </Runtime> 71 72 73 <!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. --> 74 <!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. --> 75 <Applications Default="PongGameServer"> 76 77 <!-- PongGame Application --> 78 <Application 79 Name="PongGameServer" 80 BaseDirectory="PongGame" 81 Assembly="PongGameServer" 82 Type="PongGameServer.PongGameApplication" 83 ForceAutoRestart="true" 84 WatchFiles="dll;config" 85 ExcludeFiles="log4net.config"> 86 </Application> 87 <!-- CounterPublisher Application --> 88 <Application 89 Name="CounterPublisher" 90 BaseDirectory="CounterPublisher" 91 Assembly="CounterPublisher" 92 Type="Photon.CounterPublisher.Application" 93 ForceAutoRestart="true" 94 WatchFiles="dll;config" 95 ExcludeFiles="log4net.config"> 96 </Application> 97 </Applications> 98 </MyGameServer>
10、重新開啟Photon服務器軟件,然后選擇檢測日志信息檢查配置信息是否正確(我的已經開啟,所以按鈕是灰色的)
11、日志文件出現以下信息則說明服務器啟動成功
12、新建Unity項目,創建在Assets文件夾下創建Plugins文件夾,拷貝Photon-->lib-->Photon3Unity3D.dll庫文件到Plugins文件夾下,unity會自動引用該文件夾下的庫文件
13、創建任意游戲物體,在游戲物體上掛載GameServer.cs腳本,我們將在這個文件內處理連接服務器邏輯
14、GameServer.cs文件內容
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using ExitGames.Client.Photon; //引入Photon庫 5 6 7 public class GameServer : MonoBehaviour, IPhotonPeerListener //繼承IPhotonPeerListener接口並實現接口 8 { 9 10 public PhotonPeer peer; 11 private string Message=""; 12 13 // Use this for initialization 14 void Start() 15 { 16 peer = new PhotonPeer(this,ConnectionProtocol.Udp); //創建Peer對象 17 peer.Connect("localhost:5055","PongGameServer"); //連接服務器 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 peer.Service(); //開啟監聽服務 24 } 25 26 public void DebugReturn(DebugLevel level, string message) 27 { 28 // throw new System.NotImplementedException(); 29 Debug.Log("DebugReturn Message."); 30 Debug.Log(message); 31 } 32 33 public void OnEvent(EventData eventData) 34 { 35 // throw new System.NotImplementedException(); 36 Debug.Log("OnEvent Message.");37 38 } 39 40 public void OnOperationResponse(OperationResponse operationResponse) 41 { 42 // throw new System.NotImplementedException(); 43 Debug.Log("OnOperationResponse Message.");44 45 } 46 47 public void OnStatusChanged(StatusCode statusCode) 48 { 49 //根據服務器返回的連接狀態碼判斷連接狀態 50 switch (statusCode) 51 { 52 case StatusCode.Connect: { Debug.Log("連接服務器成功."); } break; 53 case StatusCode.Disconnect: { Debug.Log("連接服務器失敗."); } break; 54 default: break; 55 } 56 } 57 58 }
15、運行unity並查看后台log信息,出現如下信息即可
搭建測試完成!!
時間匆忙,寫的不是很詳細。有時間再補補......