Asset Management

There are two ways to load assets: 1. Preloaded by Reference; 2 Dynamic Loading

Preloaded by Reference

The assets are loaded before the game logical code is executed. Normally the references are specified in scene editing and prefab editing

  • Reference Texture Asset
    Set the UIImage object's texture property or define the qc.Serializer.TEXTURE type's field in behaviour script
    image

  • Reference Animation Asset
    Set the Sprite object's texture property or define the qc.Serializer.TEXTURE type's field in behaviour script
    sprite

  • Reference Sound Asset
    Set the Sound object's audio property or define the qc.Serializer.AUDIO type's field in behaviour script
    sound

  • Reference Font Asset Set the UIText object's font property or define the qc.Serializer.FONT type's field in behaviour script
    font

  • Reference Prefab Asset
    Define the qc.Serializer.PREFAB type's field in behaviour script

  • Reference Excel Asset
    Define the qc.Serializer.EXCELASSET type's field in behaviour script

  • Reference Text Asset
    Define the qc.Serializer.TEXTASSET type's field in behaviour script
    other

Dynamic Loading

For some assets, you don't want them to be preloaded, then you can download them at runtime programmly. See game.assets.load

  • Load texture asset:

    self.game.assets.load('Assets/texture/texture.bin', function(r) {
      // Find the UIImage object to set texture asset
      self.game.world.find('UIRoot/background').texture = r;
    });
    
  • Load audio asset

    self.game.assets.load('Assets/audio/newlevel.mp3.bin', function(r) {
      // Find the Sound object to set audio asset
      var soundCtrl = self.game.world.find('Sound');
      if (!soundCtrl.audio) {
          soundCtrl.audio = r;
      }
      soundCtrl.play();
    });
    
  • Load animation asset

    self.game.assets.load('Assets/sprites/panda.bin', function(r) {
      // Find the Sprite object to set animation asset
      var spriteCtrl = self.game.world.find('Sprite');
      spriteCtrl.texture = r;
      spriteCtrl.playAnimation('atk2', 1, false);
    });
    
  • Load webFont asset

    self.game.assets.load('Assets/font/HK.bin', function(r) {
      // Find the UIText object to set font asset
      var textCtrl = self.font.find('UIText');
      textCtrl.fontFamily = qc.UIText.WEBFONT;
      textCtrl.font = r;
    });
    
  • Load prefab asset

    self.game.assets.load('Assets/prefabs/prefab.bin', function(r) {
      // Clone a new game object from prefab asset, and add to the game
      self.game.add.clone(r, self.gameObject.find('list'));
    });
    
  • Load excel asset

    self.game.assets.load('Assets/excel/mother.bin', function(r) {
      // Print all the sheets name from the excel asset
      console.log(r.sheetsName.join(','));
    });
    
  • Load text asset

    self.game.assets.load('Assets/excel/mario_map.bin', function(r) {
      // Print the json content from the text asset
      console.log(r.text);
    });
    

Release Asset

We recommand to use the way "Preloaded by Reference", if you need to load assets dynamiclly, we recommand you to use URL as key, to make the keys easy to maintain:

// Load with one param as key and URL
self.game.assets.load('Assets/sprites/panda.bin');

After assets are loaded, they are saved in memory, there are two ways to unload the loaded assets.

  1. Call game.assets.unload, specify the asset key, url or object to unload.
    self.game.assets.unload('Assets/sprites/panda.bin');
    
  2. When switching scene, set the second param as true to clear all the loaded assets
    self.game.scene.load('nextState', true);
    

API

Assets API

Demo

Assets Demo