Created
February 1, 2024 20:15
-
-
Save ealbinu/733b07c179a8af340b35fbd684ba15cc to your computer and use it in GitHub Desktop.
Cargar textura remota y asignarla a atlas en cocos creator 3.8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { _decorator, Component, Node, assetManager, SpriteFrame, Texture2D, Sprite, ImageAsset } from 'cc'; | |
| const { ccclass, property } = _decorator; | |
| @ccclass('Loader') | |
| export class Loader extends Component { | |
| // Atlas inicial | |
| @property(cc.SpriteAtlas) | |
| atlasInicial: cc.SpriteAtlas = null; | |
| start() { | |
| const self = this; | |
| let remoteurl = 'https://firebasestorage.googleapis.com/v0/b/bluegamesmx.appspot.com/o/atexture2%2Fatexture2.png?alt=media&token=b985bc68-8295-462d-a52a-03bb865776e1' | |
| // Descarga de imagen | |
| assetManager.loadRemote<ImageAsset>(remoteurl, ImageAsset, (err: any, imageAsset) => { | |
| if(err){ // Si ocurre error al descargar | |
| console.error('ERROR:', err); | |
| return; | |
| } | |
| // Convertir imagen en textura | |
| const texturaRemota = SpriteFrame.createWithImage(imageAsset); | |
| // Obtener cada frame del atlas incial | |
| for(var contador in self.atlasInicial.spriteFrames){ | |
| // Guardar cada frame en variable | |
| const frame = self.atlasInicial.spriteFrames[contador] | |
| // Asignar la textura a los frames. | |
| frame._texture = texturaRemota; | |
| } | |
| }); | |
| } | |
| update(deltaTime: number) { | |
| // Nada... | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment