SDK-基础

更新时间:2026-06-06 12:00:00OSDK

Login

如研发自研内购,则不能接入此登录 API,需自研登录 API

Login(string gameObjectName, string methodName)

说明:登录行为,函数内会调用微信wx.login获取code并完成session获取,完成后返回用户信息。

public class GameManager : MonoBehaviour
{
    public void CallLogin()
    {
        #if UNITY_WEBGL && !UNITY_EDITOR
        // 发起Login请求,等待回调,传入回调对象名(本对象)和方法名
        OSDKHelper.Login(gameObject.name, "OnLogin");
        #else
        Debug.Log("Login 仅在 WebGL 运行时有效");
        #endif
    }

    // JS 通过 SendMessage(goName, methodName, payload) 回调此方法,参数为 JSON 字符串
    public void OnLogin(string json)
    {
        Debug.Log($"Login 回调: {json},获取到openid并记录");
        // OSDKHelper.TrackEvent('playerlogin') // 在OnLogin方法里才算登录成功,这时SDK内存中才有openid 和 unionid,这时上报 playerlogin 或其他事件才会带上 openid 和 unionid 等信息
        
        // 成功:Login 回调:  {"id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","openid":"xxx","unionid":"","sessionToken":"xxx","timestamp":1760684102881}
        // 失败:Login 回调: {"code":500,"msg":"code2session error"}
        // TODO: 解析并处理登录结果,例如:
        // var result = JsonUtility.FromJson<YourLoginResultType>(json);
    }
}

SetConfig

SetConfig(string: config): void

说明:用于修改配置,参数和js库相同,具体见2.2.1.3. setConfig

先声明SdkConfig的配置类型

public class SdkConfig
{
    public string openid; // openid,必选
    public string unionid; // unionid,必选
    public string user_id; // 游戏业务的角色id,必选,没有则传空字符串
    public string nickname; // 游戏业务的角色名,必选,没有则传空字符串
    public string ab_test_id; // 用户参与ab实验时,所分配的ab_test_id
}

调用SetConfig方法

SdkConfig sdkConfig = new SdkConfig()
{
    user_id = "user123",
    openid = "123",
    unionid = "union55555",
    nickname = "nickname",
    ab_test_id = "group_a_homepage_redesign_20250101"
};
OSDKHelper.SetConfig(JsonUtility.ToJson(sdkConfig));

ReportError

ReportError(eventName: string, eventData: string): void

说明:上报错误日志到CLS,使用方式和TrackEvent相同,仅上报地址不同。

GetIpLocation(必接)

GetIpLocation(string gameObjectName, string methodName)

说明:用于获取玩家网络ip位置信息

使用场景:根据海外隐私合规要求,请在游戏启动时最先调用此方法,当 is_allowed 为 false 时不允许用户访问小游戏,且不允许保存该用户的任何信息至服务器(包括但不限于登录/存档等操作,违规存储海外用户数据的需承担相关责任)。请给于提示弹窗,提示语:【This mini-program is currently unavailable in your region. Thank you for your understanding!】,当用户点击【Confirm】按钮后,请调用 wx.exitMiniProgram() / tt.exitMiniProgram() 接口让用户退出小游戏。

返回:object类型,属性如下:

属性名

类型

说明

is_allowed

boolean

是否为允许访问的ip,true代表该ip的用户可以访问小游戏,false代表不可访问

使用:

public class GameManager : MonoBehaviour
{
    public void CallGetIpLocation()
    {
        #if UNITY_WEBGL && !UNITY_EDITOR
        OSDKHelper.GetIpLocation(gameObject.name, "OnGetIpLocation");
        #else
        Debug.Log("Login 仅在 WebGL 运行时有效");
        #endif
    }

    public void OnGetIpLocation(string json)
    {
        Debug.Log($"GetIpLocation 回调: {json}");
        // json 字符串转对象后:
        
        // 正常回调: 
        // {
        //   "is_allowed": true, //是否为允许访问的ip,true代表该ip的用户可以访问小游戏,false代表不可访问
        // }
        
        // 接口异常回调:
        // {
        //   "is_allowed": true, //当该接口报错时,osdk会返回该字段为true
        //   "raw": {
        //       "code": XXX, //请求错误码
        //       "msg": "", // 错误信息
        //   }
        // }
        
    }
}

相关文档