B小UnitySdk(新版)使用指南
B小UnitySDK(新版)是Unity适配方案(新版)的补充,使用c#定制了部分B小平台能力,包括侧边栏、添加桌面快捷方式 和 支付(充值)。
前置准备
- 将 BilibiliMiniGameSdk.unitypackage 导入工程(Assets → Import Package → Custom Package…)。
- 在启动场景任意 GameObject 上挂载
BilibiliMiniGameSdk组件,或代码中动态添加:
using BilibiliMiniGame;
using UnityEngine;
public class Demo : MonoBehaviour
{
private BilibiliMiniGameSdk miniGameSdk;
void Start()
{
miniGameSdk = GetComponent<BilibiliMiniGameSdk>();
if (miniGameSdk == null)
miniGameSdk = gameObject.AddComponent<BilibiliMiniGameSdk>();
}
}
所有 API 均为异步回调形式:
onSuccess/onError,成功或失败后还会触发onComplete。 编辑器或非 WebGL 平台调用会以错误码-1000本地失败,不会真正调用 JS。
一、侧边栏(Sidebar)
1. 跳转到侧边栏
miniGameSdk.BL_NavigateToScene(
result => Debug.Log($"NavigateToScene success: {result.ErrorMessage}"),
error => Debug.LogWarning($"NavigateToScene failed: {error}"),
() => Debug.Log("NavigateToScene complete"));
BL_NavigateToScene当前仅支持BilibiliMiniGameScene.Sidebar("sidebar"); 场景参数可省略,默认值为"sidebar"。
2.检查侧边栏入口是否可用
如果希望在不支持侧边栏的环境里禁用入口按钮,可以在页面加载时先检查:
miniGameSdk.BL_CheckScene(
result =>
{
// result.IsExist == true 表示当前环境支持侧边栏入口
sidebarButton.interactable = result.IsExist;
sidebarLabel.text = result.IsExist ? "跳转侧边栏" : "侧边栏功能无效";
},
error =>
{
sidebarButton.interactable = false;
Debug.LogWarning($"CheckScene failed: {error}");
},
() => Debug.Log("CheckScene complete"));
BL_CheckScene当前仅支持BilibiliMiniGameScene.Sidebar("sidebar"); 场景参数可省略,默认值为"sidebar"。
二、添加桌面快捷方式(Shortcut)
1. 请求添加
miniGameSdk.BL_AddShortcut(
result => Debug.Log($"AddShortcut success: {result.ErrorMessage}"),
error => Debug.LogWarning($"AddShortcut failed: {error}"),
() => Debug.Log("AddShortcut complete"));
2.检查是否已添加过
如果希望在已添加过的情况下禁用按钮或更换文案,可以调用检查接口:
miniGameSdk.BL_CheckShortcut(
result =>
{
// result.IsExist == true 表示已添加过
shortcutButton.interactable = !result.IsExist;
shortcutLabel.text = result.IsExist ? "桌面快捷方式已添加" : "添加桌面快捷方式";
},
error =>
{
shortcutButton.interactable = false;
Debug.LogWarning($"CheckShortcut failed: {error}");
},
() => Debug.Log("CheckShortcut complete"));
也可以在 BL_AddShortcut 成功后,复查添加结果并刷新 UI:
miniGameSdk.BL_AddShortcut(
async result =>
{
Debug.Log($"AddShortcut success: {result.ErrorMessage}");
await System.Threading.Tasks.Task.Delay(1000);
miniGameSdk.BL_CheckShortcut(
checkResult =>
{
shortcutButton.interactable = !checkResult.IsExist;
shortcutLabel.text = checkResult.IsExist ? "桌面快捷方式已添加" : "添加桌面快捷方式";
},
checkError => Debug.LogWarning($"Verify shortcut failed: {checkError}"),
() => Debug.Log("CheckShortcut complete"));
},
error => Debug.LogWarning($"AddShortcut failed: {error}"),
() => Debug.Log("AddShortcut complete"));
三、支付(充值)
1. 服务端接入
2. Unity 客户端拉起支付
miniGameSdk.BL_RequestRecharge(
orderId, // 开发者服务端返回的 order_id
payInfo, // 开发者服务端返回的 pay_info
onSuccess: result => Debug.Log($"RequestRecharge success: code={result.Code} msg={result.Msg}"),
onFail: error => Debug.LogWarning($"RequestRecharge failed: {error}"),
onComplete: () => Debug.Log("RequestRecharge complete"));
支付注意事项
- 具体支付参数、参与签名的字段和签名算法请参考支付接入文档 - 签名计算准则。
附:请求生命周期
- 每次调用分配唯一请求 ID,多个调用可并发。
BilibiliMiniGameRequest.BL_Cancel()取消单个请求的 C# 回调;BilibiliMiniGameSdk.BL_CancelAllRequests()取消该组件全部请求。- 组件被销毁时自动取消其未完成的请求。