Photon是目前比較好用的游戲服務器。目前網上對於Photon的服務器講解比較少,最近也對Photon做了初步的了解,做一個極其詳細的入門。
首先就是得下載Photon咯。
這個是服務器下載 :https://www.photonengine.com/en/OnPremise/Download (得注冊賬號)
這個服務器是有權限的,免費的有100人上限和30天免費,調試用的話,100人的還是很合算的。
這里有個比較好的教程 http://download.csdn.net/download/a762923309/4936547 是免費的,下載就好(CSDN的)
如果你能看懂教程就可以了。
現在開始正題。
在服務器的deploy中是配置所有服務器的,會看到有幾個啟動版本bin_Win32,bin_Win32_xp,根據自己的系統環境來選擇。
我系統是win10選的就是bin_Win64 里面有個PhotonControl.exe就是運行服務器。雙擊啟動它。
在你的系統右下角就會發現一個小圓圈,這個就是服務器啦!
右鍵它你會發現有個Photon instance:下面有個Default就是我們要用的服務器啦,上面的教程中這里是不同的,不過沒差多少。
對了,下載下來的權限就放在這個bin文件夾,我的就是bin_Win64,弄完權限記得重啟服務器啊。
下面我們就來寫一下服務器代碼。一個簡單的用戶登錄
Photon用的C#我們就用VS寫,我用的是VS2015
首先我們新建一個C#類庫我們叫MyServer,讓我們引入3個dll,在Photon的lib中
ExitGamesLibs.dll
Photon.SocketServer.dll
PhotonHostRuntimeInterfaces.dll
新建一個C#類我們叫MyPeer,繼承PeerBase,然后重寫函數,別忘了using

using Photon.SocketServer; using PhotonHostRuntimeInterfaces; namespace MyServer { using Message; using System.Collections; public class MyPeer : PeerBase { Hashtable userTabel; public MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer) : base(protocol, photonPeer) { userTabel = new Hashtable(); userTabel.Add("123", "1234"); } protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { //失去連線時候要處理的事項,例如釋放資源 } protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { //取得Client端傳過來的要求加以處理 switch (operationRequest.OperationCode) { case (byte)OpCodeEnum.Login: string uname = (string)operationRequest.Parameters[(byte)OpKeyEnum.UserName]; string pwd = (string)operationRequest.Parameters[(byte)OpKeyEnum.PassWord]; if (userTabel.ContainsKey(uname) && userTabel[uname].Equals(pwd)) { SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginSuccess, null), new SendParameters()); } else { SendOperationResponse(new OperationResponse((byte)OpCodeEnum.LoginFailed, null), new SendParameters()); } break; } } } }
然后我們再建一個C#類叫MyApplication,我們繼承AppLicationBase,然后全部重寫就好,每個函數的意思我都給出來了

using Photon.SocketServer; namespace MyServer { public class MyApplication : ApplicationBase { protected override PeerBase CreatePeer(InitRequest initRequest) { //建立連線並回傳給Photon Server return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer); } protected override void Setup() { //初始化GameServer } protected override void TearDown() { //關閉GameServer並釋放資源 } } }
還有個Message是用來標識狀態的,同樣新建一個C#類叫Message

namespace MyServer.Message { enum OpCodeEnum : byte { Login = 1, LoginSuccess = 2, LoginFailed = 3, Create = 250, Join = 255, Leave = 254, RaiseEvent = 253, SetProperties = 252, GetProperties = 251 } enum OpKeyEnum : byte { RoomId = 251, UserName = 252, PassWord = 253 } }
然后比較重要的一步,在VS中的解決方案中,我們右鍵我們的MyServer(C#類庫名)打開屬性,選擇生成,把輸出中的輸出路徑改為bin\
因為Photon就讀取bin目錄中的dll。
然后我們就生成服務器就好啦~~~
然后把我們的服務器MyServer中除了bin文件夾其他都可以刪除,然后放到Photon中的deploy文件夾中,然后我們來配置一下Photon
打開deploy目錄中的bin目錄,我就打開bin_Win64中的PhotonServer.config,用VS打開即可
建議閱讀PhotonServer.config文件中的注釋,不會英語的可以用有道。很有幫助
我們用的是Udp的傳輸方式,Photon只有一個接聽端口就是5055,所以防火牆不要封這個端口還有843,是Unity和Flash的一個接通端口所以也不要封,防火牆不會開固定端口的見http://windows.microsoft.com/zh-cn/windows/open-port-windows-firewall#1TC=windows-7
然后我們要加一段代碼在<Applications Default="Lite">下面
<!-- MyServer Application --> <Application Name="MyServer" BaseDirectory="MyServer" Assembly="MyServer" Type="MyServer.MyApplication" ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application>
然后保存即可。
這樣我們服務器端就配置完成了,現在讓我們打開Default中的Start as application,然后打開Open Logs 見到Server is running。。。表面服務器建立成功了。
然后就是Unity端了
我們新建一個工程,然后引入一個dll直接拖到Unity中就行Photon3Unity3D.dll 同樣也在lib中。
讓我們建一個C# Script 叫hotonSocket,同樣在引用中導入Photon3Unity3D.dll

using UnityEngine; using ExitGames.Client.Photon; using System.Collections.Generic; public class PhotonSocket : MonoBehaviour,IPhotonPeerListener { #region 單例 private static PhotonSocket _Instance; public static PhotonSocket Instance { get { return _Instance; } } #endregion private string address; //最好在Awake或Start中賦值,Unity 小問題,容易造成值不更改,還有最好寫成私有 private string Server; //同上 private PhotonPeer peer; public ClientState state; void Awake () { _Instance = this; address = "localhost:5055"; Server = "MyServer"; state = ClientState.DisConnect; peer = new PhotonPeer(this, ConnectionProtocol.Udp); peer.Connect(address, Server); } public void SendMessage(byte Code,Dictionary<byte,object> param) { peer.OpCustom(Code, param,true); } void Update () { peer.Service(); } public void DebugReturn(DebugLevel level, string message) { } public void OnEvent(EventData eventData) { } public void OnOperationResponse(OperationResponse operationResponse) { switch(operationResponse.OperationCode) { case (byte)OpCodeEnum.LoginSuccess: Debug.Log("login Success"); state = ClientState.LoginSuccess; break; case (byte)OpCodeEnum.LoginFailed: Debug.Log("login Failed"); state = ClientState.LoginFailed; break; } } public void OnStatusChanged(StatusCode statusCode) { switch(statusCode) { case StatusCode.Connect: Debug.Log("Connect"); break; case StatusCode.Disconnect: Debug.Log("DisConnect"); break; } } public enum ClientState : byte { DisConnect, Connect, LoginSuccess, LoginFailed } enum OpCodeEnum : byte { //Login Login = 1, LoginSuccess = 2, LoginFailed = 3, } }
這樣Unity的部分也完事了,就可以來測試啦,出現Connect的Debug就表面鏈接服務器成功,出現LoginSuccess就OK了。
以上的都是我們在教程中都能找到的部分,然后我們來說說教程中沒有的Unity部分的address。
如果想局域網聯機的就要找到你的本機內網IP,然后讓address = 本機IP:5055 這樣就OK了,
如果要想要外網鏈接呢有兩種情況,一種你是用路由器的,也有兩種方式,一種開DMZ主機,一種開虛擬服務器。
第一種DMZ主機,不建議用這種方式,他會把你的IP完全暴露在外網,不安全, address = 外網IP:5055
第二種虛擬服務器,這種方式就是開放部分端口,比較高端的路由器可以設置端口對端口,不高端的路由器只能指定端口,address = WAN口IP:5055
沒有服務器的呢,下一個花生殼軟件,他會給你一個免費的域名,然后掛在你的外網IP上然后 address = 花生殼域名:5055