当前位置:网站首页>极简式 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. 运行截图

边栏推荐
- How to set the win10 taskbar does not merge icons
- Introduction to MATLAB drawing functions ezplot explanation
- STM32LL library use - SPI communication
- How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
- Test case exercises
- 测试用例练习
- Software Testing Basics (Back)
- How to update Win11 sound card driver?Win11 sound card driver update method
- 7. Redis
- 【离散化+前缀和】Acwing802. 区间和
猜你喜欢
随机推荐
LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
Exotic curiosity-a solution looking - bit operations
General code for pytorch model to libtorch and onnx format
Mysql之MVCC
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
模板系列-二分
Detailed introduction to drawing complex surfaces using the plot_surface command
关于c语言的调试技巧
深入理解Golang之Map
2342. 数位和相等数对的最大和 哈希优化
LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
Installation and configuration of Spark and related ecological components - quick recall
Summarize computer network super comprehensive test questions
Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
2021-06-06
开心一下,9/28名场面合集
KiCad Common Shortcuts
mysql学习总结 & 索引
7.Redis
pygame image rotate continuously









