Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在IDE环境中,game事件比脚本先启动,导致脚本对game事件的监听无法回调 #17792

Open
hubluesky opened this issue Oct 31, 2024 · 2 comments
Labels
Bug Needs Triage Needs to be assigned by the team

Comments

@hubluesky
Copy link

Cocos Creator version

3.8.4

System information

windows

Issue description

为了极数与方便启动,我把项目做成单一场景,并支持预加载资源,并保证了资源在所有脚本的onLoad之前就可以正常使用。就干了以下操作。
在一个脚本中调用以下 game.onPostProjectInitDelegate.add(() => new Promise(resolve => setupConfig.initialize(resolve)));。该代码在脚本加载时就会调用,我可以在这个函数里加载一些配置,保证在场景启动之前就可以使用。
这个做法在web端,浏览器,发布后等渠道都是正常可行,但在编辑器中却无法运行,原因是在编辑器环境中,脚本的import工作是在game.init之后的,导致脚本启动时game已经初始化结束了。
image

Relevant error log output

No response

Steps to reproduce

1、新建一个空项目
2、添加以下脚本,并在浏览器和IDE中运行,可以看到浏览器有log打印,但在IDE中没有。

import { game } from "cc";

class SetupConfig {
    initialize(resolve: (value: void | PromiseLike<void>) => void): void {
        console.log("setupConfig initialize");
        resolve();
    }
}

const setupConfig = new SetupConfig();
game.onPostProjectInitDelegate.add(() => new Promise<void>(resolve => setupConfig.initialize(resolve)));

Minimal reproduction project

No response

@hubluesky hubluesky added Bug Needs Triage Needs to be assigned by the team labels Oct 31, 2024
@longchuan
Copy link
Contributor

在onLoad调用之前,是调用constructor的

@longchuan
Copy link
Contributor

单一场景,还要预加载资源,一般都是放一个loading界面,这个界面肯定是在初始化之后的。编辑器的界面也是一个游戏界面,所以是初始化过的,问题可能是编辑器在运行时没有重新进行初始化,但是如果重新初始化可能导致加载速度变慢。也许这个接口应该对编辑器进行特殊处理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Needs to be assigned by the team
Projects
None yet
Development

No branches or pull requests

2 participants