playMaker

Author Topic: Math app / display numbers as sprite[SOLVED]  (Read 813 times)

TerraImagina

  • Playmaker Newbie
  • *
  • Posts: 33
Math app / display numbers as sprite[SOLVED]
« on: March 10, 2020, 10:05:02 AM »
Hello everyone,

I am trying to find a way to display numbers as sprites (with animations). This is for a math app where I need to be able to use the numbers for calculation as int and display everything with sprite.
I am quite unsure how to design the logic as simple and versatile as possible.
I would like to have 10 sprites, from 0 to 9 and be able to use them to display any int number in real time.
I can do that easily for numbers from 0 to 9, but when the int numbers are 10 or 100
it become more tricky :

for 15, I would need to know it is made of 2 numbers : 1 and 5
 create a game object with 2 children
 use the sprite nammed 1 for the first child,
 translate the next object to the right to have some space in between
 use the sprite nammed 5 for the second child,

Is there an elegant method to work with numbers this way, without using to much ressource (creating and destroying object...) ?

Any help would be really appreciated :)
« Last Edit: March 11, 2020, 05:55:01 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Math app / display numbers as sprite
« Reply #1 on: March 11, 2020, 02:22:44 AM »
Hi,

 I think your best option will be to use TextMeshPro which allows you to create a custom font, which means you can use your own graphics for the numbers.

else, this is indeed not trivial, but doable with many pitfalls...

- you need an horyzontal layout where you will instantiate all the numbers from a prefab, or you could already have a given number of instances and you enable/disable them as required

- the prefab can be only just one, and you have an fsm on it or a manager that will switch the sprite on demand.

- turn your int number into a string
- iterate through each character of that string
- have an array where the int value of that character is the index, and the content of that index if your Sprite, assign that sprite to the UI Image component

that's the basics of it, but there is more, you need to clean up the list or disable the instances you don't need depending on the lengh of your int and likely few other things that I can't think of right now :)

Bye,

 Jean

TerraImagina

  • Playmaker Newbie
  • *
  • Posts: 33
Re: Math app / display numbers as sprite
« Reply #2 on: March 11, 2020, 05:46:21 AM »

Thank you so much Jean!
I didnt know textmeshpro allow you to use custom image atlas, just saw a video about it and it work even with colored texture, this is indeed the best way to go :)

I will try this way first, if for some reason it doesnt work as intented I will try as you suggest with playmaker and a bit of patience.