添加预制预加载逻辑
This commit is contained in:
@@ -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)
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user