37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import { Prefab, resources } from 'cc';
|
|
import { GameConstants } from '../common/Constants';
|
|
|
|
export class SpineManager {
|
|
private _cache: Map<string, Prefab> = new Map();
|
|
|
|
async load(spinePath: string): Promise<Prefab> {
|
|
if (this._cache.has(spinePath)) {
|
|
return this._cache.get(spinePath)!;
|
|
}
|
|
|
|
const prefab = await this._load(spinePath);
|
|
this._cache.set(spinePath, prefab);
|
|
return prefab;
|
|
}
|
|
|
|
private _load(path: string): Promise<Prefab> {
|
|
return new Promise((resolve, reject) => {
|
|
resources.load(path, Prefab, (err, prefab) => {
|
|
err ? reject(err) : resolve(prefab);
|
|
});
|
|
});
|
|
}
|
|
|
|
release(spinePath: string) {
|
|
if (this._cache.has(spinePath)) {
|
|
resources.release(spinePath);
|
|
this._cache.delete(spinePath);
|
|
}
|
|
}
|
|
|
|
// 加载英雄资源
|
|
async loadHero(name: string): Promise<Prefab> {
|
|
const path = `${GameConstants.SPINE.HERO}/${name}`;
|
|
return this.load(path);
|
|
}
|
|
}
|