增加图片预加载逻辑
This commit is contained in:
16
src/Main.ts
16
src/Main.ts
@@ -1,6 +1,7 @@
|
||||
import { respath } from "./constants/respath"
|
||||
import { DCManager } from "./models/DCManager"
|
||||
import { LevelManager } from "./models/LevelManager"
|
||||
import { ResourceManager } from "./models/ResourceManager"
|
||||
import { StorageManager } from "./models/StorageManager"
|
||||
|
||||
const { regClass } = Laya
|
||||
@@ -12,20 +13,7 @@ export class Main extends Laya.Script {
|
||||
DCManager.getInstance().init()
|
||||
LevelManager.getInstance().init()
|
||||
StorageManager.getInstance().init()
|
||||
|
||||
const resources: Array<{ url: string, type?: any, lazy?: boolean }> = [
|
||||
{ url: respath.score_add_ui_res() },
|
||||
|
||||
{ url: respath.dc_top_bg_normal, type: Laya.Loader.IMAGE },
|
||||
{ url: respath.dc_top_bg_month_finish, type: Laya.Loader.IMAGE },
|
||||
{ url: respath.trophy_bg_light_gold, type: Laya.Loader.IMAGE },
|
||||
{ url: respath.trophy_bg_light_grey, type: Laya.Loader.IMAGE }
|
||||
]
|
||||
Laya.loader.load(resources, Laya.Handler.create(this, (success: object) => { // 第二个参数:成功回调
|
||||
console.log("预加载资源结束>>>>", success)
|
||||
}), Laya.Handler.create(this, (resProg: number) => { // 第三个参数:进度回调
|
||||
console.log("预加载资源进度>>>>", resProg)
|
||||
}))
|
||||
ResourceManager.getInstance().init()
|
||||
}
|
||||
|
||||
onLateUpdate(): void {
|
||||
|
||||
@@ -52,7 +52,7 @@ export const respath = {
|
||||
home_top_dc_bg_normal: "atlas/images/bg_dc_top1.png",
|
||||
home_top_dc_bg_finish: "atlas/images/bg_dc_top2.png",
|
||||
dc_top_bg_normal: "atlas/images/img_dc_bg2.png",
|
||||
dc_top_bg_month_finish: "atlas/images/img_dc_finished.png",
|
||||
dc_top_bg_finish: "atlas/images/img_dc_finished.png",
|
||||
|
||||
trophy_bg_light_gold: "atlas/trophys/img_trophy_bg_light_gold.png",
|
||||
trophy_bg_light_grey: "atlas/trophys/img_trophy_bg_light_gray.png",
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import { respath } from "../constants/respath"
|
||||
|
||||
|
||||
|
||||
export class ResourceManager {
|
||||
private static instance: ResourceManager
|
||||
public static getInstance(): ResourceManager {
|
||||
@@ -6,4 +10,92 @@ export class ResourceManager {
|
||||
}
|
||||
return ResourceManager.instance
|
||||
}
|
||||
|
||||
private resources: Array<{ url: string, type?: any, lazy?: boolean }> = []
|
||||
private reqs: Map<string, Array<Laya.Image>> = new Map()
|
||||
|
||||
|
||||
public init(): void {
|
||||
|
||||
this.resources.push({ url: respath.score_add_ui_res() })
|
||||
this.resources.push({ url: respath.dc_ui_res() })
|
||||
this.resources.push({ url: respath.trophy_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 })
|
||||
this.resources.push({ url: respath.dc_top_bg_normal, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.dc_top_bg_finish, type: Laya.Loader.IMAGE })
|
||||
|
||||
this.resources.push({ url: respath.trophy_bg_light_gold, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_bg_light_grey, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_01, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_02, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_03, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_04, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_05, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_06, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_07, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_08, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_09, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_10, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_11, type: Laya.Loader.IMAGE })
|
||||
this.resources.push({ url: respath.trophy_12, type: Laya.Loader.IMAGE })
|
||||
|
||||
Laya.loader.load(this.resources, Laya.Handler.create(this, (obj: object) => { // 第二个参数:成功回调
|
||||
console.log("预加载资源结束>>>>", obj)
|
||||
this.reqs.forEach((list: Array<Laya.Image>, path: string)=>{
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const img = list[i]
|
||||
if (img && img.parent) {
|
||||
img.texture = Laya.loader.getRes(path)
|
||||
}
|
||||
}
|
||||
})
|
||||
this.reqs.clear()
|
||||
}), Laya.Handler.create(this, (resProg: number) => { // 第三个参数:进度回调
|
||||
// console.log("预加载资源进度>>>>", resProg)
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
public loadTexture(path: string, image: Laya.Image): void {
|
||||
const obj = Laya.loader.getRes(path)
|
||||
// console.log("loadTexture obj", path, obj)
|
||||
image.skin = ""
|
||||
if (obj) {
|
||||
image.texture = obj
|
||||
} else {
|
||||
if (!this.reqs.has(path)) {
|
||||
this.reqs.set(path, new Array())
|
||||
}
|
||||
let list = this.reqs.get(path)
|
||||
list.push(image)
|
||||
|
||||
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("loadTexture find", path, find)
|
||||
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)
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const img = list[i]
|
||||
if (img && img.parent) {
|
||||
img.texture = obj
|
||||
}
|
||||
}
|
||||
this.reqs.delete(path)
|
||||
}), Laya.Handler.create(this, (resProg: number) => { // 第三个参数:进度回调
|
||||
// console.log("加载资源进度>>>>", path, resProg)
|
||||
}))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { config } from "../constants/config"
|
||||
import { respath } from "../constants/respath"
|
||||
import { G_getMonthInfo } from "../models/DCManager"
|
||||
import { ResourceManager } from "../models/ResourceManager"
|
||||
import { StorageManager } from "../models/StorageManager"
|
||||
import { UIManager } from "../models/UIManager"
|
||||
import { Utility_ConvertSecondToString } from "../utils/utility"
|
||||
@@ -42,8 +43,7 @@ export class Home extends Laya.Script {
|
||||
@property(Laya.Box)
|
||||
public btn_new_white: Laya.Box
|
||||
|
||||
// 组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
|
||||
onAwake(): void {
|
||||
onStart(): void {
|
||||
const user = StorageManager.getInstance().getUser()
|
||||
const now = new Date()
|
||||
const nowYear = now.getFullYear()
|
||||
@@ -51,18 +51,18 @@ export class Home extends Laya.Script {
|
||||
const nowDay = now.getDate()
|
||||
const find = G_getMonthInfo(nowYear, nowMonth)
|
||||
if (find) {
|
||||
this.icon_dc.skin = find.icon_res
|
||||
ResourceManager.getInstance().loadTexture(find.icon_res, this.icon_dc)
|
||||
if (user.get_trophyRecord(nowYear, nowMonth)) {
|
||||
this.bg_dc.skin = respath.home_top_dc_bg_finish
|
||||
ResourceManager.getInstance().loadTexture(respath.home_top_dc_bg_finish, this.bg_dc)
|
||||
this.label_dc_title.color = "#ffffff"
|
||||
this.label_dc_date.color = "#ffffff"
|
||||
} else {
|
||||
this.bg_dc.skin = respath.home_top_dc_bg_normal
|
||||
ResourceManager.getInstance().loadTexture(respath.home_top_dc_bg_normal, this.bg_dc)
|
||||
this.label_dc_title.color = "#2d3138"
|
||||
this.label_dc_date.color = "#2d3138"
|
||||
}
|
||||
} else {
|
||||
this.bg_dc.skin = respath.home_top_dc_bg_normal
|
||||
ResourceManager.getInstance().loadTexture(respath.home_top_dc_bg_normal, this.bg_dc)
|
||||
this.label_dc_title.color = "#2d3138"
|
||||
this.label_dc_date.color = "#2d3138"
|
||||
}
|
||||
|
||||
@@ -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 { StorageManager } from "../../models/StorageManager"
|
||||
import { UIManager } from "../../models/UIManager"
|
||||
import { Utility_ConvertSecondToString } from "../../utils/utility"
|
||||
@@ -188,14 +189,14 @@ export class DailyChallenge extends Laya.Script {
|
||||
const nowMonth = now.getMonth() + 1
|
||||
const nowDay = new Date().getDate()
|
||||
|
||||
this.trophy_icon.skin = info.icon_res
|
||||
ResourceManager.getInstance().loadTexture(info.icon_res, this.trophy_icon)
|
||||
// 设置背景
|
||||
if (user.get_trophyRecord(info.year, info.month)) {
|
||||
this.bg_dc.skin = respath.dc_top_bg_month_finish
|
||||
ResourceManager.getInstance().loadTexture(respath.dc_top_bg_finish, this.bg_dc)
|
||||
this.trophy_light.visible = true
|
||||
this.trophy_icon.gray = false
|
||||
} else {
|
||||
this.bg_dc.skin = respath.dc_top_bg_normal
|
||||
ResourceManager.getInstance().loadTexture(respath.dc_top_bg_normal, this.bg_dc)
|
||||
this.trophy_light.visible = false
|
||||
this.trophy_icon.gray = true
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { TrophyRecord } from "../../types/global"
|
||||
import { config } from "../../constants/config"
|
||||
import { MONTH_LIST } from "../../models/DCManager"
|
||||
import { ResourceManager } from "../../models/ResourceManager"
|
||||
|
||||
const { regClass, property } = Laya
|
||||
|
||||
@@ -21,7 +22,7 @@ export class TrophyClaim extends Laya.Script {
|
||||
for (let i = 0; i < MONTH_LIST.length; i++) {
|
||||
const obj = MONTH_LIST[i]
|
||||
if (obj.year === record.year && obj.month === record.month) {
|
||||
this.icon.skin = obj.icon_res
|
||||
ResourceManager.getInstance().loadTexture(obj.icon_res, this.icon)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { TrophyRecord } from "../../types/global"
|
||||
import { config } from "../../constants/config"
|
||||
import { MONTH_LIST } from "../../models/DCManager"
|
||||
import { ResourceManager } from "../../models/ResourceManager"
|
||||
|
||||
const { regClass, property } = Laya
|
||||
|
||||
@@ -21,7 +22,7 @@ export class TrophyShow extends Laya.Script {
|
||||
for (let i = 0; i < MONTH_LIST.length; i++) {
|
||||
const obj = MONTH_LIST[i]
|
||||
if (obj.year === record.year && obj.month === record.month) {
|
||||
this.icon.skin = obj.icon_res
|
||||
ResourceManager.getInstance().loadTexture(obj.icon_res, this.icon)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { config } from "../../constants/config"
|
||||
import { respath } from "../../constants/respath"
|
||||
import { ResourceManager } from "../../models/ResourceManager"
|
||||
import { StorageManager } from "../../models/StorageManager"
|
||||
import { UIManager } from "../../models/UIManager"
|
||||
|
||||
@@ -24,15 +25,15 @@ export class TrophyUnit extends Laya.Script {
|
||||
public onInit(info: any): void {
|
||||
const user = StorageManager.getInstance().getUser()
|
||||
const starCount = user.get_doneCountByDate(info.year, info.month)
|
||||
this.icon.skin = info.icon_res
|
||||
ResourceManager.getInstance().loadTexture(info.icon_res, this.icon)
|
||||
this.label_count.text = `${starCount}/${info.dayCount}`
|
||||
this.label_month.text = config.MONTH_ABBRS[info.month - 1]
|
||||
if (user.get_trophyRecord(info.year, info.month)) {
|
||||
this.bg_light.skin = respath.trophy_bg_light_gold
|
||||
ResourceManager.getInstance().loadTexture(respath.trophy_bg_light_gold, this.bg_light)
|
||||
this.icon.gray = false
|
||||
this.label_month.color = "#2d3138"
|
||||
} else {
|
||||
this.bg_light.skin = respath.trophy_bg_light_grey
|
||||
ResourceManager.getInstance().loadTexture(respath.trophy_bg_light_grey, this.bg_light)
|
||||
this.icon.gray = true
|
||||
this.label_month.color = "#a7abb3"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user