大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
Scut提供Unity3d Sdk包。便利的高速发展和Scut游戏server对接; 看Unity3d示为以下的比率:
启动Unity3d项目
打开Scutc.svn\SDK\Unity3d\Assets文件夹下的TestScene.unity项目文件,选中Main Camera。将TestGUI.cs文件拖动到Inspector窗体的Script,如图:
点击
执行。例如以下:
文件夹层次说明
1) Net层:封装Http与Socket请求操作,以及网络协议的数据解析和请求參数的打包,当中NetWriter里有SetMd5Key为设置网络协议请求參数的Key,用于跟服务校验请求參数的有效性
2) Reflect层:提供高性能的反射功能
3) Security层:加密操作
4) Serialization层:封装对象的序列化操作
5) Game层:游戏业务逻辑层代码实现功能,此文件夹下的Action和Behaviour文件夹,依据业务自己实现代码
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 classTestGUI : MonoBehaviour
{
// Use this for initialization
voidStart()
{
//todo 启用自定的结构
Net.Instance.HeadFormater = newCustomHeadFormater();
}
// Update is called once per frame
voidUpdate()
{
}
voidOnGUI()
{
// Now create any Controls you like, and they will be displayed with the custom Skin
if(GUILayout.Button("Click Http"))
{
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,
Action和Behaviour文件夹下实现自己的业务代码
自定头部解析类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 classCustomHeadFormater : IHeadFormater { publicboolTryParse(byte[] data, outPackageHead head, outbyte[] bodyBytes) { bodyBytes = null; head = null; intpos = 0; if(data == null|| data.Length == 0) { returnfalse; } intheadSize = GetInt(data, refpos); byte[] headBytes = newbyte[headSize]; Buffer.BlockCopy(data, pos, headBytes, 0, headBytes.Length); pos += headSize; ResponsePack resPack = ProtoBufUtils.Deserialize<ResponsePack>(headBytes); head = newPackageHead(); head.StatusCode = resPack.ErrorCode; head.MsgId = resPack.MsgId; head.Description = resPack.ErrorInfo; head.ActionId = resPack.ActionId; head.StrTime = resPack.St; intbodyLen = data.Length - pos; if(bodyLen > 0) { bodyBytes = newbyte[bodyLen]; Buffer.BlockCopy(data, pos, bodyBytes, 0, bodyLen); } else { bodyBytes = newbyte[0]; } //UnityEngine.Debug.Log(string.Format("ActionId:{0}, ErrorCode:{1}, len:{2}", resPack.ActionId, resPack.ErrorCode, bodyBytes.Length)); returntrue; } privateintGetInt(byte[] data, refintpos) { intval = BitConverter.ToInt32(data, pos); pos += sizeof(int); returnval; } } |
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 abstractclass BaseAction : GameAction
{
protectedBaseAction(intactionId)
: base(actionId)
{
}
protectedoverridevoidSetActionHead(NetWriter writer)
{
MessagePack headPack = newMessagePack()
{
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 classAction1001 : BaseAction { privateResponse1001Pack _responseData; publicAction1001() : base((int)ActionType.RankSelect) { } protectedoverridevoidSendParameter(NetWriter writer, objectuserData) { //自定对象參数格式 Request1001Pack requestPack = newRequest1001Pack() { PageIndex = 1, PageSize = 10 }; byte[] data = ProtoBufUtils.Serialize(requestPack); writer.SetBodyData(data); } protectedoverridevoidDecodePackage(NetReader reader) { if(reader.StatusCode == 0) { //自定对象格式解包 _responseData = ProtoBufUtils.Deserialize<Response1001Pack>(reader.Buffer); } } protectedoverridevoidProcess(objectuserData) { if(_responseData != null) { UnityEngine.Debug.Log(string.Format("ok, count:{0}", _responseData.PageCount)); } } } |
一个完整的样本Sample For Unity3d源代码下载
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117324.html原文链接:https://javaforall.net
