Minimalist Unity Get bilibili live barrage, SC, ship, gifts and other plug-ins

Minimalist Unity Get bilibili live barrage, SC, ship, gifts, etc.

1. Statement

Download link The
software is only used for learning and communication, please do not use it for any commercial purpose!

2. Introduction

This project is for Unity to crawl the live broadcast barrage of station B in real time.

  • Project introduction: By passing in the account of the live broadcast room of station B, it is possible to monitor the live broadcast barrage, SC, ship, gifts, etc. of station B.
  • How to run: After downloading, drag the folder BiliBiliLive into the Asset folder of Unity to complete the installation.

3. Operational requirements

  1. Unity2019 or higher
  2. c# 5.0 and above
  3. Running requires the Json plugin to parse Json, if your project has been imported, you can ignore it. If it is not imported, the Json file in the folder can be imported.

4. How to use

  1. You need to introduce namespace in main scriptusing Liluo.BiliBiliLive;
  2. Write the following script in any Mono script to establish a connection to the BiliBili studio. RoomID is the room number.
    Note that this plugin uses a lot of asynchronous programming. For such methods, you need to use async to decorate the class, and use await to wait for the completion of the function.
IBiliBiliLiveRequest req;
async void Init(int RoomID)
{
    
    
    // 创建一个直播间监听对象
    req = await BiliBiliLive.Connect(RoomID);
}
  1. To release the listener, use the DisConnect method to release.
void OnDestroy()
{
    
    
    // 释放监听对象
    req.DisConnect();
    req = null;
}
  1. Regularly monitor the number of people in the room
    This function is called at regular intervals, and its input parameter is the number of people (heat) in the current room.
req.OnRoomViewer = number =>
{
    
    
    Debug.Log($"当前房间人数为: {
      
      number}");
};
  1. Monitor the specified content
    The following is a function that is mainly used for monitoring, and its input parameters are the relevant information structures corresponding to the monitoring events. Press Ctrl+left button in VS to learn the specific information provided by the following structures.
/// 监听弹幕回调函数
public Action<BiliBiliLiveDanmuData> OnDanmuCallBack;

/// 监听礼物回调函数
public Action<BiliBiliLiveGiftData> OnGiftCallBack;

/// 监听上舰回调函数
public Action<BiliBiliLiveGuardData> OnGuardCallBack;

/// 监听SC回调函数
public Action<BiliBiliLiveSuperChatData> OnSuperChatCallBack;

4. Example startup script

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. Run the screenshot

insert image description here

Guess you like

Origin blog.csdn.net/qq_50682713/article/details/125116647