Unity 登录与聊天室

登录

使用Photon server + NHibernate + MySQL + Unity 开发的demo。
在这里插入图片描述

NHibernate 与 MySQL 数据映射
使用NHibernate 的好处是你可以直接编写C#对MySQL数据库进行各种操作,而不用SQL语言。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyGameServer"
                   namespace="MyGameServer.Model">

  <class name="User" table="users"><!--注意表名-->
    <id name="Id" column="id" type="Int32">
      <generator class="native"></generator>
    </id><!--主键配置-->
    <property name="Username" column="username" type="String"></property><!--read the NHibernate reference-->
    <property name="Password" column="password" type="String"></property>
  </class>

</hibernate-mapping>

Photon Server 中与NHibernate 映射

    public class User
    {
        public virtual int Id { set; get; }
        public virtual string Username { set; get; }
        public virtual string Password { set; get; }
    }

MySQL中建立表格
在这里插入图片描述

  Photon Server的核心是用C++开发,因此在效能优异。网络协议可以采用Photon改良的UDP,既能快速传输,又能保证数据不丢失。在 Server 端 Script 采用C#语言,Photon的Client端支持C++、.net、java、html5 、flash、Unity、mamalade、iOS、android、winphone、cocos等,市面上常见的平台全部都有支持,使用容易、效能高、支援平台多,这些优点让photon成为一个优越的套装socket server。

聊天室

使用该API即可实现数据的传输

    public class PhotonPeer
    {
    
    
		public virtual bool OpCustom(byte customOpCode,
 		Dictionary<byte, object> customOpParameters, bool sendReliable);
   }

服务器端代码

    public class RoomTalkHandler : BaseHandler
    {
    
    
        public RoomTalkHandler()
        {
    
    
            opCode = OperationCode.RoomTalk;
        }
        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
    
    
            EventData eventData = new EventData((byte)EventCode.roomTalk);
            eventData.SetParameters(operationRequest.Parameters);
            foreach(ClientPeer otherPeer in DictionaryTool.DicTool(peer.myRoomKey, peer.GetLobby(peer.type) ) )
            {
    
    
                if(otherPeer!=peer)
                {
    
    
                    otherPeer.SendEvent(eventData,sendParameters);
                }
            }
        }
    }

Unity 搭建使用UI里的Scroll View 、InputField 、Button。在这里插入图片描述

  搭出来后加脚本就可以结合Photon的Logs进行测试。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41596891/article/details/108188541
今日推荐