在CC_EDITOR中,如何加載Resources裡的資源?

因為製作prefab的時候,
想要讓Editor介面裡的圖片也能隨著property不同而變換,

但是loadRes時都會報這個異常:

目前的代碼如下:


let buttonStyle = cc.Enum(
{
    Green_BlackHalo: 0,
    Green_GoldBorder: 1,
    Transparent_GoldBorder: 2,
    Transparent_GreenHalo: 3
});

cc.Class(
{
    extends: cc.Component,
    editor: { executeInEditMode: true },
    properties:
    {
        String:        { default:'' },
        i18n:        { default:'' },
        Style:        { type: buttonStyle, default: buttonStyle.Green_BlackHalo },
        FontSize:    { default:18 },
        Icon:        { type: cc.SpriteFrame, default:null },
    },
    onLoad: function ()
    {
        this.nodes =
        {
            Icon: this.node.getChildByName( '@Icon' ).getComponent( cc.Sprite ),
            Label: this.node.getChildByName( '@Label' ).getComponent( cc.Label )
        };
        try { this.Render(); }
        catch( ex ) { cc.log( '[@Button] Render Error: ' + ex ); }
    },
    Render ()
    {
        if( this.nodes.Label.fontSize !== this.FontSize ) this.nodes.Label.fontSize = this.FontSize;
        if( this.Icon != null )
        {
            this.nodes.Icon.node.active = true;
            this.nodes.Icon.spriteFrame = this.Icon;
        }
        else this.nodes.Icon.node.active = false;

        if( this.Style != buttonStyle.Green_BlackHalo )
        {
            let name = buttonStyle[this.Style];
            //這邊都會找不到
            cc.loader.loadRes( 'images/ui/components/TextBoxes', cc.SpriteFrame, ( err, frames ) =>
            {
                for( let idx = 0; idx < frames.length; idx++ )
                {
                    let f = frames[idx];
                    cc.log( 'frames: ' + f._name );
                }
            });
        }

    },
});

如果 CC_EDITOR下,無法使用 cc.loader.loadRes 來取得資源,我該如何設置這個圖片?

請高手指點一下,感謝 :joy:

请问你assets/resources/images/ui/components/目录下有TextBoxes.png或TextBoxes.jpg图片吗?
另外你cc.loader.loadRes的回调函数的参数也不对,见这里
另外就是我还是没有搞清楚你问的是什么?

cc.loader.loadRes這個method我重新封裝過,
這段代碼只是示例,我修改成標準的寫法了

說明一下我的情況,
在模似器下是可以正常取得 SpriteFrames的

我想要的是在Creator的編輯器中,也可以載入圖片 ( editor:{ executeInEditMode; true } 的情況下 )

謝謝您的回覆

抱歉编辑器下目前不支持 loadRes API
你需要使用插件相关的 API 去加载资源
类似

var uuid = Editor.assetdb.remote.urlToUuid('db://assets/resources/images/ui/components/TextBoxes.png/TextBoxes');
cc.loader.load({ type: uuid, uuid: uuid }, function (err, sf) {...});
1赞

感谢 @jare 大神的回复,的确可以正常载入,谢谢

万一有人需要:
应该是 type:‘uuid’, 不是type:uuid

1赞

3.5.x的版本这个功能怎么实现???

一直都没有回复吗
感觉现在论谈的活跃度没有以前高了