弹幕功能

This commit is contained in:
fatiao 2025-10-09 18:03:43 +08:00
parent 2d9b233a12
commit f6fa9ae1a1
7 changed files with 81 additions and 14 deletions

View File

@ -0,0 +1,46 @@
local DanmuData = class("DanmuData", require("DataBase"))
function DanmuData:ctor()
self.data = {}
end
function DanmuData:RegisterNetEvents()
end
function DanmuData:Clear()
self.data = {}
end
function DanmuData:Destroy()
if self.Clear then
self:Clear()
end
self:UnRegisterNetEvents()
end
function DanmuData:UnRegisterNetEvents()
end
function DanmuData:SendDanmu(danmuChannel, content)
local head = 12
local formatStr = PlatformPack.SERVERLIST_URL.."/senddanmu".."?channel=%s&head=%d&content=%s"
local url = string.format(formatStr, danmuChannel, head, content)
ManagerContainer.LuaGameMgr:RequestHttpServer(url, function(responseStr)
local response = JSON:decode(responseStr)
if response.err == "OK" then
end
end)
end
function DanmuData:GetDanmu(danmuChannel, index, count, onResp)
local formatStr = PlatformPack.SERVERLIST_URL.."/getdanmu".."?channel=%s&index=%d&count=%d"
local url = string.format(formatStr, danmuChannel, index, count)
ManagerContainer.LuaGameMgr:RequestHttpServer(url, function(responseStr)
local response = JSON:decode(responseStr)
if response.err then return end
onResp(response)
end)
end
return DanmuData

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 0ac68ae27e5669c4a807c01d5c4c8567
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -52,6 +52,7 @@ function DataMgr:ctor()
self.CombineServerDataMgr = require('CombineServerDataMgr'):new()
self.FruitData = require("FruitData"):new()
self.DanmuData = require("DanmuData"):new()
end
function DataMgr:RegisterAllDataEvents()

View File

@ -440,20 +440,6 @@ function LuaGameMgr:StopStory(storyType)
LuaBattleBridge.CurStoryOver(storyType)
end
-- 发送弹幕
function LuaGameMgr:SendDanmu(danmuChannel, content)
local formatStr = PlatformPack.DANMU_URL.."?type=set&channel=%d&content=%s"
local url = string.format(formatStr, danmuChannel, content)
ManagerContainer.LuaGameMgr:RequestHttpServer(url, function(responseStr) end)
end
-- 获取弹幕
function LuaGameMgr:GetDanmu(danmuChannel, index, count)
local formatStr = PlatformPack.DANMU_URL.."?type=get&channel=%d&index=%d&count=%d"
local url = string.format(formatStr, danmuChannel, index, count)
ManagerContainer.LuaGameMgr:RequestHttpServer(url, function(responseStr) end)
end
function LuaGameMgr:SetCreateRoleStatus(status)
self.IsCreateRole = status
end

View File

@ -1,8 +1,11 @@
using System;
using UnityEngine;
using System.Collections.Generic;
using TMPro;
using System.Collections;
using System.Linq;
using LuaInterface;
using Random = UnityEngine.Random;
[RequireComponent(typeof(RectTransform))]
public class DanMuPlayer : MonoBehaviour
@ -44,6 +47,7 @@ public class DanMuPlayer : MonoBehaviour
[Header("Loop Settings")]
[SerializeField] private float loopInterval = 3f;
[SerializeField] private int maxLoopDanmakuCount = 50;
private int lastPlayFinishCount = 10;
private RectTransform canvasRect;
private Queue<GameObject> danmakuPool = new Queue<GameObject>();
@ -55,6 +59,7 @@ public class DanMuPlayer : MonoBehaviour
private List<string> loopDanmakuList = new List<string>();
private Coroutine loopCoroutine;
private bool isLooping = false;
private Action onPlayFinished = null;
private class DanmakuItem
{
@ -144,6 +149,13 @@ public class DanMuPlayer : MonoBehaviour
{
ReturnDanmakuToPool(item);
activeDanmakus.RemoveAt(i);
if (activeDanmakus.Count == lastPlayFinishCount)
{
if (onPlayFinished != null)
{
onPlayFinished();
}
}
trackOccupiedUntil[item.trackIndex] = Mathf.Min(trackOccupiedUntil[item.trackIndex], Time.time);
}
}
@ -158,6 +170,7 @@ public class DanMuPlayer : MonoBehaviour
danmakuPool.Enqueue(item.obj);
}
[NoToLua]
public void AddToLoopList(string content)
{
if (!string.IsNullOrEmpty(content))
@ -170,6 +183,14 @@ public class DanMuPlayer : MonoBehaviour
}
}
public void PlayDanmu(string[] contents, int lastPlayFinishCount, Action onPlayFinished)
{
this.onPlayFinished = onPlayFinished;
this.lastPlayFinishCount = lastPlayFinishCount;
Play(contents.ToList(), false);
}
[NoToLua]
public void Play(List<string> contents, bool loop = false)
{
ClearAllDanmakus();
@ -187,6 +208,7 @@ public class DanMuPlayer : MonoBehaviour
}
}
[NoToLua]
public void ToggleLoop(bool enable)
{
isLooping = enable;
@ -200,6 +222,7 @@ public class DanMuPlayer : MonoBehaviour
}
}
[NoToLua]
public int GetLoopListCount()
{
return loopDanmakuList.Count;
@ -406,6 +429,7 @@ public class DanMuPlayer : MonoBehaviour
}
}
[NoToLua]
public void StopLoopPlay()
{
isLooping = false;
@ -416,6 +440,7 @@ public class DanMuPlayer : MonoBehaviour
}
}
[NoToLua]
public void ClearAllDanmakus()
{
StopLoopPlay();

View File

@ -322,6 +322,7 @@ public static class CustomSettings
_GT (typeof (UiLoopAutoMove)),
_GT (typeof(GUIUtility)),
_GT (typeof(UnityEngineUtils)),
_GT(typeof(DanMuPlayer))
};
public static List<Type> dynamicList = new List<Type>() {

View File

@ -102,6 +102,7 @@ public static class LuaBinder
UIImageSwitchSpriteWrap.Register(L);
RolRectTransformWrap.Register(L);
UiLoopAutoMoveWrap.Register(L);
DanMuPlayerWrap.Register(L);
SingletonMono_GameMgrWrap.Register(L);
MonoBaseWrap.Register(L);
UIBaseWrap.Register(L);