首页 > 其他分享 >cocos creator 教程:框架 - 资源管理

cocos creator 教程:框架 - 资源管理

时间:2023-02-08 09:44:22浏览次数:37  
标签:cocos const creator cc script bundle id 资源管理 加载

【muzzik 教程】:框架 - 资源管理

实现功能

  • asset (资源管理器)

    • 资源加载接口整合

    • 编辑器资源加载

    • 资源生命周期管理

  • bundle (bundle 管理器)

    • bundle 加载/重载

    • scene 加载

    • bundle 管理单元

asset (资源管理器)

资源加载接口整合

统一本地/远程/编辑器资源加载接口为 get/get_dir

编辑器资源加载

// path_s_ 格式为 "db://assets/xxx"
const uuid_s = await Editor.Message.request("asset-db", "query-uuid", path_s_);
cc.assetManager.loadAny({ uuid: uuid_s }, completed_f);

资源生命周期管理

在游戏中经常会遇到短时间加载/卸载同一资源,所以针对这点进行优化

  1. 加载资源后引用计数 + 2
  2. 重载 decRef,在引用计数为 1 时加入释放表,释放表间隔批量释放

bundle (bundle 管理器)

bundle 加载

很简答,封装 cc.assetManager.loadBundle 为异步函数即可

bundle 重载

由于引擎没有实现 bundle 重载后的脚本资源更新,所以需要我们自己做

/** 重新加载 bundle */
async reload(info: Partial<bundle_.bundle_info>): Promise<cc.AssetManager.Bundle | null> {
	if (PREVIEW) {
		return null;
	}

	await this._engine_init_task;

	/** bundle 信息 */
	const bundle_info: bundle_.bundle_info = new bundle_.bundle_info(info)!;
	/** bundle 脚本表 */
	const bundle_script_tab: Record<string, any> = {};
	/** js 系统 */
	const system_js = self["System"];
	/** 脚本缓存表 */
	const script_cache_tab: Record<string, any> = system_js[Reflect.ownKeys(system_js).find((v) => typeof v === "symbol")];

	// 更新 bundle 信息
	this.add(bundle_info);

	// 初始化 bundle 脚本表
	Object.keys(script_cache_tab).forEach((v_s) => {
		const current = script_cache_tab[v_s];
		const parent = script_cache_tab[v_s].p;
		const child = parent.d;

		if (!parent || !child || current.id !== parent.id) {
			return;
		}

		const name_s = parent.id.slice((parent.id as string).lastIndexOf("/") + 1);

		bundle_script_tab[name_s] = parent;
	});

	// 清理脚本缓存
	{
		const bundle_root = bundle_script_tab[bundle_info.bundle_s];

		if (bundle_root) {
			bundle_root.d.forEach((v: { id: string }) => {
				/** 脚本名 */
				const name_s = v.id.slice((v.id as string).lastIndexOf("/") + 1, v.id.length - 3);
				/** ccclass */
				const ccclass = cc.js.getClassByName(name_s);

				// 注销组件
				if (ccclass) {
					cc.js.unregisterClass(ccclass);
				}

				delete script_cache_tab[v.id];
				delete system_js["registerRegistry"][v.id];
			});
			delete script_cache_tab[bundle_root.id];
			delete system_js["registerRegistry"][bundle_root.id];
		}
	}

	// 清理 bundle 资源
	{
		const bundle = cc.assetManager.getBundle(bundle_info.bundle_s);

		if (bundle) {
			bundle.releaseAll();
			cc.assetManager.removeBundle(bundle);
		}
	}

	// 加载 bundle
	return this.load(bundle_info);
}

scene 加载

/** switch_scene 配置 */
export class switch_scene_config {
	constructor(init_?: Partial<switch_scene_config>) {
		Object.assign(this, init_);
	}

	/** bundle名(getBundle 时使用) */
	bundle_s = "resources";
	/** 预加载 */
	preload_b?: boolean;
	/** 加载回调 */
	progress_callback_f?: (finish_n: number, total_n: number, item?: cc.AssetManager.RequestItem) => void;
	/** 加载前调用的函数 */
	before_load_callback_f?: cc.Director.OnBeforeLoadScene;
	/** 启动后调用的函数 */
	launched_callback_f?: cc.Director.OnSceneLaunched;
	/** 场景卸载后回调 */
	unloaded_callback_f?: cc.Director.OnUnload;
}

bundle 管理单元

...

标签:cocos,const,creator,cc,script,bundle,id,资源管理,加载
From: https://www.cnblogs.com/muzzik/p/17099273.html

相关文章

  • OKR 目标绩效系统:支持人力资源管理
    OKRs被各地的团队和组织用来将高层战略转化为短期目标。作为一个致力于管理公司人力资源(HR)的团队,你可能正在监督那些在自己的工作中使用OKRs的团队,或者有兴趣为自己实施这......
  • Cocos rgb代码修改颜色
    项目中遇到美术给的rgb代码要通过代码改变颜色代码实现如下:colorOfString(value:string):Color{if(value==null||value.length!=7){retur......
  • Qt Creator9.0生成工程后没有.pro文件
    QtCreator9.0默认建立的widget项目只有CMakeLists.txt文件,没有pro文件发现生成工程文件时默认选择的是cmake,不是qmake导致的这个问题,将Builldsystem中转化成qmake之后......
  • 《ATK-DLRV1126Qt开发环境搭建》第一章 Ubuntu下安装Qt Creator​
    Ubuntu下安装QtCreator1.1安装Qt​进入下载页面(注意如果找不到下载链接,我们就进行这个顶层目录一个个目录找,因为Qt下载链接会变动),选择Linux的安装包下载。使用迅雷下载再拷......
  • 编译Qt Creator 工程
    //build1.下载QtCreator源码2.2.下载Qt库源码,下载地址http://download.qt.io/archive/qt/5.6/5.6.1/single/,下载qt-everywhere-opensource-src-5.6.1.zip。3.建立空......
  • cocos 扩展插件的制作
    在cocos里面创建一个新的扩展,会生成两个文件,一个是package.json和browser.js。json文件就是来写配置的,里面主要是配置当前的扩展如何打开,以及扩展的一些名称,描述,版本限制......
  • CocosCreator 性能优化:DrawCall
    在游戏开发中,DrawCall作为一个非常重要的性能指标,直接影响游戏的整体性能表现。无论是CocosCreator、Unity、Unreal还是其他游戏引擎,只要说到游戏性能优化,DrawCall都......
  • 通过修改注册表添加资源管理器右键菜单选项
    regedit新建如下项计算机\HKEY_CLASSES_ROOT\Directory\Background\shell\自定义选项修改默认字符串值、新建:pushd&popd>helppushd保存当前目录以供POPD命令......
  • vs2015和cocos2d-x-3.17.2 开发环境搭建
    vs2015和cocos2d-x-3.17.2开发环境搭建 cocos2d-x当前最新稳定版本是3.17.2,需要使用vs2015来构建。首先是安装vs2015。选择安装下面的vs版本:cn_visual_studio_community_2......
  • 优化 Win11 资源管理器打开文件夹速度
    Win11比Win10多了很多动画、特效,所以会觉得Win11没有Win10用着快。通过以下设置可以有效提升文件夹打开速度:打开“高级系统设置”,点击“性能”设置,性能选项中勾选......