Dynamic loading (assets) on Walker Script

Hi!

I have a scene script (as described here) and I need to load resources dynamically using the cc.loader class. But for some reason every method on that class is returning null or an empty object for valid paths or file names.
The dynamic loading only works when the game is running? Can be used at editor context? If that is the case how can I get SpriteFrames or SpriteAtlas from the scene script?

Regards

You can load external images into sprite. No need to use scene.

Hi @StudioAMK , Thanks for you answer.
I don’t understand what you meant with loading external images.

What I exactly need is to assign to a SpriteComponent a SpriteFrame (and Atlas). I need to do this by code at Editor time. For this I have the name of the Atlas and the name of the sprite frame (the atlas is already imported into the project under the resources folder), so when I try to load the atlas dynamically using the cc.loader from the scene-script, all the methods are returning null.

Regards.

That’s because your texture is returned by a callback and not by the function itself :slight_smile:

cc.loader.loadRes("FolderPathAsString", (err, resource)=> {
   if(err != null)
   {
      yourSprite.spriteFrame = resource;
   }
   else
   {
       console.error(err);
   }
});

Hi @JodanAziul !
Yeah my bad when posting the issue, I know that the result come from the callback, the resource variable of the callback is returning null or empty (depend on the method used).

At the end I was able to fix my issue but not using the cc.loader. Instead I checked out the AtlasSprite and FrameSprite classes of Cocos Creator and I noticed that those classes are using the cc.AssetLibrary class to load the assets using the uuid. So my solution at the end was to obtain the uuid from the .meta file of my asset and then use the cc.AssetLibray.load(uuid, function(err,asset) { }); method.

Regards!