123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- import common from "../common/common";
- class Loader {
- private static _instance: Loader;
- public static getInstance(): Loader {
- if (this._instance) {
- return this._instance;
- }
- this._instance = new Loader();
- return this._instance;
- };
- /**
- * 加载bundle资源
- * @param bundleName
- */
- loadBundle(bundleName: string): Promise<cc.AssetManager.Bundle> {
- return new Promise((resolve) => {
- cc.assetManager.loadBundle(bundleName, null, (err, asset: cc.AssetManager.Bundle) => {
- if (err) {
- common.log("加载bundle失败:", bundleName);
- resolve(null);
- }
- else {
- common.log("加载bundle:", bundleName);
- resolve(asset);
- }
- });
- });
- }
- loadResLoacl(path): Promise<any>{
- return new Promise((resolve) => {
- cc.resources.load(path, function (err, asset: any) {
- if (err) {
- common.log("加载失败:", path);
- resolve(null);
- }
- else {
- common.log("加载资源:", path);
- resolve(asset);
- }
- });
- });
- }
- /**
- * 加载资源:bundle内
- * @param type
- * @param bundle
- * @param path
- */
- loadBundleRes(type, bundle, path) {
- switch (type) {
- case "img":
- return this.loadBundleImg(bundle, path);
- case "audio":
- return this.loadBundleAudio(bundle, path);
- case "json":
- return this.loadBundleJson(bundle, path);
- case "font":
- return this.loadBundleFont(bundle, path);
- case "spine":
- return this.loadBundleSpine(bundle, path);
- case "prefab":
- return this.loadBundlePrefab(bundle, path);
- default:
- break;
- }
- }
- /**
- * 加载图片资源:bundle内
- * @param bundle
- * @param path
- * @param callBack
- */
- loadBundleImg(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Texture2D> {
- return new Promise((resolve) => {
- bundle.load(path, cc.Texture2D, (err, asset: cc.Texture2D) => {
- if (err) {
- common.log("加载图片失败:", path);
- }
- else {
- common.log("加载图片:", path);
- resolve(asset);
- }
- });
- })
- }
- /**
- * 加载音频资源:bundle内
- * @param bundle
- * @param path
- */
- loadBundleAudio(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.AudioClip> {
- return new Promise((resolve) => {
- bundle.load(path, cc.AudioClip, (err, asset: cc.AudioClip) => {
- if (err) {
- common.log("加载音频失败:", path);
- }
- else {
- common.log("加载音频:", path);
- resolve(asset);
- }
- });
- });
- }
- /**
- * 加载json资源:bundle内
- * @param bundle
- * @param path
- */
- loadBundleJson(bundle: cc.AssetManager.Bundle, path): Promise<cc.JsonAsset> {
- return new Promise((resolve) => {
- bundle.load(path, cc.JsonAsset, (err, asset: cc.JsonAsset) => {
- if (err) {
- common.log("加载json失败:", path);
- }
- else {
- common.log("加载json:", path);
- resolve(asset);
- }
- });
- });
- }
- /**
- * 加载字体资源:bundle内
- * @param bundle
- * @param path
- */
- loadBundleFont(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Font> {
- return new Promise((resolve) => {
- bundle.load(path, cc.Font, (err, asset: cc.Font) => {
- if (err) {
- common.log("加载字体失败: ", path);
- }
- else {
- common.log("加载字体: ", path);
- resolve(asset);
- }
- });
- });
- }
- /**
- * 加载spine资源:bundle内
- * @param bundle
- * @param path
- */
- loadBundleSpine(bundle: cc.AssetManager.Bundle, path: string): Promise<sp.SkeletonData> {
- return new Promise((resolve) => {
- bundle.load(path, sp.SkeletonData, (err, asset: sp.SkeletonData) => {
- if (err) {
- common.log("加载spine失败: ", path);
- }
- else {
- common.log("加载spine: ", path);
- resolve(asset);
- }
- });
- });
- }
- /**
- * 加载prefab资源:bundle内
- * @param bundle
- * @param path
- */
- loadBundlePrefab(bundle: cc.AssetManager.Bundle, path): Promise<cc.Prefab> {
- return new Promise((resolve) => {
- bundle.load(path, cc.Prefab, (err, asset: cc.Prefab) => {
- if (err) {
- common.log("加载prefab失败: ", path);
- }
- else {
- common.log("加载prefab: ", path);
- resolve(asset);
- }
- });
- });
- }
- /**
- * 加载图片资源:bundle内
- * @param bundle
- * @param path
- * @param callBack
- */
- loadBundleDir(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Asset[]> {
- return new Promise((resolve) => {
- bundle.loadDir(path, cc.Asset, (err, assets: cc.Asset[]) => {
- if (err) {
- common.log("加载图片失败:", path);
- }
- else {
- common.log("加载图片:", path);
- resolve(assets);
- }
- });
- })
- }
- /**
- * 加载其他资源:bundle内
- * @param bundle
- * @param path
- * @param callBack
- */
- loadBundleOther(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Asset> {
- return new Promise((resolve) => {
- bundle.load(path, cc.Asset, (err, assets: cc.Asset) => {
- if (err) {
- common.log("加载资源失败:", path);
- }
- else {
- common.log("加载资源:", path);
- resolve(assets);
- }
- });
- })
- }
- /**
- * 加载资源:远程加载
- * @param type
- * @param url
- */
- loadRemote(type, url) {
- switch (type) {
- case "img":
- return this.loadRemoteImg(url);
- case "mp3":
- return this.loadRemoteAudio(url);
- default:
- break;
- }
- };
- /**
- * 加载图片资源:远程加载
- * @param url
- */
- loadRemoteImg(url) {
- return new Promise((resolve) => {
- cc.assetManager.loadRemote(url, (err: Error, asset: cc.Texture2D) => {
- if (err) {
- common.log("加载图片失败:", url);
- }
- else {
- common.log("加载图片:", url);
- resolve(asset);
- }
- });
- });
- };
- /**
- * 加载音频资源:远程加载
- * @param url
- */
- loadRemoteAudio(url) {
- return new Promise((resolve) => {
- cc.assetManager.loadRemote(url, (err: Error, asset: cc.AudioClip) => {
- if (err) {
- common.log("加载音频失败:", url);
- }
- else {
- common.log("加载音频:", url);
- resolve(asset);
- }
- });
- });
- };
- /**
- * 加载spine资源:远程加载
- * @param key
- * @param url
- * @param obj
- * @param callBack
- */
- loadRemoteSpine(key, url, obj, callBack) {
- let functionA = (texture, skeAtlas, skeJson) => {
- let skeletonData = new sp.SkeletonData();
- skeletonData.skeletonJson = skeJson.json;
- skeletonData.atlasText = skeAtlas.text;
- skeletonData.textures = [texture];
- if (skeletonData["textureNames"]) {
- skeletonData["textureNames"] = [key + ".png"];
- }
- obj[url] = skeletonData;
- common.log("加载spine:", url);
- callBack();
- };
- cc.assetManager.loadRemote(url + ".png", (err: Error, texture: cc.Texture2D) => {
- cc.assetManager.loadRemote(url + ".atlas", (error: Error, skeAtlas: cc.TextAsset) => {
- cc.assetManager.loadRemote(url + ".json", (error: Error, skeJson: cc.JsonAsset) => {
- functionA(texture, skeAtlas, skeJson);
- });
- });
- });
- };
- };
- export default Loader.getInstance();
|