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代碼

 

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代碼

 

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代碼

 

/// <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代碼

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源碼下載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM