| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import { _decorator, Component, __private, AssetManager, resources, Asset, assetManager, SceneAsset } from "cc";
- const { ccclass, property } = _decorator;
- /**
- * 资源加载组件
- * @author 袁浩
- */
- @ccclass('ResourceLoader')
- export class ResourceLoader extends Component {
- /**
- * 加载本地资源,使用说明见resources.load
- * @returns await 加载成功后返回的数据
- */
- public load<T extends Asset>(paths: string[] | string, type: __private.cocos_core_asset_manager_shared_AssetType, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
- return new Promise<T>((resolve: (data: T) => void, reject) => {
- resources.load(paths as any, type, onProgress, (err: Error = null, data: T): void => {
- if (!err) {
- if (!this.node || !this.node.isValid) {
- return;
- }
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 加载远程资源,使用说明见assetManager.loadRemote
- * @returns await 加载成功后返回的数据
- */
- public loadRemote<T extends Asset>(url: string, options: __private.cocos_core_asset_manager_shared_IRemoteOptions, onComplete?: __private.cocos_core_asset_manager_shared_CompleteCallbackWithData<T> | null) {
- return new Promise<T>((resolve: (data: T) => void, reject) => {
- assetManager.loadRemote(url, options, (err: Error = null, data: T): void => {
- if (!err) {
- if (!this.node.isValid) {
- return;
- }
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 使用说明见resources.preload
- * @returns await 加载成功后返回的数据
- */
- public preload(paths: string | any, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
- return new Promise<AssetManager.RequestItem[]>((resolve: (data: AssetManager.RequestItem[]) => void, reject) => {
- resources.preload(paths, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => {
- if (!err) {
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 使用说明见resources.loadDir
- * @returns await 加载成功后返回的数据
- */
- public loadDir<T extends Asset>(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType<T> = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
- return new Promise<T>((resolve: (data: T) => void, reject) => {
- resources.loadDir(dir, type, onProgress, (err: Error = null, data: T | any): void => {
- if (!err) {
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 使用说明见resources.preloadDir
- * @returns await 加载成功后返回的数据
- */
- public preloadDir(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
- return new Promise<AssetManager.RequestItem[]>((resolve: (data: AssetManager.RequestItem[]) => void, reject) => {
- resources.preloadDir(dir, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => {
- if (!err) {
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 使用说明见resources.loadScene
- * @returns await 加载成功后返回的数据
- */
- public loadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
- return new Promise<any>((resolve: (data: any) => void, reject) => {
- resources.loadScene(sceneName, options, onProgress, (err: Error = null, data: SceneAsset): void => {
- if (!err) {
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 使用说明见resources.preloadScene
- * @returns await 加载成功后返回的数据
- */
- public preloadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback) {
- return new Promise<void>((resolve: () => void, reject) => {
- resources.preloadScene(sceneName, options, onProgress, (err: Error = null): void => {
- if (!err) {
- resolve();
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 加载资源包
- * @param nameOrUrl 资源包名字或者地址
- * @returns 资源包对象
- */
- public loadBundle(nameOrUrl: string) {
- return new Promise<AssetManager.Bundle>((resolve: (data: AssetManager.Bundle) => void, reject) => {
- assetManager.loadBundle(nameOrUrl, (err: Error | null, data: AssetManager.Bundle) => {
- if (!err) {
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- /**
- * 加载资源包中的资源
- * @param bundle
- * @param paths
- * @param type
- * @param onProgress
- * @returns
- */
- public loadInBundle<T extends Asset>(bundle: AssetManager.Bundle, paths: string[] | string, type: __private.cocos_core_asset_manager_shared_AssetType, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
- return new Promise<T>((resolve: (data: T) => void, reject) => {
- bundle.load(paths as any, type, onProgress, (err: Error = null, data: T) => {
- if (!err) {
- resolve(data);
- }
- else {
- reject(err);
- }
- });
- });
- }
- }
|