| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import {
- _decorator,
- Asset,
- AssetManager,
- assetManager,
- Component,
- error,
- Node,
- Prefab,
- resources,
- SpriteFrame
- } from 'cc';
- const {ccclass, property} = _decorator;
- @ccclass('LoadUtils')
- export class LoadUtils {
- public static loadSpriteFrame(url: string, succ: Function, fail: Function) {
- resources.load(url, SpriteFrame, (err, asset) => {
- if (err) {
- console.log(err);
- } else {
- succ(asset)
- }
- });
- }
- public static loadPrefab(url: string, succ: Function, fail: Function) {
- resources.load(url, Prefab, (err, asset) => {
- if (err) {
- console.log(err);
- } else {
- succ(asset)
- }
- });
- }
- public static loadBundleRes(bundleName: string, url: string, call?: Function, caller?: any, assetType?: typeof Asset) {
- let tmpBundle = assetManager.getBundle(bundleName);
- if (tmpBundle) {
- tmpBundle.load(url, assetType, (err: Error, asset: any) => {
- if (err) {
- console.log("加载资源异常:", url, err);
- } else {
- if (call) call.call(caller, asset);
- }
- });
- return;
- }
- assetManager.loadBundle(bundleName, (err: Error, bundle: AssetManager.Bundle) => {
- if (bundle) {
- bundle.load(url, assetType, (err: Error, asset: any) => {
- if (!err) {
- if (call) call.call(caller, asset);
- } else {
- console.log("加载资源异常:", url, err);
- }
- })
- } else {
- console.log("加载", bundle, "分包出错", err);
- }
- })
- }
- /**从分包中加载图片 */
- public static loadBundleSpriteFrame(bundleName: string, url: string, call?: Function, caller?: any) {
- this.loadBundleRes(bundleName, url, call, caller, SpriteFrame);
- }
- /**从分包中加载预制体 */
- public static loadBundlePrefab(bundleName: string, url: string, call?: Function, caller?: any) {
- this.loadBundleRes(bundleName, url, call, caller, Prefab);
- }
- }
|