PrefabManager.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const DWTool = require("./DWTool");
  2. var Promise = require('../lib/es6-promise').Promise;
  3. class PrefabManager {
  4. static getPrefabByPath(path, res) {
  5. let filePath = `prefabs/precedence/${path}`;
  6. let uuid = cc.loader._getResUuid(filePath, cc.Prefab, true);
  7. let result = res.filter((item) => {
  8. return item._uuid === uuid
  9. });
  10. if (result != null && result.length != undefined) {
  11. let prefab = result[0];
  12. if (prefab) {
  13. return new cc.instantiate(prefab);
  14. } else {
  15. return null;
  16. }
  17. }
  18. return null;
  19. }
  20. //prefab预制资源
  21. static loadPrefab(name) {
  22. if (GameGlobal.prefabsRes) {
  23. // let prefab = this.getPrefabByPath(name,GameGlobal.prefabsRes);
  24. // sprite.spriteFrame = prefab;
  25. let p = new Promise((resolve, reject) => {
  26. let prefab = this.getPrefabByPath(name,GameGlobal.prefabsRes);
  27. if (prefab) {
  28. resolve(prefab)
  29. } else {
  30. DWTool.loadResPrefab(`./prefabs/precedence/${name}`)
  31. .then((result) => {
  32. let alert = cc.instantiate(result);
  33. resolve(alert);
  34. });
  35. }
  36. });
  37. return p;
  38. } else {
  39. let p = new Promise((resolve, reject) => {
  40. DWTool.loadResPrefab(`./prefabs/precedence/${name}`)
  41. .then((result) => {
  42. let alert = cc.instantiate(result);
  43. resolve(alert);
  44. });
  45. })
  46. return p;
  47. }
  48. }
  49. }
  50. module.exports = PrefabManager;