{"id":13288496,"url":"https://github.com/Mortal-Li/cocos-creator-async","last_synced_at":"2025-03-10T06:33:36.894Z","repository":{"id":214306408,"uuid":"402332756","full_name":"Mortal-Li/cocos-creator-async","owner":"Mortal-Li","description":"a light asynchronous game development framework based on cocos creator","archived":false,"fork":false,"pushed_at":"2024-02-21T04:28:51.000Z","size":1010,"stargazers_count":26,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-07-29T16:59:30.173Z","etag":null,"topics":["asyncapi","cocos-creator","cocos-creator-framework"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mortal-Li.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-09-02T07:39:09.000Z","updated_at":"2024-06-07T10:49:22.000Z","dependencies_parsed_at":"2024-01-11T13:19:28.249Z","dependency_job_id":"2774c235-bc8a-40c7-a00b-2fe5461e7479","html_url":"https://github.com/Mortal-Li/cocos-creator-async","commit_stats":null,"previous_names":["mortal-li/cocos-creator-sparrow","mortal-li/cocos-creator-async"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mortal-Li%2Fcocos-creator-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mortal-Li%2Fcocos-creator-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mortal-Li%2Fcocos-creator-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mortal-Li%2Fcocos-creator-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mortal-Li","download_url":"https://codeload.github.com/Mortal-Li/cocos-creator-async/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242805420,"owners_count":20187995,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncapi","cocos-creator","cocos-creator-framework"],"created_at":"2024-07-29T16:56:55.244Z","updated_at":"2025-03-10T06:33:36.886Z","avatar_url":"https://github.com/Mortal-Li.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"## \u003ccenter\u003eCocos-Creator-Async\u003c/center\u003e\r\n\u003ccenter\u003e\r\n\r\n![Def](https://img.shields.io/badge/cocos--creator-2.4.x-blue)\r\n![GitHub](https://img.shields.io/github/license/Mortal-Li/cocos-creator-async)\r\n![GitHub last commit](https://img.shields.io/github/last-commit/Mortal-Li/cocos-creator-async)\r\n\r\n\u003c/center\u003e\r\n\r\nCocos-Creator-Async是一个基于Cocos-creator的轻量级的、异步的、高效的游戏开发框架，它主要有以下特征：\r\n- 单场景、多Layer的UI结构，自动内存管理；\r\n- 大部分API都是异步的，代码更直观高效；\r\n- 使用引擎的Bundle思想来管理游戏代码和资源\r\n- 提供常用的功能模块和通用的解决方案（不需要的功能模块可自行删除）\r\n- 配合插件 **kk-helper** 使用，效率更高\r\n\r\n希望能用更简洁的代码提供更效率的开发体验，故起别名kk，寓意“快快”。\r\n\r\n**简要声明**：\r\n- 本项目基于 Cocos Creator 2.4.x。\r\n- 本项目使用 *MIT License* 开源协议，主要供读者学习参考。\r\n- `后期将主要修改优化Cocos Creator 3.x版本，2.x官方支持越来越少，所以本仓库也将维护越来越少`。[3.x版本](https://github.com/Mortal-Li/cocos-creator-async3)\r\n\r\n### 基本使用指南\r\n#### 1、UI结构说明\r\n\r\n本框架UI基于单场景，在单场景下，有4种类型的UI界面容器：\r\n- Layer：层，用于从一个界面切换到另一个界面，界面元素的主要载体；\r\n- Popup：弹窗，在当前界面弹出一个界面展示窗体；\r\n- Panel：面板，即可用于Layer也可用于Popup，相当于嵌入其中的子视图；\r\n- Widget：控件，主要指一些通用的小的UI模块或功能性挂件；\r\n\r\n#### 2、资源目录说明\r\n```\r\nassets\r\n├───Boot\r\n│   ├───Scripts\r\n│   │   ├───AssetConfig.ts -------\u003e 记录不同预制体的配置脚本(也可以分散在各bundle)\r\n│   └───Stage.fire ---------------\u003e 主场景\r\n├───GameABundle --------------\u003e GameA Bundle 优先级 1\r\n│   ├───Prefabs\r\n│   │   ├───Layer ------------\u003e Layer预制体\r\n│   │   ├───Panel ------------\u003e Panel预制体\r\n│   │   ├───Popup ------------\u003e Popup预制体\r\n│   │   └───Widget -----------\u003e Widget预制体\r\n│   ├───Scripts --------------\u003e GameA 相关脚本\r\n│   └───Textures -------------\u003e GameA 相关资源\r\n├───GameBBundle --------------\u003e GameB Bundle 优先级 1\r\n│   ├───Prefabs\r\n│   │   ├───Layer\r\n│   │   ├───Panel\r\n│   │   ├───Popup\r\n│   │   └───Widget\r\n│   ├───Scripts\r\n│   └───Textures\r\n├───HallBundle -------------------\u003e 大厅 bundle 优先级 3\r\n│   ├───Prefabs\r\n│   │   ├───Layer\r\n│   │   ├───Panel\r\n│   │   ├───Popup\r\n│   │   └───Widget\r\n│   ├───Scripts\r\n│   └───Textures\r\n├───MainBundle -------------------\u003e 主要 bundle 优先级 5，主要是一些通用的模块和资源\r\n│   ├───Prefabs\r\n│   │   ├───Layer\r\n│   │   ├───Panel\r\n│   │   ├───Popup\r\n│   │   └───Widget\r\n│   ├───Scripts\r\n│   │   └───common ---------------\u003e 一些通用脚本\r\n│   ├───Sounds -------------------\u003e 项目音频资源(也可分散在各bundle)\r\n│   └───Textures -----------------\u003e 通用资源\r\n└───framework --------------------\u003e 框架代码所在\r\n    ├───manager ------------------\u003e 一些管理类，如自定义事件分发、UI控制管理、本地存储等\r\n    ├───tools --------------------\u003e 一些工具类，如通用加密、装饰器等\r\n    ├───ui -----------------------\u003e 主要是对4种UI界面类型的封装\r\n    ├───widget -------------------\u003e 一些通用控件或组件，如TableView、适配组件等\r\n    └───kk.ts --------------------\u003e 控制UI树根节点，管理所有manager\r\n```\r\n#### 3、UI创建示范\r\n使用 kk-helper 插件来一键创建UI，快捷键CTRL+F12；然后在AssetConfig里面配置对应的信息，再调用对应的API函数即可。\r\n```typescript\r\n// UI的配置格式如下，定义在UIConfig.ts中\r\ninterface IUIConfig {\r\n    /**\r\n     * Bundle包名\r\n     */\r\n    bundle: string;\r\n    /**\r\n     * prefab资源名\r\n     */\r\n    name: string;\r\n    /**\r\n     * 挂载的脚本名, 可选；缺省表示和prefab资源名一致\r\n     */\r\n    script?: string;\r\n    /**\r\n     * 所加载资源的缓存模式，默认为 UICacheMode.NoCache，不缓存\r\n     */\r\n    cacheMode?: number;\r\n}\r\n\r\n// ---------------- Layer类型 ----------------\r\n// const LayerConf = {\r\n//     Hall: \u003cIUIConfig\u003e {\r\n//         bundle: BundleConf.Hall,\r\n//         name: \"HallLayer\",\r\n//         cacheMode: UICacheMode.Cache,\r\n//     }\r\n// };\r\n\r\n// 异步方法，跳转到指定的Layer;\r\n// 第一个参数类型是IUIConfig，下面一样；\r\n// 第二个参数表示传入这个Layer的任意类型数据，可选；传入的数据会被脚本自动保存。\r\nkk.uiMgr.goLayerAsync(LayerConf.Hall, data);\r\n// 异步方法，重置刷新当前Layer，data为传入的数据，可选。\r\nkk.uiMgr.resetCurLayerAsync(data)\r\n\r\n// ---------------- Popup类型 ----------------\r\n// const PopupConf = {\r\n//     Settings: \u003cIUIConfig\u003e {\r\n//         bundle: BundleConf.Hall,\r\n//         name: \"SettingsPopup\",\r\n//     }\r\n// };\r\n\r\n// 异步方法，显示指定的弹窗；\r\n// 第一个参数类型是IUIConfig；第二个是要传入的数据，可选\r\nkk.uiMgr.showPopupAsync(PopupConf.Settings);\r\n// 弹窗关闭后，会返回用户在弹窗脚本中设置的任意类型数据。\r\nlet ret = await kk.uiMgr.showPopupAsync(PopupConf.Settings, data);\r\n\r\n// ---------------- Panel、Widget类型 ----------------\r\n// const PanelConf = {\r\n//     Game: \u003cIUIConfig\u003e {\r\n//         bundle: BundleConf.Hall,\r\n//         name: \"GamePanel\"\r\n//     },\r\n// };\r\n//\r\n// const WidgetConf = {\r\n//     Toast: \u003cIUIConfig\u003e {\r\n//         bundle: BundleConf.Main,\r\n//         name: \"Toast\",\r\n//         cacheMode: UICacheMode.Cache\r\n//     }\r\n// }\r\n\r\n// 都是异步方法，跟前面不同的是，这两种类型创建后，需要自行设置父节点才会显示。\r\nkk.uiMgr.createPanelAsync(PanelConf.Game);\r\nkk.uiMgr.createWidgetAsync(WidgetConf.Toast);\r\n\r\n```\r\n#### 4、网络模块使用示范\r\nhttp和websocket都使用async/await、Promise进行封装，既支持异步也支持同步。\r\n```typescript\r\n// http\r\nlet ret = await kk.httpMgr.reqAsync(...);\r\nkk.httpMgr.reqAsync(...).then(...).catch(...);\r\n\r\n// websocket\r\nawait kk.socketMgr.connectAsync(...)\r\nlet ret = await kk.socketMgr.reqAsync(...);\r\nkk.socketMgr.connectAsync(...).then(...).catch(...);\r\n\r\nkk.socketMgr.on(cmd, callback, target);\r\nkk.socketMgr.off(cmd, callback, target);\r\n\r\n```\r\n详细使用以及其他使用示例请运行demo工程查看。  \r\n[更新日志](https://github.com/Mortal-Li/cocos-creator-async/blob/main/CHANGELOG.md)\r\n \r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMortal-Li%2Fcocos-creator-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMortal-Li%2Fcocos-creator-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMortal-Li%2Fcocos-creator-async/lists"}