cocos-engine icon indicating copy to clipboard operation
cocos-engine copied to clipboard

无法通过 cc.director.getScene().name 获得当前场景的名称.

Open finscn opened this issue 9 months ago • 3 comments

Cocos Creator version

3.8.5

System information

all

Issue description

执行 cc.director.getScene().name 返回的是空字符串.

很多很多年前 有人问过类似问题 https://forum.cocos.org/t/1-3-2-cc-director-getscene-name/42358/4 (不过是老版本的 cocos)

我在网上也没搜到正确答案.

我这里选择的是 当前场景 .

Image

我觉得 无论用户是选择 当前场景 , 还是选择 某一个特定的 scene, 无论这个scene是放在某个bundle中 还是没有, 无论是开发模式 还是 预览模式 还是 release模式, "能够正确获得当前场景的名称" 都是一个基本能力.

Relevant error log output

No response

Steps to reproduce

.

Minimal reproduction project

No response

finscn avatar Mar 11 '25 10:03 finscn

我再补充一个需求,获得项目名称:game.projectName

hubluesky avatar Mar 12 '25 02:03 hubluesky

这个问题我遇上过。 1、对于默认启动场景,无法通过 director.getScene().name 获得名称。 2、对于使用 director.loadScene 加载的场景,可以获得名称。

MrKylinGithub avatar Mar 12 '25 07:03 MrKylinGithub

这个问题我遇上过。 1、对于默认启动场景,无法通过 director.getScene().name 获得名称。 2、对于使用 director.loadScene 加载的场景,可以获得名称。

@MrKylinGithub 这个问题能解决吗? 还是说底层设计导致的无法实现?


更新:
找到了一个获得名字的方法. 但是太麻烦了.

        const uuid = cc.director.getScene().uuid;
        const bundle = cc.assetManager.bundles.find(
            (bundle: cc.AssetManager.Bundle): boolean => !!bundle.getAssetInfo(uuid)
        );
        const assetInfo = bundle.getAssetInfo(uuid);
        const path = (assetInfo as any).path as string;
        const sceneName = path.split('/').pop();
        console.log(sceneName);

finscn avatar Sep 02 '25 16:09 finscn