Scut游戏服务器引擎之Unity3d接入

Scut提供Unity3d Sdk包,方便开发人员快速与Scut游戏服务器对接; 先看Unity3d示例如下:

启动Unity3d项目

打开Scutc.svn\SDK\Unity3d\Assets目录下的TestScene.unity项目文件,选中Main Camera,将TestGUI.cs文件拖动到Inspector窗口的Script,如图:








点击运行,如下:

 

目录层次说明
1)       Net:封装HttpSocket请求操作,以及网络协议的数据解析和请求参数的打包,其中NetWriter里有SetMd5Key为设置网络协议请求参数的Key,用于跟服务校验请求参数的有效性
2)       Reflect层:提供高性能的反射功能
3)       Security层:加密操作
4)       Serialization层:封装对象的序列化操作
5)       Game:游戏业务逻辑层代码实现功能,此目录下的ActionBehaviour目录,根据业务自己实现代码
6)       CustomHeadFormater类:自定的结构消息头解析器
7)       TestGUI.cs为测试脚本
 

TestGUI代码

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using UnityEngine;
  
public class TestGUI : MonoBehaviour
{
  
     // Use this for initialization
     void Start()
     {
         //todo 启用自定的结构
         Net.Instance.HeadFormater = new CustomHeadFormater();
     }
  
     // Update is called once per frame
     void Update()
     {
  
     }
  
     void OnGUI()
     {
  
         // Now create any Controls you like, and they will be displayed with the custom Skin
         if (GUILayout.Button( "Click Http" ))
         {
             NetWriter.SetUrl( "http://ph.scutgame.com/service.aspx" );
             Net.Instance.Send(( int )ActionType.RankSelect, null );
         }
  
         // Any Controls created here will use the default Skin and not the custom Skin
         if (GUILayout.Button( "Click Socket" ))
         {
             NetWriter.SetUrl( "ph.scutgame.com:9001" );
             Net.Instance.Send(( int )ActionType.RankSelect, null );
         }
     }
}

 

  

 

Send方法接口会根据url是否带http字段来判断是否是用http还是socket,
ActionBehaviour目录下实现自己的业务代码

自定头部解析类CustomHeadFormater代码

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using GameRanking.Pack;
using ZyGames.Framework.Common.Serialization;
  
/// <summary>
/// 定制的头部结构解析
/// </summary>
public class CustomHeadFormater : IHeadFormater
{
     public bool TryParse( byte [] data, out PackageHead head, out byte [] bodyBytes)
     {
         bodyBytes = null ;
         head = null ;
         int pos = 0;
         if (data == null || data.Length == 0)
         {
             return false ;
         }
         int headSize = GetInt(data, ref pos);
         byte [] headBytes = new byte [headSize];
         Buffer.BlockCopy(data, pos, headBytes, 0, headBytes.Length);
         pos += headSize;
         ResponsePack resPack = ProtoBufUtils.Deserialize<ResponsePack>(headBytes);
  
         head = new PackageHead();
         head.StatusCode = resPack.ErrorCode;
         head.MsgId = resPack.MsgId;
         head.Description = resPack.ErrorInfo;
         head.ActionId = resPack.ActionId;
         head.StrTime = resPack.St;
  
         int bodyLen = data.Length - pos;
         if (bodyLen > 0)
         {
             bodyBytes = new byte [bodyLen];
             Buffer.BlockCopy(data, pos, bodyBytes, 0, bodyLen);
         }
         else
         {
             bodyBytes = new byte [0];
         }
  
         //UnityEngine.Debug.Log(string.Format("ActionId:{0}, ErrorCode:{1}, len:{2}", resPack.ActionId, resPack.ErrorCode, bodyBytes.Length));
  
         return true ;
     }
  
     private int GetInt( byte [] data, ref int pos)
     {
         int val = BitConverter.ToInt32(data, pos);
         pos += sizeof ( int );
         return val;
     }
}

 

  

 

BaseAction代码

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/// <summary>
/// 自定结构Action代理基类
/// </summary>
public abstract class BaseAction : GameAction
{
     protected BaseAction( int actionId)
         : base (actionId)
     {
     }
  
     protected override void SetActionHead(NetWriter writer)
     {
         MessagePack headPack = new MessagePack()
         {
             MsgId = Head.MsgId,
             ActionId = ActionId,
             SessionId = Head.SessionId,
             UserId = Head.UserId
         };
         byte [] data = ProtoBufUtils.Serialize(headPack);
         writer.SetHeadBuffer(data);
         writer.SetBodyData( null );
     }
}

 

  

 

Action1001代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using GameRanking.Pack;
using ZyGames.Framework.Common.Serialization;
  
public class Action1001 : BaseAction
{
     private Response1001Pack _responseData;
  
     public Action1001()
         : base (( int )ActionType.RankSelect)
     {
     }
  
     protected override void SendParameter(NetWriter writer, object userData)
     {
     //自定对象参数格式
     Request1001Pack requestPack = new Request1001Pack()
     {
         PageIndex = 1,
         PageSize = 10
     };
     byte [] data = ProtoBufUtils.Serialize(requestPack);
     writer.SetBodyData(data);
     }
  
     protected override void DecodePackage(NetReader reader)
     {
         if (reader.StatusCode == 0)
         {
         //自定对象格式解包
         _responseData = ProtoBufUtils.Deserialize<Response1001Pack>(reader.Buffer);
              
         }
     }
  
     protected override void Process( object userData)
     {
         if (_responseData != null )
         {
             UnityEngine.Debug.Log( string .Format( "ok, count:{0}" , _responseData.PageCount));
         }
     }
}

  



完整例子Sample For Unity3d源码下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值