Sprite Frame Assets

Sprite Frame is a container for UI rendering and basic graphics, which manages the clipping and tiling data on top of a Texture2D asset (by holding a reference to it).

Importing Sprite Frame Assets

Use the default asset import method to import image assets into the project, then set the type of image as sprite-frame and can then be seen in the Assets panel.

imported texture

Image assets will use thumbnails of their own pictures as icons in the Assets panel. When the image sub-asset is selected in the Assets panel, a thumbnail of the image is displayed below the Inspector panel.

Using a Sprite Frame

The object contained in the container is using textures

In the editor, drag the SpriteFrame asset to the Sprite Frame property of the Sprite component to switch the image displayed by the Sprite. At runtime, taking the content picture in the above picture as an example, The entire asset is divided into image asset (content), its sub-asset (spriteFrame) and sub-asset (texture). The assets in the game package can be obtained by the following methods:

Method 1: (load ImageAsset):

  1. const self = this;
  2. const url = 'test_assets/test_altas/content';
  3. resources.load(url, ImageAsset, (err: any, imageAsset) => {
  4. const sprite = this.getComponent(Sprite);
  5. const spriteFrame = new SpriteFrame();
  6. const tex = new Texture2D();
  7. tex.image = imageAsset;
  8. spriteFrame.texture = tex;
  9. sprite.spriteFrame = spriteFrame;
  10. });

Method 2: (load SpriteFrame):

  1. const self = this;
  2. const url = 'test_assets/test_altas/content/spriteFrame';
  3. resources.load(url, SpriteFrame, (err: any, spriteFrame) => {
  4. const sprite = this.getComponent(Sprite);
  5. sprite.spriteFrame = spriteFrame;
  6. });

Assets on the server can only be loaded into ImageAsset. For specific methods, please refer to the dynamic load asset documentation.

Cocos Creator will provide a way to package an Image Asset as a SpriteFrame in a later release to make it easier for users to use image assets.

The container contains objects that are used by RenderTexture

RenderTexture is a rendering texture that renders content from the camera directly to a texture instead of the screen. SpriteFrame can easily display 3D camera content on the UI by managing RenderTexture. Use is as follows:

  1. const cameraComp = this.getComponent(Camera);
  2. const renderTexture = new RenderTexture();
  3. const size = view.getVisibleSize();
  4. renderTexture.reset({
  5. width: size.width,
  6. height: size.height,
  7. colorFormat: RenderTexture.PixelFormat.RGBA8888,
  8. depthStencilFormat: RenderTexture.DepthStencilFormat.DEPTH_24_STENCIL_8
  9. });
  10. cameraComp.targetTexture = renderTexture;
  11. const spriteFrame = new SpriteFrame();
  12. spriteFrame.texture = renderTexture;
  13. const sprite = this.getComponent(Sprite);
  14. sprite.spriteFrame = spriteFrame;

API interface document: SpriteFrame.