cocos creator 插件开发小知识

编辑器

  • 获取预览地址
    /** cocos 编辑器窗口对象 */
    const cocos_window = ((electron as any).remote as typeof electron).BrowserWindow.getAllWindows().find(
    	(v) => v.title !== "CocosCreator" && v.title.includes("Cocos Creator")
    )!;
    /** 预览地址 */
    let preview_url_s = await cocos_window.webContents.executeJavaScript("window.xxx.address");
    
    // 不存在预览地址
    if (!preview_url_s) {
    	await cocos_window.webContents.executeJavaScript(
    		'window.xxx.$refs.qr.parentElement.dispatchEvent(new Event("mouseenter")); window.xxx.$refs.qr.parentElement.dispatchEvent(new Event("click"))'
    	);
    	await new Promise<void>((resolve_f) => {
    		const timer = setInterval(async () => {
    			preview_url_s = await cocos_window.webContents.executeJavaScript("window.xxx.address");
    			if (preview_url_s) {
    				clearInterval(timer);
    				resolve_f();
    			}
    		}, 10);
    	});
    
    	cocos_window.webContents.executeJavaScript('window.xxx.$refs.qr.parentElement.dispatchEvent(new Event("mouseleave"))');
    }
    

窗口

  • 获取当前打开的窗口对象
const window_id_ns = electron.BrowserWindow.getAllWindows().map((v) => v.id);
await Editor.Panel.open(config.name_s);
const window = electron.BrowserWindow.getAllWindows().find((v) => !window_id_ns.includes(v.id))!;
posted @ 2023-04-16 03:05  Muzzik  阅读(268)  评论(0编辑  收藏  举报