扩展UICallbacks对象,添加网络异常时,窗口加载失败回调。方便处理异常时的游戏体验逻辑

This commit is contained in:
donggang
2024-05-23 10:41:40 +08:00
parent 3040668482
commit f3d58d7931
5 changed files with 23 additions and 13 deletions

View File

@@ -30,7 +30,10 @@ export interface UICallbacks {
* @param node 当前界面节点
* @param next 回调方法
*/
onBeforeRemove?: (node: Node, next: Function) => void
onBeforeRemove?: (node: Node, next: Function) => void,
/** 网络异常时,窗口加载失败回调 */
onLoadFailure?: () => void;
}
/** 本类型仅供gui模块内部使用请勿在功能逻辑中使用 */

View File

@@ -37,9 +37,7 @@ export class DelegateComponent extends Component {
if (typeof this.vp.callbacks.onBeforeRemove === "function") {
this.vp.callbacks.onBeforeRemove(
this.node,
() => {
this.removed(this.vp, isDestroy);
});
this.onBeforeRemoveNext.bind(this, isDestroy));
}
else {
this.removed(this.vp, isDestroy);
@@ -47,6 +45,11 @@ export class DelegateComponent extends Component {
}
}
/** 窗口关闭前动画处理完后的回调方法,主要用于释放资源 */
private onBeforeRemoveNext(isDestroy?: boolean) {
this.removed(this.vp, isDestroy);
}
/** 窗口组件中触发移除事件与释放窗口对象 */
private removed(vp: ViewParams, isDestroy?: boolean) {
vp.valid = false;

View File

@@ -191,7 +191,10 @@ export class LayerManager {
return new Promise<Node | null>((resolve, reject) => {
var callbacks: UICallbacks = {
onAdded: (node: Node, params: any) => {
resolve(node)
resolve(node);
},
onLoadFailure: () => {
resolve(null);
}
};
this.open(uiId, uiArgs, callbacks);

View File

@@ -1,4 +1,4 @@
import { error, instantiate, Node, Prefab, warn, Widget } from "cc";
import { instantiate, Node, Prefab, Widget } from "cc";
import { oops } from "../../Oops";
import { UICallbacks, ViewParams } from "./Defines";
import { DelegateComponent } from "./DelegateComponent";
@@ -35,7 +35,7 @@ export class LayerUI extends Node {
*/
add(config: UIConfig, params?: any, callbacks?: UICallbacks) {
if (this.ui_nodes.has(config.prefab)) {
warn(`路径为【${config.prefab}】的预制重复加载`);
console.warn(`路径为【${config.prefab}】的预制重复加载`);
return;
}
@@ -69,7 +69,8 @@ export class LayerUI extends Node {
oops.res.load(bundle, vp.config.prefab, (err: Error | null, res: Prefab) => {
if (err) {
this.ui_nodes.delete(vp.config.prefab);
error(`路径为【${vp.config.prefab}】的预制加载失败`);
console.warn(`路径为【${vp.config.prefab}】的预制加载失败`);
vp.callbacks && vp.callbacks.onLoadFailure && vp.callbacks.onLoadFailure();
return;
}

View File

@@ -1,5 +1,5 @@
/** 列表 */
export class List<T> {
export class List<T> {
private element: Array<T>;
/** 是否保证元素的唯一性 */
@@ -54,9 +54,9 @@ export class List<T> {
if (this.element.length > 0) {
const result = this.element.pop();
this.count = this.element.length;
return result;
return result!;
}
return null;
return null!;
}
/**
@@ -67,9 +67,9 @@ export class List<T> {
if (this.element.length > 0) {
const result = this.element.shift();
this.count = this.element.length;
return result;
return result!;
}
return null;
return null!;
}
/**