| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import { _decorator, Component, Node, Prefab, Material, assetManager, AudioClip } from 'cc';
- const { ccclass, property } = _decorator;
- import { config, IdToName } from './CubeConfig';
- const m_bundle = [
- { name: 'CubePrefab', url: 'Cube', type: 'Prefab', variable: 'cubePrefab' },
- { name: 'Materials', url: '', type: 'Material', variable: 'materials' },
- { name: 'GUI', url: 'MainCanvas', type: 'Prefab', variable: 'mainCanvas' },
- { name: 'GUI', url: 'UI_Show', type: 'Prefab', variable: 'ui_Show' },
- { name: 'GUI', url: 'SettingsPanel', type: 'Prefab', variable: 'settingsPanel' },
- { name: 'GUI', url: 'RankPanel', type: 'Prefab', variable: 'rankPanel' },
- { name: 'GUI', url: 'Item', type: 'Prefab', variable: 'item' },
- { name: 'GUI', url: 'InforPanel', type: 'Prefab', variable: 'inforPanel' },
- { name: 'GUI', url: 'AdPanel', type: 'Prefab', variable: 'adPanel' },
- { name: 'GUI', url: 'ResultPanel', type: 'Prefab', variable: 'resultPanel' },
- { name: 'GUI', url: 'SignPanel', type: 'Prefab', variable: 'signPanel' },
- { name: 'Container', url: 'Container', type: 'Prefab', variable: 'container' },
- { name: 'Audio', url: '', type: 'AudioClip', variable: '' },
- ]
- @ccclass('LoadRes')
- export class LoadRes {
- private static instance: LoadRes = null;
- private resourcesLoadedCount: number = 0;
- private onResourceLoaded: () => void = null;
- @property(Prefab)
- public cubePrefab: Prefab = null;// 方块
- @property(Material)
- public materials: Material[] = [];// 材质
- @property(Prefab)
- public container: Prefab = null;// 容器
- @property(Prefab)
- public mainCanvas: Prefab = null;// 主界面
- @property(Prefab)
- public settingsPanel: Prefab = null;// 设置界面
- @property(Prefab)
- public rankPanel: Prefab = null;// 排行榜界面
- @property(Prefab)
- public inforPanel: Prefab = null;// 个人信息界面
- @property(Prefab)
- public ui_Show: Prefab = null;// UI界面
- @property(Prefab)
- public adPanel: Prefab = null;//广告界面
- @property(Prefab)
- public resultPanel: Prefab = null;//结算界面
- @property(Prefab)
- public signPanel: Prefab = null;
- @property(Prefab)
- public item: Prefab = null;
- private constructor() {
- // 私有化构造函数以防止外部实例化
- }
- public static getInstance(): LoadRes {
- if (LoadRes.instance === null) {
- LoadRes.instance = new LoadRes();
- }
- return LoadRes.instance;
- }
- setOnResourceLoaded(callback: () => void) {
- this.onResourceLoaded = callback;
- }
- StartLoadRes() {
- for (let i = 0; i < m_bundle.length; i++) {
- if (m_bundle[i].type === 'Prefab') {
- this.preloadPrefab(i);
- }
- else if (m_bundle[i].type === 'Material') {
- this.preloadMaterial(i);
- }
- else if (m_bundle[i].type === 'AudioClip') {
- this.preloadAudio(i);
- }
- else if (m_bundle[i].type === 'Font') {
- this.preloadFont(i);
- }
- }
- }
- update(deltaTime: number) {
- }
- preloadPrefab(index: number) {
- assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
- bundle.load(m_bundle[index].url, Prefab, (err, prefab) => {
- if (err) {
- console.error(err);
- return;
- }
- this[m_bundle[index].variable] = prefab;
- if (this.onResourceLoaded) {
- this.onResourceLoaded();
- }
- this.checkAllResourcesLoaded();
- });
- });
- }
- preloadMaterial(index: number) {
- const materialPromises: Promise<void>[] = [];
- for (let i = 0; i < 5; i++) {
- materialPromises.push(new Promise<void>((resolve, reject) => {
- assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
- if (err) {
- console.error(err);
- return;
- }
- bundle.load(config[IdToName[i]].effectName, Material, (err, material) => {
- if (err) {
- console.error(err);
- reject(err);
- } else {
- this[m_bundle[index].variable][i] = material;
- resolve();
- }
- });
- });
- }));
- }
- Promise.all(materialPromises).then(() => {
- this[m_bundle[index].variable].forEach((material, index) => {
- material.initialize({
- effectName: config[IdToName[index]].effectName,
- defines: {
- USE_RGBE_CUBEMAP: true
- }
- });
- if (this.onResourceLoaded) {
- this.onResourceLoaded();
- }
- });
- this.checkAllResourcesLoaded();
- }).catch(error => {
- console.error('Error loading materials:', error);
- });
- }
- preloadAudio(index: number) {
- assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
- if (this.onResourceLoaded) {
- this.onResourceLoaded();
- }
- this.checkAllResourcesLoaded();
- });
- }
- preloadFont(index: number) {
- assetManager.loadBundle(m_bundle[index].name, (err, bundle) => {
- if (this.onResourceLoaded) {
- this.onResourceLoaded();
- }
- this.checkAllResourcesLoaded();
- });
- }
- private checkAllResourcesLoaded(): void {
- this.resourcesLoadedCount++;
- if (this.resourcesLoadedCount === 4) {
- //资源都加载好了
- }
- }
- }
|