拓展资料:
一、Photon Server是什么?
Photon Server是一套套装的游戏服务器,以往开发线上游戏都必需自行花费大笔的研发资金和人力先从研发游戏引擎和伺服器开始,后来慢慢的游戏引擎开始走向套装化,研发人员有许多现成的游戏引擎可以选择,像是unreal或是unity等等。
市面上常见的套装Game Server有smart fox server、electro server5、Photon等等, 这几个都是非常优秀的套装伺服器,市面上非常多的FB游戏都是利用这些套装伺服器作为通讯用平台,都是经过市场验证过的产品。
客户端----发送消息:
在unity脚本里写个函数调用
接收响应:
在PhotonEngine脚本中
服务器----
ClientPeer.cs里面
如何请求带数据
客户端----
发送数据:
响应接收服务器数据:
服务器----
ClientPeer.cs里面
本文将对NHibernate数据进行简单封装,方便在PhotonServer服务器中进行调用
[csharp] view plain copy
using NHibernate
using NHibernate.Cfg
using MyGameServer.Domain
using NHibernate.Criterion
using System.Collections.Generic
namespace MyGameServer.Helper
{
public static class NHibernateHelper
{
private static ISessionFactory sessionFactory = null
private static ISession session = null
public static ISession GetSession
{
get
{
if (sessionFactory == null)
{
Configuration cfg = new Configuration()
//解析固定路径配置文件nhibernate.cfg.xml
cfg.Configure()
//映射目标程序集 解析映射文件 Student.xml ......
cfg.AddAssembly(typeof(Student).Assembly)
//获取会话对象
sessionFactory = cfg.BuildSessionFactory()
}
session = sessionFactory.OpenSession()
return session
}
private set { }
}
//添加行
public static void AddData<T>(T t)
{
using (ISession session = GetSession)
{
using (ITransaction transaction=session.BeginTransaction())
{
GetSession.Save(t)
transaction.Commit()
}
}
}
//添加列
public static void RemoveData<T>(T t)
{
using (ISession session = GetSession)
{
using (ITransaction transaction = session.BeginTransaction())
{
GetSession.Delete(t)
transaction.Commit()
}
}
}
//通过ID获取对象
public static T GetDataById<T>(int id)
{
using (ISession session = GetSession)
{
using (ITransaction transaction = session.BeginTransaction())
{
T t = session.Get<T>(id)
transaction.Commit()
return t
}
}
}
/// <summary>
/// 通过名称获取对象
/// </summary>
/// <typeparam name="T">需要获取的对象</typeparam>
/// <param name="dataBaseName">在数据库中的列名称</param>
/// <param name="targetName">获取对象的目标名</param>
/// <returns></returns>
public static T GetDataByName<T>(string dataBaseName, string targetName)
{
using (ISession session = GetSession)
{
T t = session.CreateCriteria(typeof(T)).Add(Restrictions.Eq(dataBaseName, targetName)).UniqueResult<T>()
return t
}
}
/// <summary>
/// 得到表内的全部对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static ICollection<T> GetAllUsers<T>()
{
using (ISession session = GetSession)
{
IList<T> ts = session.CreateCriteria(typeof(T)).List<T>()
return ts
}
}
//查询是否有符合id和姓名相同的对象
public static bool VerifyUser<T>(params object[] arg)
{
using (ISession session = GetSession)
{
T t = session
.CreateCriteria(typeof(T))
.Add(Restrictions.Eq(arg[0].ToString(), arg[1]))//类属性名 属性值
.Add(Restrictions.Eq(arg[2].ToString(), arg[3]))
.UniqueResult<T>()
if (t == null)
{
return false
}
return true
}
}
/// <summary>
/// 更新数据表
/// </summary>
/// <typeparam name="T">数据表映射的对象</typeparam>
/// <param name="t"></param>
public static void UpdateData<T>(T t)
{
using (ISession session = GetSession)
{
using (ITransaction transaction=session.BeginTransaction())
{
session.Update(t)
transaction.Commit()
}
}
}
}
}
在主函数调用
[csharp] view plain copy
using NHibernate
using NHibernate.Cfg
using LJL.Domain
using LJL.Helper
namespace LJL
{
class Program
{
static void Main(string[] args)
{
Student sd = new Student { mID = 6, mName = "小张", mScore = 10 }
NHibernateHelper.AddData(sd)
}
}
}
运行程序,一切正常,打开SQLyog,在student数据表中添加一行数据
接下来就是在PhotonServer中调用,来实现与MySQL数据库交互
按图下将类、配置文件集成到类库中(注意需要修改下类和配置文件中的程序集及命名空间)
接下来就是在Unity3d游戏客户端中与PhotonServer通信进而访问本地数据库
在Unity3d客户端上创建UI(这里简单拖入输入框和按钮)
如下图创建脚本
我们的游戏有很多中请求(比如登入、注册请求等等)
所以都继承自BaseRequest
[csharp] view plain copy
using ExitGames.Client.Photon
using System.Collections
using System.Collections.Generic
using UnityEngine
public abstract class BaseRequest : MonoBehaviour
{
[HideInInspector]
//该请求的操作类型
public Collective.OperationMode operationMode = Collective.OperationMode.Default
public virtual void Start() { }
public abstract void OnOperationRequest()
public abstract void OnOperationResponse(OperationResponse operationResponse)
}
这里博主简单的做了一下登录请求
[csharp] view plain copy
using ExitGames.Client.Photon
using System.Collections
using System.Collections.Generic
using UnityEngine
using UnityEngine.UI
public class LoginRequest : BaseRequest
{
//用户名
private InputField mInputName
//密码
private InputField mInputPassword
public override void Start()
{
operationMode = Collective.OperationMode.LOGIN
GameContext.GetInstance.AddRequest(this)
mInputName = GameObject.Find("IDInputField").GetComponent<InputField>()
mInputPassword = GameObject.Find("NameInputField").GetComponent<InputField>()
//登录按钮点击事件
GameObject.Find("LoginButton").GetComponent<Button>().onClick.AddListener(() => { OnOperationRequest() })
}
public override void OnOperationRequest()
{
GameContext.GetInstance.peer.OpCustom(
(byte)this.operationMode,
new Dictionary<byte, object> { { (byte)Collective.ParameterMode.NAME, mInputName.text }, { (byte)Collective.ParameterMode.PASSWORD, mInputPassword.text } },
true
)
}
public override void OnOperationResponse(OperationResponse operationResponse)
{
Collective.OperationResult resultCode = (Collective.OperationResult)operationResponse.ReturnCode
if (resultCode == Collective.OperationResult.SUCCESS)
{
//登录成功
Debug.Log("用户登录成功")
}
else if(resultCode == Collective.OperationResult.FAIL)
{
//登录失败
Debug.Log("登录失败")
}
}
}
最后附上上篇博文GameContext脚本,在这里有些地方发生了更新
[csharp] view plain copy
using System.Linq
using System.Collections.Generic
using UnityEngine
using UnityEngine.UI
using ExitGames.Client.Photon
public class GameContext : MonoBehaviour,IPhotonPeerListener
{
/// <summary>
/// 存储操作类型与请求
/// </summary>
public Dictionary<Collective.OperationMode, BaseRequest> requestDic = new Dictionary<Collective.OperationMode, BaseRequest>()
public PhotonPeer peer
private static GameContext _instance
public static GameContext GetInstance
{
get
{
if (_instance == null)
{
_instance = GameObject.Find("GameContext").GetComponent<GameContext>()
}
return _instance
}
}
public void DebugReturn(DebugLevel level, string message)
{
}
//接收服务器发来的事件
public void OnEvent(EventData eventData)
{
switch (eventData.Code)
{
case 0:
//获取事件数据
object value = eventData.Parameters.FirstOrDefault(q => q.Key == 1).Value
Debug.Log(value.ToString())
break
default:
break
}
}
//接收响应数据(客户端发送了请求)
public void OnOperationResponse(OperationResponse operationResponse)
{
BaseRequest request = requestDic.FirstOrDefault(q => q.Key == (Collective.OperationMode)operationResponse.OperationCode).Value
if (request != null)
{
request.OnOperationResponse(operationResponse)
}
else
{
//获取响应数据失败
Debug.LogError("获取响应数据失败")
}
}
//连接状态发送改变
public void OnStatusChanged(StatusCode statusCode)
{
Debug.Log("数据连接状态发生的改变:" + statusCode)
}
private void Start()
{
//传输协议UDP 、 通过Listener接收服务器端的响应
peer = new PhotonPeer(this, ConnectionProtocol.Udp)
//连接本地服务器
peer.Connect("127.0.0.1:5055", "MYGameServer")
}
private void Update()
{
//和服务器实时保持数据连接
peer.Service()
}
private void OnDestroy()
{
if (peer != null && peer.PeerState == PeerStateValue.Connected)
{
//断开连接
peer.Disconnect()
}
}
public void AddRequest(BaseRequest request)
{
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)