页面h5适配
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
import { _decorator, Component, instantiate, Node, Vec3 } from "cc";
|
||||
import {
|
||||
_decorator,
|
||||
Component,
|
||||
instantiate,
|
||||
Node,
|
||||
UITransform,
|
||||
Vec3,
|
||||
} from "cc";
|
||||
import { DialogManager } from "../../manager/DialogManager";
|
||||
import { DialogBubble } from "./DialogBubble";
|
||||
import { Dialog } from "../../data/DialogData";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
const BUTTOM_Y = -955.571;
|
||||
|
||||
@ccclass("ChatContentsLayout")
|
||||
export class ChatContentsLayout extends Component {
|
||||
manager: DialogManager = null;
|
||||
|
||||
@property(Node)
|
||||
initPos: Node = null;
|
||||
|
||||
@property(DialogBubble)
|
||||
lBubble: DialogBubble = null;
|
||||
@property(DialogBubble)
|
||||
@@ -18,9 +26,12 @@ export class ChatContentsLayout extends Component {
|
||||
bubbles: DialogBubble[] = [];
|
||||
cachedBubbles: DialogBubble[] = [];
|
||||
|
||||
fixMaxWidth: number;
|
||||
|
||||
protected start(): void {
|
||||
this.lBubble.node.active = false;
|
||||
this.rBubble.node.active = false;
|
||||
this.fixMaxWidth = this.node.getComponent(UITransform).contentSize.y * 0.6;
|
||||
}
|
||||
|
||||
protected onEnable(): void {
|
||||
@@ -38,22 +49,22 @@ export class ChatContentsLayout extends Component {
|
||||
}
|
||||
}
|
||||
this.bubbles = [];
|
||||
|
||||
|
||||
this.updateDialogLayout(dialogs);
|
||||
|
||||
|
||||
// 清理多余的缓存气泡,避免内存泄漏
|
||||
this.cleanupExcessCachedBubbles();
|
||||
}
|
||||
|
||||
private updateDialogLayout(dialogs: Dialog[]) {
|
||||
let initPosY: number = BUTTOM_Y;
|
||||
let initPosY: number = this.initPos.position.y;
|
||||
let pendingUpdates = 0;
|
||||
|
||||
|
||||
const updateNextBubble = (index: number) => {
|
||||
if (index < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const dialog = dialogs[index];
|
||||
let newBubble: DialogBubble = null;
|
||||
|
||||
@@ -64,7 +75,10 @@ export class ChatContentsLayout extends Component {
|
||||
if (cached && cached.node) {
|
||||
// 检查气泡类型是否匹配(通过位置判断左右气泡)
|
||||
const isRightBubble = cached.node.position.x > 0;
|
||||
if ((isPlayerBubble && isRightBubble) || (!isPlayerBubble && !isRightBubble)) {
|
||||
if (
|
||||
(isPlayerBubble && isRightBubble) ||
|
||||
(!isPlayerBubble && !isRightBubble)
|
||||
) {
|
||||
newBubble = cached;
|
||||
this.cachedBubbles.splice(j, 1);
|
||||
break;
|
||||
@@ -78,27 +92,28 @@ export class ChatContentsLayout extends Component {
|
||||
? instantiate(this.rBubble.node)
|
||||
: instantiate(this.lBubble.node);
|
||||
newBubble = newBubbleNode.getComponent(DialogBubble);
|
||||
newBubble.init(this.fixMaxWidth);
|
||||
newBubbleNode.setParent(this.node);
|
||||
}
|
||||
|
||||
newBubble.node.active = true;
|
||||
let pos = newBubble.node.position;
|
||||
newBubble.node.position = new Vec3(pos.x, initPosY, pos.z);
|
||||
|
||||
|
||||
pendingUpdates++;
|
||||
newBubble.updateBubbleContent(dialog.content, (actualHeight: number) => {
|
||||
initPosY += actualHeight + 20;
|
||||
pendingUpdates--;
|
||||
|
||||
|
||||
// 当当前气泡更新完成后,处理下一个
|
||||
if (pendingUpdates === 0) {
|
||||
updateNextBubble(index - 1);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
this.bubbles.push(newBubble);
|
||||
};
|
||||
|
||||
|
||||
// 从最后一个对话开始处理(倒序)
|
||||
updateNextBubble(dialogs.length - 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user