cocos加载图片

随手记录一下使用cocos框架从resources文件夹下加载图片过程中出现的一个问题,虽然我也不懂为什么要这么改,希望有大神可以赐教一下。

初始代码如下:

import { _decorator, Component, Sprite, SpriteFrame, resources } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ImageLoader')
export class ImageLoader extends Component {

    start() {
        this.loadImageAndApply();
    }

    loadImageAndApply() {
        const imagePath = 'images/1'; // 不包括文件扩展名

        resources.load(imagePath, SpriteFrame, (err, spriteFrame: SpriteFrame) => {
            if (err) {
                console.error(`Failed to load image at path '${imagePath}':`, err);
                return;
            }

            const sprite = this.getComponent(Sprite);
            if (!sprite) {
                console.error('Sprite component not found on the node.');
                return;
            }

            sprite.spriteFrame = spriteFrame;
        });
    }
}

简单说一下这段代码是希望能够从resources中读取图片然后挂载到节点的sprite组件上。但是经过实践发现会一直报错“找不到文件”

Failed to load image: Error: Bundle resources doesn't contain images/1
    at _loop (url-transformer.ts:106:35)
    at parse (url-transformer.ts:40:44)
    at Pipeline.sync (pipeline.ts:218:28)
    at preprocess (preprocess.ts:68:55)
    at Pipeline._flow (pipeline.ts:261:9)
    at Pipeline.async (pipeline.ts:256:14)
    at AssetManager.loadAny (asset-manager.ts:539:18)
    at Bundle.load (bundle.ts:247:31)
    at ImageLoader.loadImageAndApply (ImageLoader.ts:14:19)
    at ImageLoader.start (ImageLoader.ts:8:14)
(anonymous) @ ImageLoader.ts:16

经过伟大的gpt反复折腾之后,gpt给出了一个能够解决上述问题的方案,但我还是不是很清楚具体原理。修改后的代码如下:

import { _decorator, Component, Sprite, SpriteFrame, resources, ImageAsset, Texture2D } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ImageLoader')
export class ImageLoader extends Component {

    start() {
        this.loadImageAndApply();
    }

    loadImageAndApply() {
        const imagePath = 'images/1'; // 资源路径,不包括文件扩展名

        resources.load(imagePath, ImageAsset, (err, imageAsset: ImageAsset) => {
            if (err) {
                console.error(`Failed to load image asset at path '${imagePath}':`, err);
                return;
            }

            // 创建 Texture2D 对象
            const texture = new Texture2D();
            texture.image = imageAsset;

            // 创建 SpriteFrame 并设置 Texture2D
            const spriteFrame = new SpriteFrame();
            spriteFrame.texture = texture;

            // 获取当前节点的 Sprite 组件
            const sprite = this.getComponent(Sprite);
            if (!sprite) {
                console.error('Sprite component not found on the node.');
                return;
            }

            // 设置 SpriteFrame
            sprite.spriteFrame = spriteFrame;
        });
    }
}

可以发现它是先将图片用imageasset读入再一步一步进行转换。

好了,就这么简单地记录一下。恳请各位大佬赐教

==============================================================

2024.02.03更新

在官方文档里找到了答案,之前没有认真看官方文档。

因为我加载的是png文件,所以得到的类型应该是ImageAsset。因此需要从imageasset到texture再到spriteframe的转换。

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Cocos引擎加载temp图片是通过使用Sprite组件和Texture2D组件实现的。 首先,要加载temp图片,需要将图片资源添加到Cocos项目的资源文件夹中。可以将图片文件拖拽到项目的"Resources"文件夹下,或者使用代码将图片添加到资源管理器中。 接下来,我们可以在对应的场景或节点上添加一个Sprite组件。在Cocos Creator编辑器中,选中对应的节点后,在属性检查器中点击“添加组件”按钮,选择“Sprite”组件。 然后,在Sprite组件的属性检查器中,找到“spriteFrame”属性。可以通过代码或拖拽的方式,将temp图片资源赋值给“spriteFrame”。如果通过代码赋值,可以使用cc.SpriteFrame.createWithTexture2D(texture2D)来创建一个SpriteFrame,并将其赋值给“spriteFrame”。 除了Sprite组件之外,还需要使用Texture2D组件来加载temp图片。可以在代码中使用cc.loader.load加载图片资源,并通过cc.Texture2D来获取Texture2D对象。接着,将Texture2D对象传递给SpriteFrame,再将SpriteFrame赋值给Sprite组件的“spriteFrame”。 另外,当加载图片大小和显示的尺寸不一致时,可以通过Sprite组件的“sizeMode”和“type”属性来调整图片的显示方式和尺寸。 总结起来,要加载temp图片,需要通过Sprite组件和Texture2D组件来实现。首先将图片资源添加到项目的资源文件夹中,然后在对应的节点上添加Sprite组件,并将temp图片赋值给Sprite组件的“spriteFrame”。同时,使用Texture2D组件加载图片资源并将其赋值给Sprite组件的“spriteFrame”。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小麦要吃麦当劳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值