增加图片预加载逻辑

This commit is contained in:
2025-06-05 14:57:04 +08:00
parent 10333a3bdc
commit 9d2a177325
15 changed files with 123 additions and 40 deletions

View File

@@ -62,10 +62,9 @@
"_$id": "7270vjom", "_$id": "7270vjom",
"_$type": "Image", "_$type": "Image",
"name": "bg", "name": "bg",
"width": 16, "width": 936,
"height": 16, "height": 468,
"skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"useSourceSize": true,
"color": "#ffffff" "color": "#ffffff"
}, },
{ {

View File

@@ -103,7 +103,7 @@
"left": 0, "left": 0,
"right": 0, "right": 0,
"top": 0, "top": 0,
"skin": "res://4182ef3f-6065-44dd-83c2-70490f02e9cb", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {
@@ -129,7 +129,7 @@
"height": 468, "height": 468,
"top": 110, "top": 110,
"centerX": 225, "centerX": 225,
"skin": "res://09690a9b-3d5a-4da1-b457-1a3275cf5c14", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {

View File

@@ -96,7 +96,7 @@
"height": 960, "height": 960,
"left": 0, "left": 0,
"top": 0, "top": 0,
"skin": "res://4182ef3f-6065-44dd-83c2-70490f02e9cb", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {
@@ -123,7 +123,7 @@
"height": 600, "height": 600,
"bottom": 30, "bottom": 30,
"centerX": 0, "centerX": 0,
"skin": "res://09690a9b-3d5a-4da1-b457-1a3275cf5c14", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {

View File

@@ -71,7 +71,7 @@
"height": 834, "height": 834,
"centerX": 0, "centerX": 0,
"centerY": 0, "centerY": 0,
"skin": "res://7d235ac1-6755-4409-ab86-23aaf41e1537", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {

View File

@@ -73,7 +73,7 @@
"height": 600, "height": 600,
"centerX": 0, "centerX": 0,
"centerY": 0, "centerY": 0,
"skin": "res://7d235ac1-6755-4409-ab86-23aaf41e1537", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {

View File

@@ -71,7 +71,7 @@
"height": 834, "height": 834,
"centerX": 0, "centerX": 0,
"centerY": 0, "centerY": 0,
"skin": "res://7d235ac1-6755-4409-ab86-23aaf41e1537", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {

View File

@@ -73,7 +73,7 @@
"height": 600, "height": 600,
"centerX": 0, "centerX": 0,
"centerY": 0, "centerY": 0,
"skin": "res://7d235ac1-6755-4409-ab86-23aaf41e1537", "skin": "res://07b90718-84ee-4924-9098-8f0ee3663d92",
"color": "#ffffff" "color": "#ffffff"
}, },
{ {

View File

@@ -1,6 +1,7 @@
import { respath } from "./constants/respath" import { respath } from "./constants/respath"
import { DCManager } from "./models/DCManager" import { DCManager } from "./models/DCManager"
import { LevelManager } from "./models/LevelManager" import { LevelManager } from "./models/LevelManager"
import { ResourceManager } from "./models/ResourceManager"
import { StorageManager } from "./models/StorageManager" import { StorageManager } from "./models/StorageManager"
const { regClass } = Laya const { regClass } = Laya
@@ -12,20 +13,7 @@ export class Main extends Laya.Script {
DCManager.getInstance().init() DCManager.getInstance().init()
LevelManager.getInstance().init() LevelManager.getInstance().init()
StorageManager.getInstance().init() StorageManager.getInstance().init()
ResourceManager.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)
}))
} }
onLateUpdate(): void { onLateUpdate(): void {

View File

@@ -52,7 +52,7 @@ export const respath = {
home_top_dc_bg_normal: "atlas/images/bg_dc_top1.png", home_top_dc_bg_normal: "atlas/images/bg_dc_top1.png",
home_top_dc_bg_finish: "atlas/images/bg_dc_top2.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_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_gold: "atlas/trophys/img_trophy_bg_light_gold.png",
trophy_bg_light_grey: "atlas/trophys/img_trophy_bg_light_gray.png", trophy_bg_light_grey: "atlas/trophys/img_trophy_bg_light_gray.png",

View File

@@ -1,3 +1,7 @@
import { respath } from "../constants/respath"
export class ResourceManager { export class ResourceManager {
private static instance: ResourceManager private static instance: ResourceManager
public static getInstance(): ResourceManager { public static getInstance(): ResourceManager {
@@ -6,4 +10,92 @@ export class ResourceManager {
} }
return ResourceManager.instance 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)
}))
}
}
}
} }

View File

@@ -1,6 +1,7 @@
import { config } from "../constants/config" import { config } from "../constants/config"
import { respath } from "../constants/respath" import { respath } from "../constants/respath"
import { G_getMonthInfo } from "../models/DCManager" import { G_getMonthInfo } from "../models/DCManager"
import { ResourceManager } from "../models/ResourceManager"
import { StorageManager } from "../models/StorageManager" import { StorageManager } from "../models/StorageManager"
import { UIManager } from "../models/UIManager" import { UIManager } from "../models/UIManager"
import { Utility_ConvertSecondToString } from "../utils/utility" import { Utility_ConvertSecondToString } from "../utils/utility"
@@ -42,8 +43,7 @@ export class Home extends Laya.Script {
@property(Laya.Box) @property(Laya.Box)
public btn_new_white: Laya.Box public btn_new_white: Laya.Box
// 组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次 onStart(): void {
onAwake(): void {
const user = StorageManager.getInstance().getUser() const user = StorageManager.getInstance().getUser()
const now = new Date() const now = new Date()
const nowYear = now.getFullYear() const nowYear = now.getFullYear()
@@ -51,18 +51,18 @@ export class Home extends Laya.Script {
const nowDay = now.getDate() const nowDay = now.getDate()
const find = G_getMonthInfo(nowYear, nowMonth) const find = G_getMonthInfo(nowYear, nowMonth)
if (find) { if (find) {
this.icon_dc.skin = find.icon_res ResourceManager.getInstance().loadTexture(find.icon_res, this.icon_dc)
if (user.get_trophyRecord(nowYear, nowMonth)) { 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_title.color = "#ffffff"
this.label_dc_date.color = "#ffffff" this.label_dc_date.color = "#ffffff"
} else { } 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_title.color = "#2d3138"
this.label_dc_date.color = "#2d3138" this.label_dc_date.color = "#2d3138"
} }
} else { } 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_title.color = "#2d3138"
this.label_dc_date.color = "#2d3138" this.label_dc_date.color = "#2d3138"
} }

View File

@@ -2,6 +2,7 @@ import { config } from "../../constants/config"
import { respath } from "../../constants/respath" import { respath } from "../../constants/respath"
import { MONTH_LIST } from "../../models/DCManager" import { MONTH_LIST } from "../../models/DCManager"
import { EVENT_TYPES, EventManager } from "../../models/EventManager" import { EVENT_TYPES, EventManager } from "../../models/EventManager"
import { ResourceManager } from "../../models/ResourceManager"
import { StorageManager } from "../../models/StorageManager" import { StorageManager } from "../../models/StorageManager"
import { UIManager } from "../../models/UIManager" import { UIManager } from "../../models/UIManager"
import { Utility_ConvertSecondToString } from "../../utils/utility" import { Utility_ConvertSecondToString } from "../../utils/utility"
@@ -188,14 +189,14 @@ export class DailyChallenge extends Laya.Script {
const nowMonth = now.getMonth() + 1 const nowMonth = now.getMonth() + 1
const nowDay = new Date().getDate() 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)) { 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_light.visible = true
this.trophy_icon.gray = false this.trophy_icon.gray = false
} else { } 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_light.visible = false
this.trophy_icon.gray = true this.trophy_icon.gray = true
} }

View File

@@ -1,6 +1,7 @@
import type { TrophyRecord } from "../../types/global" import type { TrophyRecord } from "../../types/global"
import { config } from "../../constants/config" import { config } from "../../constants/config"
import { MONTH_LIST } from "../../models/DCManager" import { MONTH_LIST } from "../../models/DCManager"
import { ResourceManager } from "../../models/ResourceManager"
const { regClass, property } = Laya const { regClass, property } = Laya
@@ -21,7 +22,7 @@ export class TrophyClaim extends Laya.Script {
for (let i = 0; i < MONTH_LIST.length; i++) { for (let i = 0; i < MONTH_LIST.length; i++) {
const obj = MONTH_LIST[i] const obj = MONTH_LIST[i]
if (obj.year === record.year && obj.month === record.month) { if (obj.year === record.year && obj.month === record.month) {
this.icon.skin = obj.icon_res ResourceManager.getInstance().loadTexture(obj.icon_res, this.icon)
break break
} }
} }

View File

@@ -1,6 +1,7 @@
import type { TrophyRecord } from "../../types/global" import type { TrophyRecord } from "../../types/global"
import { config } from "../../constants/config" import { config } from "../../constants/config"
import { MONTH_LIST } from "../../models/DCManager" import { MONTH_LIST } from "../../models/DCManager"
import { ResourceManager } from "../../models/ResourceManager"
const { regClass, property } = Laya const { regClass, property } = Laya
@@ -21,7 +22,7 @@ export class TrophyShow extends Laya.Script {
for (let i = 0; i < MONTH_LIST.length; i++) { for (let i = 0; i < MONTH_LIST.length; i++) {
const obj = MONTH_LIST[i] const obj = MONTH_LIST[i]
if (obj.year === record.year && obj.month === record.month) { if (obj.year === record.year && obj.month === record.month) {
this.icon.skin = obj.icon_res ResourceManager.getInstance().loadTexture(obj.icon_res, this.icon)
break break
} }
} }

View File

@@ -1,5 +1,6 @@
import { config } from "../../constants/config" import { config } from "../../constants/config"
import { respath } from "../../constants/respath" import { respath } from "../../constants/respath"
import { ResourceManager } from "../../models/ResourceManager"
import { StorageManager } from "../../models/StorageManager" import { StorageManager } from "../../models/StorageManager"
import { UIManager } from "../../models/UIManager" import { UIManager } from "../../models/UIManager"
@@ -24,15 +25,15 @@ export class TrophyUnit extends Laya.Script {
public onInit(info: any): void { public onInit(info: any): void {
const user = StorageManager.getInstance().getUser() const user = StorageManager.getInstance().getUser()
const starCount = user.get_doneCountByDate(info.year, info.month) 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_count.text = `${starCount}/${info.dayCount}`
this.label_month.text = config.MONTH_ABBRS[info.month - 1] this.label_month.text = config.MONTH_ABBRS[info.month - 1]
if (user.get_trophyRecord(info.year, info.month)) { 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.icon.gray = false
this.label_month.color = "#2d3138" this.label_month.color = "#2d3138"
} else { } 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.icon.gray = true
this.label_month.color = "#a7abb3" this.label_month.color = "#a7abb3"
} }