添加预制预加载逻辑

This commit is contained in:
2025-06-05 17:00:48 +08:00
parent 609b952823
commit ee54b50bb5
12 changed files with 1757 additions and 1685 deletions

View File

@@ -1,3 +1,4 @@
import { config } from "../constants/config"
import { respath } from "../constants/respath"
export class ResourceManager {
@@ -10,12 +11,27 @@ export class ResourceManager {
}
private resources: Array<{ url: string, type?: any, lazy?: boolean }> = []
private reqs: Map<string, Array<Laya.Image>> = new Map()
private textureReqs: Map<string, Array<Laya.Image>> = new Map()
private prefabReqs: Map<string, Array<any>> = new Map()
public init(): void {
this.resources.push({ url: respath.gamepause_ui_res() })
this.resources.push({ url: respath.gameover_ui_res() })
this.resources.push({ url: respath.gamedone_ui_res(true) })
this.resources.push({ url: respath.gamedone_ui_res(false) })
this.resources.push({ url: respath.score_add_ui_res() })
this.resources.push({ url: respath.dc_ui_res() })
this.resources.push({ url: respath.trophy_claim_ui_res() })
this.resources.push({ url: respath.trophy_show_ui_res() })
this.resources.push({ url: respath.dc_ui_res() })
this.resources.push({ url: respath.trophy_ui_res() })
this.resources.push({ url: respath.trophy_year_ui_res() })
this.resources.push({ url: respath.trophy_cell_ui_res() })
if (config.H_SCREEN) {
this.resources.push({ url: respath.common_tips_ui_res })
} else {
this.resources.push({ url: respath.difficulty_ui_res })
}
this.resources.push({ url: respath.home_top_dc_bg_normal, type: Laya.Loader.IMAGE })
this.resources.push({ url: respath.home_top_dc_bg_finish, type: Laya.Loader.IMAGE })
@@ -39,7 +55,7 @@ export class ResourceManager {
Laya.loader.load(this.resources, Laya.Handler.create(this, (obj: object) => { // 第二个参数:成功回调
console.log("预加载资源结束>>>>", obj)
this.reqs.forEach((list: Array<Laya.Image>, path: string) => {
this.textureReqs.forEach((list: Array<Laya.Image>, path: string) => {
for (let i = 0; i < list.length; i++) {
const img = list[i]
if (img && img.parent) {
@@ -47,10 +63,27 @@ export class ResourceManager {
}
}
})
this.reqs.clear()
this.textureReqs.clear()
}), Laya.Handler.create(this, (resProg: number) => { // 第三个参数:进度回调
console.log("预加载资源进度>>>>", resProg)
}))
Laya.timer.frameLoop(1, this, ()=>{
let removes: Array<any> = []
this.prefabReqs.forEach((list: Array<any>, path: string)=>{
const obj = Laya.loader.getRes(path)
if (obj) {
removes.push(path)
for (let i = 0; i < list.length; i++) {
list[i](obj)
}
}
})
for (let i = 0; i < removes.length; i++) {
this.prefabReqs.delete(removes[i])
}
})
}
public loadTexture(path: string, image: Laya.Image): void {
@@ -60,10 +93,10 @@ export class ResourceManager {
if (obj) {
image.texture = obj
} else {
if (!this.reqs.has(path)) {
this.reqs.set(path, [])
if (!this.textureReqs.has(path)) {
this.textureReqs.set(path, [])
}
const list = this.reqs.get(path)
const list = this.textureReqs.get(path)
list.push(image)
let find = false
@@ -78,16 +111,49 @@ export class ResourceManager {
if (find === false) {
Laya.loader.load(path, Laya.Handler.create(this, (obj: Laya.Texture) => { // 第二个参数:成功回调
// console.log("加载资源成功>>>>", path, obj, image, image.parent)
const list = this.reqs.get(path)
const list = this.textureReqs.get(path)
for (let i = 0; i < list.length; i++) {
const img = list[i]
if (img && img.parent) {
img.texture = obj
}
}
this.reqs.delete(path)
this.textureReqs.delete(path)
}))
}
}
}
public loadPrefab(path: string, callback: any): void {
const obj = Laya.loader.getRes(path)
// console.log("loadPrefab >>>>>", path, obj)
if (obj) {
if (callback) {
callback(obj)
}
} else {
if (callback) {
if (!this.prefabReqs.has(path)) {
this.prefabReqs.set(path, [])
}
const list = this.prefabReqs.get(path)
list.push(callback)
}
let find = false
for (let i = 0; i < this.resources.length; i++) {
const info = this.resources[i]
if (info.url === path) {
find = true
break
}
}
// console.log("loadPrefab find", path, find)
if (find === false) {
Laya.loader.load(path, Laya.Handler.create(this, (obj: object) => { // 第二个参数:成功回调
console.log("加载资源成功>>>>", path, obj)
}))
}
}
}
}

View File

@@ -12,6 +12,7 @@ import { GamePause } from "../views/GamePause"
import { Stage } from "../views/Stage"
import { TopBar } from "../views/TopBar"
import { StorageManager } from "./StorageManager"
import { ResourceManager } from "./ResourceManager"
const { regClass, property } = Laya
@@ -78,14 +79,14 @@ export class UIManager extends Laya.Script {
}
public loadHomeUI(): void {
Laya.loader.load(respath.home_ui_res).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.home_ui_res, (go: any)=> {
const prefab = go.create()
this.getUIRoot().addChild(prefab)
})
}
public loadTopBarUI(): void {
Laya.loader.load(respath.topbar_ui_res).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.topbar_ui_res, (go: any)=> {
const prefab = go.create()
this.topbar = this.getUIRoot().addChild(prefab).getComponent(TopBar)
})
@@ -105,7 +106,7 @@ export class UIManager extends Laya.Script {
public loadDCUI(): void {
this.mask.visible = true
Laya.loader.load(respath.dc_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.dc_ui_res(), (go: any)=> {
const prefab = go.create()
this.dc_ui = this.getUIRoot().addChild(prefab).getComponent(DailyChallenge)
if (this.dc_year <= 0 || this.dc_month <= 0) {
@@ -133,7 +134,7 @@ export class UIManager extends Laya.Script {
}
public loadTrophyClaimUI(record: TrophyRecord): void {
Laya.loader.load(respath.trophy_claim_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.trophy_claim_ui_res(), (go: any)=> {
const prefab = go.create()
const obj = this.getUIRoot().addChild(prefab).getComponent(TrophyClaim)
obj.onSetShow(record)
@@ -142,7 +143,7 @@ export class UIManager extends Laya.Script {
public loadTrophyUI(): void {
this.mask.visible = true
Laya.loader.load(respath.trophy_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.trophy_ui_res(), (go: any)=> {
const prefab = go.create()
this.trophyRoom = this.getUIRoot().addChild(prefab).getComponent(TrophyRoom)
this.mask.visible = false
@@ -158,7 +159,7 @@ export class UIManager extends Laya.Script {
public loadTrophyShowUI(record: TrophyRecord): void {
this.mask.visible = true
Laya.loader.load(respath.trophy_show_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.trophy_show_ui_res(), (go: any)=> {
const prefab = go.create()
const obj = this.getUIRoot().addChild(prefab).getComponent(TrophyShow)
obj.onSetShow(record)
@@ -170,7 +171,7 @@ export class UIManager extends Laya.Script {
if (this.stage) {
this.stage.onLoadStage(StorageManager.getInstance().loadStage(stageID))
} else {
Laya.loader.load(respath.stage_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.stage_ui_res(), (go: any)=> {
const prefab = go.create()
this.stage = this.getUIRoot().addChild(prefab).getComponent(Stage)
this.stage.onLoadStage(StorageManager.getInstance().loadStage(stageID))
@@ -187,7 +188,7 @@ export class UIManager extends Laya.Script {
public loadGamePauseUI(doStage: DOStage): void {
this.mask.visible = true
Laya.loader.load(respath.gamepause_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.gamepause_ui_res(), (go: any)=> {
const prefab = go.create()
const ui = this.getUIRoot().addChild(prefab).getComponent(GamePause)
ui.onSetStageInfo(doStage)
@@ -197,7 +198,7 @@ export class UIManager extends Laya.Script {
public loadGameOverUI(doStage: DOStage): void {
this.mask.visible = true
Laya.loader.load(respath.gameover_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.gameover_ui_res(), (go: any)=> {
const prefab = go.create()
const ui = this.getUIRoot().addChild(prefab).getComponent(GameOver)
ui.onSetStageInfo(doStage)
@@ -207,7 +208,7 @@ export class UIManager extends Laya.Script {
public loadGameDoneUI(isClassic: boolean, doStage: DOStage): void {
this.mask.visible = true
Laya.loader.load(respath.gamedone_ui_res(isClassic)).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.gamedone_ui_res(isClassic), (go: any)=> {
const prefab = go.create()
const ui = this.getUIRoot().addChild(prefab).getComponent(GameDone)
ui.onSetStageInfo(doStage)

View File

@@ -1,10 +1,11 @@
import { respath } from "../constants/respath"
import { ResourceManager } from "../models/ResourceManager"
import { UIManager } from "../models/UIManager"
const { regClass, property } = Laya
export function G_ShowCommonTips(title: string, content: string, showToggle: boolean, func: any): void {
Laya.loader.load(respath.common_tips_ui_res).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.common_tips_ui_res, (go: any)=> {
const prefab = go.create()
const obj = UIManager.getInstance().getUIRoot().addChild(prefab).getComponent(CommonTips)
obj.showCommonTips(title, content, showToggle, func)

View File

@@ -5,6 +5,7 @@ import { StorageManager } from "../models/StorageManager"
import { UIManager } from "../models/UIManager"
import { Utility_ConvertSecondToString } from "../utils/utility"
import { Difficulty } from "./Difficulty"
import { ResourceManager } from "../models/ResourceManager"
const { regClass, property } = Laya
@@ -115,7 +116,7 @@ export class GameDone extends Laya.Script {
UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
this.owner.destroy()
} else {
Laya.loader.load(respath.difficulty_ui_res).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.difficulty_ui_res, (go: any)=> {
const prefab = go.create()
const d = UIManager.getInstance().getUIRoot().addChild(prefab).getComponent(Difficulty)
d.onInit((value: string) => {

View File

@@ -5,6 +5,7 @@ import { EVENT_TYPES, EventManager } from "../models/EventManager"
import { StorageManager } from "../models/StorageManager"
import { UIManager } from "../models/UIManager"
import { Difficulty } from "./Difficulty"
import { ResourceManager } from "../models/ResourceManager"
const { regClass, property } = Laya
@@ -52,7 +53,7 @@ export class GameOver extends Laya.Script {
UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
this.owner.destroy()
} else {
Laya.loader.load(respath.difficulty_ui_res).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.difficulty_ui_res, (go: any)=> {
const prefab = go.create()
const d = UIManager.getInstance().getUIRoot().addChild(prefab).getComponent(Difficulty)
d.onInit((value: string) => {

View File

@@ -139,7 +139,7 @@ export class Home extends Laya.Script {
}
public onClickNew(_evt: Laya.Event): void {
Laya.loader.load(respath.difficulty_ui_res).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.difficulty_ui_res, (go: any)=> {
const prefab = go.create()
const d = UIManager.getInstance().getUIRoot().addChild(prefab).getComponent(Difficulty)
d.onInit((value: string) => {

View File

@@ -1,8 +1,9 @@
import { respath } from "../../constants/respath"
import { ResourceManager } from "../../models/ResourceManager"
import { ScoreAdd } from "../ScoreAdd"
export function G_ShowScoreTips(text: string, parent: any): void {
Laya.loader.load(respath.score_add_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.score_add_ui_res(), (go: any)=> {
const prefab = go.create()
const score = parent.addChild(prefab).getComponent(ScoreAdd)
score.onSetText(text)

View File

@@ -2,6 +2,7 @@ import { config } from "../../constants/config"
import { respath } from "../../constants/respath"
import { MONTH_LIST } from "../../models/DCManager"
import { EVENT_TYPES, EventManager } from "../../models/EventManager"
import { ResourceManager } from "../../models/ResourceManager"
import { UIManager } from "../../models/UIManager"
import { TrophyUnit } from "./TrophyUnit"
@@ -55,7 +56,7 @@ export class TrophyRoom extends Laya.Script {
list.push(obj)
}
console.log(">>>>>>>>>>>>years=", years)
Laya.loader.load(respath.trophy_year_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.trophy_year_ui_res(), (go: any)=> {
for (let i = 1; i < years.length; i++) {
const obj_year = this.obj_items.getChildAt(i)
if (!obj_year) {
@@ -64,7 +65,7 @@ export class TrophyRoom extends Laya.Script {
}
}
Laya.loader.load(respath.trophy_cell_ui_res()).then((go) => {
ResourceManager.getInstance().loadPrefab(respath.trophy_cell_ui_res(), (go: any)=> {
let itemsHeight = 0
for (let i = 0; i < years.length; i++) {
const list = monthMap.get(years[i])