# Integration Manual for WeChat Mini Game SDK (Unity Solution)

# Document Revision History


Document Version Document Update Date Document Update Content
1.0.0.0 2024-04-25 Initial release
1.0.0.1 2024-09-13 Added support for ad tracking and impression logging

# 1. Preface


This document provides guidance for integrating the Mini Game SDK (Unity solution). It contains instructions for integrating the Mini Game SDK and introductions to its interfaces.

# 2. Preparation for Integration


  1. Configure the WeChat Mini Game development environment and the authorized WeChat account for WeChat Mini Game development Click to Enter (opens new window)
  2. Set up communication domain access for WeChat Mini Games (currently, the following domains need to be accessed) Click to Enter (opens new window)
  3. Download SDK integration resources Click to Download (opens new window)

# 3. Project Integration


# 3.1 Project Introduction

  1. Copy the GSDKBridge directory (containing the three files: GSDKBridge.cs, GSDKBridgeCallback.cs, and GSDKBridge.jslib) from the Plugins directory in the integration resources to the Assets/Plugins directory in your Unity project.

  2. Download the JSON parsing plugin Newtonsoft Json (you can directly download this plugin via the package manager, git url: com.unity.nuget.newtonsoft-json).

  3. Copy the gsdk_unity directory (containing the index.js file) from the wechat-default directory in the integration resources to the Assets/WX-WASM-SDK-V2/Runtime/wechat-default directory in your Unity project.

  4. Open the game.js file in the Assets/WX-WASM-SDK-V2/Runtime/wechat-default directory in your Unity project, and add the following code:

    import './gsdk_unity/index';
    
    1

Callback Data Format

Property Name Type Description
code int Status code, used for handling different states and troubleshooting errors
msg string Error message
data object Response data

Unless otherwise specified, the response for the following interfaces contains only the data content.

# 3.2 Interface Parameters Are All in JSON String Format

For specific integration code, refer to the Demo.cs example in the unity demo within the integration resources.

# 3.3 Interfaces

# 3.3.1 Initialization
  • Method

    public void Init(string json)
    
    1
  • Parameters

    Property Name Type Required Description
    appId string Yes The appId assigned to your application
  • Response

    No callback response is required. Developers do not need to concern themselves with the initialization result.

  • Callback Method

    None

  • Example

    Dictionary<string, object> inParams = new Dictionary<string, object>
            {
                { "appId", "791000741"}
            };
    GSDKBridge.Instance.Init(JsonConvert.SerializeObject(inParams));
    
    1
    2
    3
    4
    5
# 3.3.2 Login
  • Method

    public void Login(string json)
    
    1
  • Input Parameters

    None

  • Response

    Property Name Type Description
    ticket string Game login ticket, valid for 5 minutes
    token string Shengqu Mini Game SDK login state
    userid string Shengqu appmid
    openid string WeChat openid
    unionid string WeChat unionid
  • Callback Method

    GSDKBridgeCallback.LoginCallback
    
    1
  • Example

    // Login
    GSDKBridge.Instance.Login("");
    // Callback listener
    GSDKBridgeCallback.LoginCallback = (loginInfo) =>
    {
        // Handle login info
    };
    
    1
    2
    3
    4
    5
    6
    7
# 3.3.3 Get Ticket
  • Method

    public void GetTicket(string json)
    
    1
  • Input Parameters

    None

  • Response

    Property Name Type Description
    ticket string Game login ticket, valid for 5 minutes
  • Callback Method

    GSDKBridgeCallback.GetTicketCallback
    
    1
  • Example

    // Get ticket
    GSDKBridge.Instance.GetTicket("");
    // Callback listener
    GSDKBridgeCallback.GetTicketCallback = (ticketInfo) =>
    {
        // Handle ticket info
    };
    
    1
    2
    3
    4
    5
    6
    7
# 3.3.4 Payment
  • Method

    public void Pay(string json)
    
    1
  • Input Parameters

    Property Name Type Required Description
    areaId string Yes Area ID
    productId string Yes Product ID
    orderId string Yes Order ID
    extend string No Order extension info, will be returned unchanged by the server during delivery
    groupId string No Group ID, provide as needed
  • Response

    Property Name Type Description
    payType number Payment method, 1: Native payment
    orderId string Shengqu order number
  • Callback Method

    GSDKBridgeCallback.PayCallback
    
    1
  • Example

    // Payment
    Dictionary<string, object> inParams = new Dictionary<string, object>
    {
        { "areaId", "1"},
        { "productId", "test.100"},
        { "orderId", "1"},
        { "extend", "1"},
        { "groupId", "1"},
    };
    GSDKBridge.Instance.Pay(JsonConvert.SerializeObject(inParams));
    // Callback listener
    GSDKBridgeCallback.PayCallback = (payInfo) =>
    {
        // Handle payment info
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
# 3.3.5 Action Reporting
  • Method

    public void ActionReport(string json)
    
    1
  • Input Parameters

    Property Name Type Required Description
    action string Yes The reported action (currently supports role_create (role creation), role_login (role login), role_level (role upgrade))
    actionData JSON Yes The reported data

    Explanation of actionData

    Parameter Name Parameter Description Required Data Type
    roleId Role ID true String
    roleName Role Name true String
    roleLevel Role Level true Number
    zoneId Zone ID true String
    zoneName Zone Name true String
    roleCtime Role Creation Time (Timestamp) true Number
    shiftLevel Turn Level true Number
    fight Combat Power true Number
  • Response

    Property Name Type Description
  • Callback Method

    GSDKBridgeCallback.ActionReportCallback
    
    1
  • Example

    // Action Reporting
    Dictionary<string, object> actionData = new Dictionary<string, object>
            {
                { "ucid", 100},
                { "roleId", "1000"},
                { "roleName", "Test Role Name"},
                { "roleLevel", 1},
                { "zoneId", "1"},
                { "zoneName", "Test Zone 1"},
                { "roleCtime", 1725431204651},
                { "shiftLevel", 1},
                { "fight", 100},
            };
    Dictionary<string, object> inParams = new Dictionary<string, object>
            {
                { "action", "role_login"},
                { "actionData", actionData}
            };
    GSDKBridge.Instance.ActionReport(JsonConvert.SerializeObject(inParams));
    // Callback Listener
    GSDKBridgeCallback.ActionReportCallback = (actionInfo) =>
            {
            };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

# 3.4 Error Codes

3.4.1 Login Error Codes:

Error Code Description
-10403020 db is error
-10403021 get weixin service error
-10403019 json parse error
-10403013 The system is busy
-10403014 The code is invalid
-10403015 Frequency limitation
-10403016 High-risk level user
-10403022 get lsc4gauth service error
-10403005 Request param error

3.4.2 Ticket Acquisition Error Codes:

Error Code Description
-10403005 Request param error
-10403023 token has expired

# 4. FAQ

  • When switching WeChat accounts, an error related to permissions is prompted.

    Confirm if the relevant permissions are enabled for the WeChat account and restart the WeChat Developer Tool.

  • How to parse callback data?

    For the convenience of game extension parameters, the SDK does not have a fixed data model. Developers can convert the dictionary in the data to the desired data model.

  • This SDK is a wrapper and forwarder for the GHOME JS SDK. For more features, please refer to the GHOME JS SDK (opens new window).

Last Updated: 12/19/2024, 10:49:31 AM