当前位置:网站首页>极简式 Unity 获取 bilibili 直播弹幕、SC、上舰、礼物等 插件
极简式 Unity 获取 bilibili 直播弹幕、SC、上舰、礼物等 插件
2022-08-02 14:11:00 【莉萝爱萝莉】
极简式 Unity 获取 bilibili 直播弹幕、SC、上舰、礼物等
1. 声明
下载链接
软件均仅用于学习交流,请勿用于任何商业用途!
2. 介绍
该项目为Unity实时爬取B站直播弹幕。
- 项目介绍:通过传入B站直播间账号,实现监控B站直播弹幕、SC、上舰、礼物等。
- 运行方式:下载后将文件夹 文件夹 BiliBiliLive 拖进 Unity 的 Asset 文件夹内即可完成安装。
3. 运行需求
- Unity2019 或更高
- c# 5.0以上
- 运行需要 Json 插件解析Json,若您的项目已经导入则无视即可。若未导入可将文件夹内的Json文件导入。
4. 使用方式
- 您需要在主脚本中引入命名空间
using Liluo.BiliBiliLive; - 在任意Mono脚本中编写以下脚本,以建立一个连接到BiliBili直播间。RoomID 为房间号。
注意,本插件大量使用了异步编程,对于这样的方法,您需要使用async修饰类,并使用await等待函数完成。
IBiliBiliLiveRequest req;
async void Init(int RoomID)
{
// 创建一个直播间监听对象
req = await BiliBiliLive.Connect(RoomID);
}
- 如需释放监听,可使用 DisConnect 方法释放。
void OnDestroy()
{
// 释放监听对象
req.DisConnect();
req = null;
}
- 定时监听房间人数
该函数每隔一段时间调用,其入参为当前房间人数(热度)。
req.OnRoomViewer = number =>
{
Debug.Log($"当前房间人数为: {
number}");
};
- 监听指定内容
以下是个函数为主要监听使用函数,其入参分别为对应监听事件的相关信息结构体。VS中按下 Ctrl+左键 即可了解以下结构体提供的具体信息。
/// 监听弹幕回调函数
public Action<BiliBiliLiveDanmuData> OnDanmuCallBack;
/// 监听礼物回调函数
public Action<BiliBiliLiveGiftData> OnGiftCallBack;
/// 监听上舰回调函数
public Action<BiliBiliLiveGuardData> OnGuardCallBack;
/// 监听SC回调函数
public Action<BiliBiliLiveSuperChatData> OnSuperChatCallBack;
4. 示例启动脚本
using UnityEngine;
using UnityEngine.UI;
using Liluo.BiliBiliLive;
public class Online : MonoBehaviour
{
public Image img;
public int RoomID;
IBiliBiliLiveRequest req;
async void Start()
{
// 创建一个监听对象
req = await BiliBiliLive.Connect(RoomID);
req.OnDanmuCallBack = GetDanmu;
req.OnGiftCallBack = GetGift;
req.OnSuperChatCallBack = GetSuperChat;
bool flag = true;
req.OnRoomViewer = number =>
{
// 仅首次显示
if (flag) Debug.Log($"当前房间人数为: {
number}");
};
}
/// <summary>
/// 接收到礼物的回调
/// </summary>
public async void GetGift(BiliBiliLiveGiftData data)
{
Debug.Log($"<color=#FEA356>礼物</color> 用户名: {
data.username}, 礼物名: {
data.giftName}, 数量: {
data.num}, 总价: {
data.total_coin}");
img.sprite = await BiliBiliLive.GetHeadSprite(data.userId);
}
/// <summary>
/// 接收到弹幕的回调
/// </summary>
public async void GetDanmu(BiliBiliLiveDanmuData data)
{
Debug.Log($"<color=#60B8E0>弹幕</color> 用户名: {
data.username}, 内容: {
data.content}, 舰队等级: {
data.guardLevel}");
img.sprite = await BiliBiliLive.GetHeadSprite(data.userId);
}
/// <summary>
/// 接收到SC的回调
/// </summary>
public async void GetSuperChat(BiliBiliLiveSuperChatData data)
{
Debug.Log($"<color=#FFD766>SC</color> 用户名: {
data.username}, 内容: {
data.content}, 金额: {
data.price}");
img.sprite = await BiliBiliLive.GetHeadSprite(data.userId);
}
private void OnApplicationQuit()
{
req.DisConnect();
}
}
4. 运行截图

边栏推荐
- cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
- STM32LL库使用——SPI通信
- 质数相关问题-小记
- 动态规划理论篇
- 6. Unified logging
- 二叉树的遍历:递归法/ 迭代法/ 统一迭代法(强QAQ)
- How to simulate 1/3 probability with coins, and arbitrary probability?
- 二叉树遍历之后序遍历(非递归、递归)入门详解
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
- Codeforces Round #624 (Div. 3)
猜你喜欢

总结计算机网络超全面试题

MATLAB绘图命令fimplicit绘制隐函数图形入门详解

第三十三章:图的基本概念与性质

STM32LL库使用——SPI通信

Based on the least squares linear regression equation coefficient estimation

Redis的线程模型

第三十二章:二叉树的存储与遍历

Introduction to MATLAB drawing functions ezplot explanation

Configure clangd for vscode

How to simulate 1/3 probability with coins, and arbitrary probability?
随机推荐
pygame image rotate continuously
Mysql lock
质数相关问题-小记
推开机电的大门《电路》(一):电压,电流,参考方向
How to solve Win11 without local users and groups
计算机导论——数据库
专硕与学硕
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
倍增和稀疏表
4.发布帖子,评论帖子
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
flink+sklearn——使用jpmml实现flink上的机器学习模型部署
KiCad常用快捷键
Codeforces Round #605 (Div. 3)
2.登录退出,登录状态检查,验证码
A clean start Windows 7?How to load only the basic service start Windows 7 system
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
What are IPV4 and IPV6?
Use tencent cloud builds a personal blog