123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import EventManager from "../Event/EventManager";
- import { EventName } from "../Event/EventName";
- import Tool from "../Tool/Tool";
- /**
- * 预制体资源 - 数据层
- */
- export default class PrefabResources {
- /**资源列表 */
- private static resources: Map<string, any> = new Map<string, any>();
- /**资源包 */
- private static bundle: cc.AssetManager.Bundle = null;
- /**加载预制体资源 */
- public static loadPrefabResources (prefabRes: Array<string>, denominator: number, currentProgress: number, callback: Function) {
- Tool.log("加载预制体资源");
- let complete = () => {
- Tool.log(this.resources);
- Tool.log("加载完毕");
- Tool.log("");
- if (callback == null) {
- // 发送初始加载完成 本地消息
- EventManager.sendEvent(EventName.Res.Res_LoadComplete);
- }
- else {
- callback();
- }
- }
- if (prefabRes.length == 0) {
- complete();
- }
- else {
- cc.assetManager.loadBundle("res/prefab", (err: Error, prefab: cc.AssetManager.Bundle) => {
- this.bundle = prefab;
- this.bundle.load(prefabRes, cc.Asset, (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
- // 发送初始加载进度 本地消息
- EventManager.sendEvent(EventName.Res.Res_LoadProgress, (finish/total)*denominator + currentProgress);
- }, (err, assets: Array<cc.Asset>) => {
- Tool.log(err);
-
- for (let index in assets) {
- this.resources.set(assets[index].name, assets[index]);
- }
-
- complete();
- });
- });
- }
- }
- /**获得预制体资源 */
- public static getResources (name: string): any {
- let _name: string = name;
- if (name.includes("/") == true) {
- _name = name.substring(name.indexOf("/")+1);
- }
- if (this.resources.get(_name) != null) {
- return this.resources.get(_name);
- }
- else {
- return this.bundle.get(_name);
- }
- }
- }
|