resMgr.ts 897 B

1234567891011121314151617181920212223242526272829
  1. export class ResMgr {
  2. private static _inst: ResMgr;
  3. public static get inst(): ResMgr {
  4. if (this._inst == null) {
  5. this._inst = new ResMgr();
  6. }
  7. return this._inst;
  8. }
  9. // 跟随节点释放
  10. loadPrefab(name: string, parent: cc.Node): Promise<void>{
  11. return new Promise((resolve)=>{
  12. cc.resources.load(name, cc.Prefab, (err, res) => {
  13. if (res) {
  14. let node = cc.instantiate(res);
  15. parent.addChild(node);
  16. res.addRef();
  17. parent.on(cc.Node.EventType.CHILD_REMOVED, (child: cc.Node)=>{
  18. if(child.uuid == node.uuid) {
  19. res.decRef();
  20. }
  21. });
  22. }
  23. resolve();
  24. });
  25. });
  26. }
  27. }