Browse Source

ResourcesMgr-async-await

0224995 1 month ago
parent
commit
50d4552d65
38 changed files with 197 additions and 2 deletions
  1. 9 0
      assets/Script/Frames.meta
  2. 7 0
      assets/Script/Frames/DataManager.ts
  3. 9 0
      assets/Script/Frames/DataManager.ts.meta
  4. 53 0
      assets/Script/Frames/ResourcesMgr.ts
  5. 9 0
      assets/Script/Frames/ResourcesMgr.ts.meta
  6. 3 0
      assets/Script/Game/GameFrameWork/MonsterMgr.ts
  7. 2 0
      assets/Script/Game/GameFrameWork/TowerMgr.ts
  8. 59 0
      assets/Script/Game/GameResMgr.ts
  9. 9 0
      assets/Script/Game/GameResMgr.ts.meta
  10. 1 1
      assets/Script/SelectLevel.ts
  11. 2 0
      assets/Script/SelectScene.ts
  12. 4 1
      assets/Script/StartScene.ts
  13. 9 0
      assets/resources/Data.meta
  14. 1 0
      assets/resources/Data/GameResPath.csv
  15. 11 0
      assets/resources/Data/GameResPath.csv.meta
  16. 9 0
      assets/resources/Res.meta
  17. 0 0
      assets/resources/Res/Prefab.meta
  18. 0 0
      assets/resources/Res/Prefab/Monster.prefab
  19. 0 0
      assets/resources/Res/Prefab/Monster.prefab.meta
  20. 0 0
      assets/resources/Res/Prefab/Tower.prefab
  21. 0 0
      assets/resources/Res/Prefab/Tower.prefab.meta
  22. 0 0
      assets/resources/Res/ThemeMap.meta
  23. 0 0
      assets/resources/Res/ThemeMap/Theme01.plist
  24. 0 0
      assets/resources/Res/ThemeMap/Theme01.plist.meta
  25. 0 0
      assets/resources/Res/ThemeMap/Theme01.png
  26. 0 0
      assets/resources/Res/ThemeMap/Theme01.png.meta
  27. 0 0
      assets/resources/Res/ThemeMap/Theme02.plist
  28. 0 0
      assets/resources/Res/ThemeMap/Theme02.plist.meta
  29. 0 0
      assets/resources/Res/ThemeMap/Theme02.png
  30. 0 0
      assets/resources/Res/ThemeMap/Theme02.png.meta
  31. 0 0
      assets/resources/Res/ThemeMap/Theme03.plist
  32. 0 0
      assets/resources/Res/ThemeMap/Theme03.plist.meta
  33. 0 0
      assets/resources/Res/ThemeMap/Theme03.png
  34. 0 0
      assets/resources/Res/ThemeMap/Theme03.png.meta
  35. 0 0
      assets/resources/Res/number.plist
  36. 0 0
      assets/resources/Res/number.plist.meta
  37. 0 0
      assets/resources/Res/number.png
  38. 0 0
      assets/resources/Res/number.png.meta

+ 9 - 0
assets/Script/Frames.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "1818b6c8-e053-4113-894a-a0ea9b325bdb",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 7 - 0
assets/Script/Frames/DataManager.ts

@@ -0,0 +1,7 @@
+class DataManager{
+    static instance: DataManager = null;
+
+}
+
+export const dataMgr: DataManager = DataManager.instance = new DataManager();
+

+ 9 - 0
assets/Script/Frames/DataManager.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "c4df3d32-8d4f-4ff5-a784-759683731d52",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 53 - 0
assets/Script/Frames/ResourcesMgr.ts

@@ -0,0 +1,53 @@
+import { Asset, AssetManager, Prefab, resources, SpriteFrame, TiledMap, TiledMapAsset } from "cc";
+
+class ResourcesMgr{
+
+    static instance: ResourcesMgr = null;
+
+    private _spriteFrame: Map<string,SpriteFrame> = new Map();
+    private _prefab: Map<string, Prefab> = new Map();
+    private _tmxAsset: Map<number,TiledMapAsset> = new Map();
+    async loadAllRes(path: string, onProgress:((finish: number,total: number, item: AssetManager.RequestItem)=>void) = null){
+        //下载resources文件夹下的所有资源
+        //异步函数
+        const assets: Asset[] = await new Promise<Asset[]>((value, reject)=>{
+            resources.loadDir(path, onProgress, (err:Error,assets: Asset[])=>{
+                //完成回调 assets就是那些所有资源的数组
+                if(err){
+                    reject(err);
+                }
+                else{
+                    value(assets);
+                }
+                
+            })
+        })
+       //遍历所有资源 分门别类存储到对应的容器中
+       for(const asset of assets){
+    
+       }
+    }
+
+    addRes(name: string | string, asset: Asset){
+        if(asset instanceof SpriteFrame){
+            this._spriteFrame.set(String(name), asset);
+        }
+        if(asset instanceof Prefab){
+            this._prefab.set(String(name), asset);
+        }
+        if(asset instanceof TiledMapAsset){
+            this._tmxAsset.set(Number(name), asset)
+        }
+    }
+
+    getSpriteFrame(name: string): SpriteFrame{
+        return this._spriteFrame.get(name);
+    }
+
+    getPrefab(name: string): Prefab{
+        return this._prefab.get(name);
+    }
+}
+
+export const resMgr: ResourcesMgr = ResourcesMgr.instance = new ResourcesMgr();
+

+ 9 - 0
assets/Script/Frames/ResourcesMgr.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "dbbec8a5-02f4-4f50-826d-498d6a848166",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 3 - 0
assets/Script/Game/GameFrameWork/MonsterMgr.ts

@@ -1,4 +1,6 @@
 import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
+import { resMgr } from '../../Frames/ResourcesMgr';
+import { ModulerBase } from './ModulerBase';
 const { ccclass, property } = _decorator;
 
 @ccclass('MonsterMgr')
@@ -6,6 +8,7 @@ export class MonsterMgr extends Component {
     @property(Prefab)
     monsterPre: Prefab = null;
     start() {
+        //this.monsterPre = resMgr.getPrefab("Monster");
         this.schedule(()=>{
             const monster = instantiate(this.monsterPre);
             monster.parent = this.node;

+ 2 - 0
assets/Script/Game/GameFrameWork/TowerMgr.ts

@@ -2,6 +2,7 @@ import { _decorator, instantiate, Prefab, Vec3 } from 'cc';
 import { ModulerBase } from './ModulerBase';
 import { GameMgr } from './GameMgr';
 import { MapMgr } from './MapMgr';
+import { resMgr } from '../../Frames/ResourcesMgr';
 const { ccclass, property } = _decorator;
 
 @ccclass('TowerMgr')
@@ -14,6 +15,7 @@ export class TowerMgr extends ModulerBase {
     }
     creatTower(pos: Vec3){
         const node = instantiate(this.pre);
+        //const node = instantiate(resMgr.getPrefab("Tower"));
         node.setPosition(pos);
         node.parent = this.node;
     }

+ 59 - 0
assets/Script/Game/GameResMgr.ts

@@ -0,0 +1,59 @@
+const ResPath = [
+    {
+        bg:[
+            "Theme/Theme1/BG0/BG1-hd.pvr.ccz/spriteFrame",
+            "Theme/Theme1/BG0/BG2-hd.pvr.ccz/spriteFrame",
+        ],
+        monster:[
+            "Theme/Theme1/Items/Monsters01-hd",
+            "Theme/Theme1/Items/Monsters02-hd",
+        ],
+        obs:[
+            "Theme/Theme1/Items/Object01-hd",
+            "Theme/Theme1/Items/Object02-hd",
+        ],
+        pathImg:"Theme/Theme1/BGlv/BG-hd.pvr.ccz/spriteFrame",
+        tmx:"Theme/Theme1/BG1lv/BGPath"
+        
+    },
+    {
+        bg:[
+            "Theme/Theme2/BG0/BG1-hd.pvr.ccz/spriteFrame",
+            "Theme/Theme2/BG0/BG2-hd.pvr.ccz/spriteFrame",
+        ],
+        monster:[
+            "Theme/Theme2/Items/Monsters01-hd",
+            "Theme/Theme2/Items/Monsters02-hd",
+        ],
+        obs:[
+            "Theme/Theme2/Items/Object01-hd",
+            "Theme/Theme2/Items/Object02-hd",
+        ],
+        pathImg:"Theme/Theme2/BGlv/BG-hd.pvr.ccz/spriteFrame",
+        tmx:"Theme/Theme2/BG1lv/BGPath"
+    },
+    {
+        bg:[
+            "Theme/Theme2/BG0/BG1-hd.pvr.ccz/spriteFrame",
+            "Theme/Theme2/BG0/BG2-hd.pvr.ccz/spriteFrame",
+        ],
+        monster:[
+            "Theme/Theme2/Items/Monsters01-hd",
+            "Theme/Theme2/Items/Monsters02-hd",
+        ],
+        obs:[
+            "Theme/Theme2/Items/Object01-hd",
+            "Theme/Theme2/Items/Object02-hd",
+        ],
+        pathImg:"Theme/Theme2/BGlv/BG-hd.pvr.ccz/spriteFrame",
+        tmx:"Theme/Theme2/BG1lv/BGPath"
+    },
+]
+
+class GameResMgr{
+    static instance: GameResMgr = null;
+
+}
+
+export const gameResMgr: GameResMgr = GameResMgr.instance = new GameResMgr();
+

+ 9 - 0
assets/Script/Game/GameResMgr.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "b9d42472-1fa6-4f18-9c71-8f5f1e6e83e6",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 1 - 1
assets/Script/SelectLevel.ts

@@ -75,7 +75,7 @@ export class SelectLevel extends Component {
     //资源导入
     private _loadLvRes(){
         const theme = GameInfo.Instance.CurTheme;
-        resources.load("ThemeMap/Theme0" + theme ,SpriteAtlas,(error:Error | null,asset:SpriteAtlas)=>{
+        resources.load("Res/ThemeMap/Theme0" + theme ,SpriteAtlas,(error:Error | null,asset:SpriteAtlas)=>{
             if(error){
                 console.error("No found Theme0" + theme);
             } else {

+ 2 - 0
assets/Script/SelectScene.ts

@@ -1,5 +1,6 @@
 import { _decorator, Component, Node ,Button,director, PageView, NodeEventType, Prefab, instantiate, Animation, Vec3} from 'cc';
 import { GameInfo } from './GameInfo';
+import { resMgr } from './Frames/ResourcesMgr';
 const { ccclass, property } = _decorator;
 
 @ccclass('SelectScene')
@@ -26,6 +27,7 @@ export class SelectScene extends Component {
     btnLeft: Node = null;
 
     start() {
+        console.log(resMgr)
          //总页数
          this.totalPages = this.pageView.getComponent(PageView).getPages().length;
          //当前索引值

+ 4 - 1
assets/Script/StartScene.ts

@@ -1,4 +1,5 @@
 import { _decorator, Button, Component, Node,director ,tween, Vec3, Vec2, UIOpacity, Animation,} from 'cc';
+import { resMgr } from './Frames/ResourcesMgr';
 const { ccclass, property } = _decorator;
 
 @ccclass('StartScene')
@@ -28,7 +29,9 @@ export class NewComponent extends Component {
         this._loadingPic = this.node.getChildByPath("Logo/LoadingBg");
         
     }
-    start() {
+    async start() {
+        await resMgr.loadAllRes("Res");
+
         this._logo();
 
         this._btnLogin.on(Button.EventType.CLICK,()=>{

+ 9 - 0
assets/resources/Data.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "8bb853a6-67a5-4e78-82a9-744756d625d9",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 1 - 0
assets/resources/Data/GameResPath.csv

@@ -0,0 +1 @@
+

+ 11 - 0
assets/resources/Data/GameResPath.csv.meta

@@ -0,0 +1,11 @@
+{
+  "ver": "1.0.2",
+  "importer": "text",
+  "imported": true,
+  "uuid": "cd73799d-ad57-4a1a-ac62-d70acbe94aba",
+  "files": [
+    ".json"
+  ],
+  "subMetas": {},
+  "userData": {}
+}

+ 9 - 0
assets/resources/Res.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "5f4f5651-f506-42dc-a4c4-3797c1056a90",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 0 - 0
assets/resources/Prefab.meta → assets/resources/Res/Prefab.meta


+ 0 - 0
assets/resources/Prefab/Monster.prefab → assets/resources/Res/Prefab/Monster.prefab


+ 0 - 0
assets/resources/Prefab/Monster.prefab.meta → assets/resources/Res/Prefab/Monster.prefab.meta


+ 0 - 0
assets/resources/Prefab/Tower.prefab → assets/resources/Res/Prefab/Tower.prefab


+ 0 - 0
assets/resources/Prefab/Tower.prefab.meta → assets/resources/Res/Prefab/Tower.prefab.meta


+ 0 - 0
assets/resources/ThemeMap.meta → assets/resources/Res/ThemeMap.meta


+ 0 - 0
assets/resources/ThemeMap/Theme01.plist → assets/resources/Res/ThemeMap/Theme01.plist


+ 0 - 0
assets/resources/ThemeMap/Theme01.plist.meta → assets/resources/Res/ThemeMap/Theme01.plist.meta


+ 0 - 0
assets/resources/ThemeMap/Theme01.png → assets/resources/Res/ThemeMap/Theme01.png


+ 0 - 0
assets/resources/ThemeMap/Theme01.png.meta → assets/resources/Res/ThemeMap/Theme01.png.meta


+ 0 - 0
assets/resources/ThemeMap/Theme02.plist → assets/resources/Res/ThemeMap/Theme02.plist


+ 0 - 0
assets/resources/ThemeMap/Theme02.plist.meta → assets/resources/Res/ThemeMap/Theme02.plist.meta


+ 0 - 0
assets/resources/ThemeMap/Theme02.png → assets/resources/Res/ThemeMap/Theme02.png


+ 0 - 0
assets/resources/ThemeMap/Theme02.png.meta → assets/resources/Res/ThemeMap/Theme02.png.meta


+ 0 - 0
assets/resources/ThemeMap/Theme03.plist → assets/resources/Res/ThemeMap/Theme03.plist


+ 0 - 0
assets/resources/ThemeMap/Theme03.plist.meta → assets/resources/Res/ThemeMap/Theme03.plist.meta


+ 0 - 0
assets/resources/ThemeMap/Theme03.png → assets/resources/Res/ThemeMap/Theme03.png


+ 0 - 0
assets/resources/ThemeMap/Theme03.png.meta → assets/resources/Res/ThemeMap/Theme03.png.meta


+ 0 - 0
assets/resources/number.plist → assets/resources/Res/number.plist


+ 0 - 0
assets/resources/number.plist.meta → assets/resources/Res/number.plist.meta


+ 0 - 0
assets/resources/number.png → assets/resources/Res/number.png


+ 0 - 0
assets/resources/number.png.meta → assets/resources/Res/number.png.meta