Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Drawing a bitmap font

Discussion in '2D' started by m-szulc, Jan 26, 2015.

  1. m-szulc

    m-szulc

    Joined:
    May 22, 2013
    Posts:
    3
    Hi,

    I'm trying to draw a bitmap font in Unity3d. I've got a png image with bitmap font (attached), I have an array of offsets for each character and now I would like to draw a part of this texture (one character) several times in one update.

    font_number.png
    How can I do that? In other frameworks (such as Cocos2dx) I am able to override Draw function, but AFAIK it is not possible in Unity3d.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    The easiest way to do it is probably to import it as a sprite, set it to multiple and cut it up with the sprite editor as usual. Put the sprite in a folder named "Resources". Then add the sprites to the scene where you want them to be. Finally use something like this to display the correct frame:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class number : MonoBehaviour {
    5.     Sprite[] numbers;
    6.  
    7.     public int n;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         // Replace "numbers" with the name of your sprite asset
    12.         numbers = Resources.LoadAll<Sprite> ("numbers");
    13.     }
    14.    
    15.     void Update() {
    16.         SpriteRenderer sr = p.GetComponent<SpriteRenderer> ();
    17.        
    18.         // Which number you want to display
    19.         sr.sprite = numbers [n];
    20.     }
    21. }
     
    theANMATOR2b likes this.
  3. m-szulc

    m-szulc

    Joined:
    May 22, 2013
    Posts:
    3
    Thanks for your answer. I know, that I can create multiple sprites (one for each character). However I am not sure how this will impact performance on mobile devices. I just thought clipping and drawing will be more efficient.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    I don't think this will have any significant impact on performance. Using sprites from a resource folder, might have a slight impact since Unity doesn't use the sprite packer for those sprites. Which might mean an extra draw call. But it should affect performance in a noticeable way.