This commit is contained in:
2025-09-11 10:54:06 +08:00
parent 53f2037c49
commit a590433a91
7 changed files with 413 additions and 419 deletions
+71 -42
View File
@@ -5,13 +5,17 @@ import {
Sprite,
UITransform,
VideoPlayer,
VideoClip,
view,
Size,
Vec3,
} from "cc";
import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { ViewManager } from "db://assets/Scripts/Main/Manager/ViewManager";
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
import { SceneBgVideoLayer } from "../../../Sub/UI/SceneBgVideoLayer";
const { ccclass, property } = _decorator;
interface ShowPanelData {
@@ -26,6 +30,8 @@ export class ShowPanel extends li_BaseView {
image: Sprite;
@property(Sprite)
splash: Sprite;
@property(UITransform)
videoArea: UITransform;
@property(VideoPlayer)
videoPlayer: VideoPlayer;
@@ -41,15 +47,6 @@ export class ShowPanel extends li_BaseView {
onLoadCT() {
this.show(this.url);
// 添加视频加载完成回调
this.videoPlayer.node.on(
VideoPlayer.EventType.READY_TO_PLAY,
() => {
this.adjustVideoScale();
},
this
);
}
start() {
@@ -58,6 +55,11 @@ export class ShowPanel extends li_BaseView {
}
protected onClose() {
// 停止视频播放
if (this.videoPlayer) {
this.videoPlayer.stop();
}
if (this.func) {
this.func();
}
@@ -70,42 +72,69 @@ export class ShowPanel extends li_BaseView {
Utils.adjustBgPixelRatio(this.image.node, 3);
});
} else {
ResManager.I.changeBundleVideo(this.videoPlayer, path, "Girls", () => {
this.adjustVideoScale();
});
// 使用自己的 videoPlayer 进行置顶显示
if (this.videoPlayer) {
const targetSize = this.videoArea
? new Size(this.videoArea.width, this.videoArea.height)
: new Size(600, 800);
ResManager.I.changeBundleVideo(
this.videoPlayer,
path,
"Girls",
(res: VideoClip) => {
// 设置视频大小和位置
const uiTransform = this.videoPlayer.node.getComponent(UITransform);
if (uiTransform && this.videoArea) {
uiTransform.setContentSize(targetSize.width, targetSize.height);
// 设置位置为 videoArea 的位置
const pos = this.videoArea.node.getWorldPosition();
this.videoPlayer.node.setWorldPosition(pos);
}
this.videoPlayer.loop = true;
this.videoPlayer.play();
// 延迟调整缩放以填充区域
this.scheduleOnce(() => {
this.adjustVideoScale();
}, 0.1);
}
);
} else {
// 如果没有配置 videoPlayer,使用 SceneBgVideoLayer 作为后备
const targetSize = this.videoArea
? new Size(this.videoArea.width, this.videoArea.height)
: new Size(600, 800);
const videoAreaPos = this.videoArea
? this.videoArea.node.getWorldPosition()
: undefined;
if (SceneBgVideoLayer.handle) {
SceneBgVideoLayer.handle.playVideo(path, {
loop: true,
size: targetSize,
position: videoAreaPos,
});
}
}
}
}
adjustVideoScale() {
if (!this.videoPlayer) {
return;
}
const tryAdjustScale = () => {
const screenSize = view.getVisibleSize();
const videoTransform = this.videoPlayer.node.getComponent(UITransform);
if (videoTransform && videoTransform.height > 0) {
let scale = screenSize.width / videoTransform.width;
this.videoPlayer.node.setScale(scale, scale, 1);
return true;
}
return false;
};
// 立即尝试一次
if (!tryAdjustScale()) {
// 如果失败,延迟尝试
this.scheduleOnce(() => {
if (!tryAdjustScale()) {
// 再次延迟尝试
this.scheduleOnce(() => {
tryAdjustScale();
}, 0.5);
}
}, 0.1);
/**
* 调整视频缩放以填满区域
*/
private adjustVideoScale() {
if (!this.videoPlayer || !this.videoArea) return;
const videoTransform = this.videoPlayer.node.getComponent(UITransform);
if (videoTransform && videoTransform.height > 0) {
const scaleX = this.videoArea.width / videoTransform.width;
const scaleY = this.videoArea.height / videoTransform.height;
const scale = Math.max(scaleX, scaleY); // 填满整个区域
this.videoPlayer.node.setScale(new Vec3(scale, scale, 1));
console.log(`ShowPanel: 调整视频缩放到 ${scale}`);
}
}
}