[SOLVED] How can I change images dynamically?


#1

I have a json of images and need to fill a carousel with them. I can create new elements but Im missing how to attach the image resource to them

The docs are really lacking


#2

Assuming you have a reference to the texture asset, https://developer.playcanvas.com/en/api/pc.ElementComponent.html#textureAsset


#3

I have a reference to the png asset on the folder - would that be the texture?


#4

Yes, that would work. If you select the asset in the project pane, the inspector will tell you what type of asset it is.

You can also use the AssetRegistry to get an asset by name, tag or a custom filter (https://developer.playcanvas.com/en/api/pc.AssetRegistry.html)


#5

Thanks, how do you assign the texture to the image element with code?


#6

Example here where I find the asset by name in the AssetRegistry and use the API reference doc that I linked to earlier: https://playcanvas.com/project/626800/overview/load-texture-asset-to-ui

// initialize code called once per entity
LoadImageToUi.prototype.initialize = function() {
    var textureAsset = this.app.assets.find("Camera1.png");
    this.entity.element.textureAsset = textureAsset;
};

#7

Brilliant, thanks a lot


#8

Yaustar - that worked flawlessly thanks.

Got a similar method to add a model to the entity and texture it? Im making a custom particle system and want to spawn 3D rotating coins for example


#9

Think I found it


#10

Same process except you will be needing the model asset (or mesh asset, I can’t remember the actual name) and corresponding material assets.