感觉时间过得好快,不知不觉接触Unity一年多了,当初学习Unity的目的就是为了自己做游戏,可惜现在的游戏行业一片混乱,各种卖情怀....(多的不想说了,颇有一种学医救不了中国的感觉),现在自己从事VR行业,感觉也不是自己想追求的,说实话,自己也感觉比较迷茫,不知道以后何去何从,忽然想起以前不知是谁说的一句话"不忘初心,方得始终",既然如此,就自己做一个游戏,当做这一年多以来学习Unity的作业吧!
废话不多说了,今天开始找了许多资源,想做一个MOBA类的游戏,初步决定游戏服务器使用PhotonServer,以下记录一下PhotonServer的启动设置吧
首先安装好PhotonServer,用VS新建一个类库,引用PhotonServer安装目录lib下的5个类库ExitGames.Logging.Log4Net.dll、ExitGamesLibs.dll、log4net.dll、Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll,
创建Server类:
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net;
using log4net.Config;
using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MobaServer2._0
{
public class MobaServer : ApplicationBase
{
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new MobaClient(initRequest);
}
/// <summary>
/// 服务器初始化
/// </summary>
protected override void Setup()
{
InitLogging();
LogInfo("-----------------------");
LogInfo("Server is Setup");
}
/// <summary>
/// 服务器关闭
/// </summary>
protected override void TearDown()
{
LogInfo("Server is Down");
}
#region 日志功能
private static readonly ILogger log = ExitGames.Logging.LogManager.GetCurrentClassLogger();
/// <summary>
/// 初始化日志
/// </summary>
private void InitLogging()
{
ExitGames.Logging.LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
GlobalContext.Properties["LogFileName"] = "Moba2.0"; //this.ApplicationName+"2.0";
XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")));
}
/// <summary>
/// 日志输出
/// </summary>
/// <param name="str"></param>
public static void LogInfo(string str)
{
log.Info(str);
}
#endregion
}
}
实现基类的MobaClient类如下:
using Photon.SocketServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PhotonHostRuntimeInterfaces;
namespace MobaServer2._0
{
public class MobaClient : ClientPeer
{
/// <summary>
/// 构造
/// </summary>
/// <param name="initRequest"></param>
public MobaClient(InitRequest initRequest) : base(initRequest)
{
}
/// <summary>
/// 客户端断开连接
/// </summary>
/// <param name="reasonCode"></param>
/// <param name="reasonDetail"></param>
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
/// <summary>
/// 客户端向服务器发起请求
/// </summary>
/// <param name="operationRequest"></param>
/// <param name="sendParameters"></param>
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
}
}
}
从deploy\Loadbalancing\GameServer\bin目录下复制一个log4net.config文件到VS目录下,选择始终复制,这样就可以在Log文件夹下查看输出日志了
在deploy文件夹下建一个Moba\bin文件夹,选择生成路径生成
配置PhotonServer.config文件如下:
Name:项目名字
BaseDirectory:根目录,deploy文件夹下为基础目录
Assembly :是在生成的类库中的bin目录下与我们项目名称相同的.dll文件的名字
Type:是主类的全称,在这里是:MyServer.MyApplication,一定要包括命名空间
EnableAutoRestart:是否是自动启动,表示当我们替换服务器文件时候,不用停止服务器,替换后photon会自动加载文件
WatchFiles和ExcludeFiles
这段代码放在<Default><Applications>放这里</Applications></Default>节点下面
最后.运行!