Search Unity

Sprites as numbers, automatic.

Discussion in '2D' started by Jip1912, Oct 3, 2015.

  1. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Hello,

    First of all sorry for the title, I really couldn't came up with a good title xD

    I have made a point system, included with GUI.Label. I don't like the look of those numbers in game.
    So I want to make it that if I have ''1'' point, there will come up a sprite with the number 1, which looks better.
    I know how to make it because I already made it with my countdown system aswell.
    In my countdown system it looked like this:

    Code (CSharp):
    1.     float timeRemaining = 4.0f;
    2.  
    3.     void Update () {
    4.         timeRemaining -= Time.deltaTime;
    5.     }
    6.  
    7.     void OnGUI(){
    8.  
    9.         if (timeRemaining > 2 && timeRemaining < 3)
    10.             count3.SetActive (true);          
    11.         else
    12.             count3.SetActive(false);
    13.  
    14.         if(timeRemaining > 0 && timeRemaining < 1)
    15.             count1.SetActive(true);
    16.         else
    17.             count1.SetActive(false);
    18.  
    19.         if(timeRemaining > 1 && timeRemaining < 2)
    20.             count2.SetActive(true);
    21.         else
    22.             count2.SetActive(false);
    23.  
    24.         if(timeRemaining > 0){
    25.             GUI.Label(new Rect(600, 600, 1000, 600),
    26.                       "Time Remaining : "+timeRemaining);
    27.  
    28.         }
    29.         else{
    30.             GUI.Label(new Rect(600, 600, 1000, 600), "Time's Up");
    31.             Application.LoadLevel ("InGame");
    32.         }
    33.     }
    34. }
    (count1,2,3 are sprites)

    But if I have a point system, the player can reach points like 27, 99, 375, 214, 69 and more...
    Its isnt really nice to write that all out and use tons of sprites (for every number).
    Instead of that I want to only have the numbers 0,1,2,3,4,5,6,7,8,9 and if I reach number 12, as example, there will come number 12 automaticly (with the sprites 1 and 2). And if I have number 235... yeah you understand it...

    Is this possible, or is there a better way to do this?

    Thank you.
     
  2. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    You can try this:
    2015-10-04_10-32-16.gif

    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour
    2. {
    3.     public float timeRemaining = 106.0f;
    4.     public int third = 0;
    5.     public int second = 0;
    6.     public int first = 0;
    7.  
    8.     public GameObject[] FirstSprites = new GameObject[10];
    9.     public GameObject[] SecondSprites = new GameObject[10];
    10.     public GameObject[] ThirdSprites = new GameObject[10];
    11.  
    12.     void Start()
    13.     {
    14.         ActivateSprites(timeRemaining);
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         timeRemaining -= Time.deltaTime;
    20.         ActivateSprites(timeRemaining);
    21.     }
    22.  
    23.     void ActivateSprites(float time)
    24.     {
    25.         // for example 159
    26.         // tempValue = 9
    27.         int tempValue = (int)time % 10;
    28.         if (first != tempValue)
    29.         {
    30.             //            FirstSprites[first].SetActive(false);
    31.             //            FirstSprites[tempValue].SetActive(true);
    32.             Debug.Log("First. Deactivate : " + first);
    33.             Debug.Log("First. Activate : " + tempValue);
    34.             first = tempValue;
    35.         }
    36.  
    37.         // tempValue = 5
    38.         tempValue = (int)time / 10 % 10;
    39.         if (second != tempValue)
    40.         {
    41.             //            SecondSprites[second].SetActive(false);
    42.             //            SecondSprites[tempValue].SetActive(true);
    43.             Debug.Log("Second. Deactivate : " + second);
    44.             Debug.Log("Second. Activate : " + tempValue);
    45.             second = tempValue;
    46.         }
    47.  
    48.         // tempValue = 1
    49.         tempValue = (int)time / 100;
    50.         if (third != tempValue)
    51.         {
    52.             //            ThirdSprites[third].SetActive(false);
    53.             //            ThirdSprites[tempValue].SetActive(true);
    54.             Debug.Log("Third. Deactivate : " + third);
    55.             Debug.Log("Third. Activate : " + tempValue);
    56.             third = tempValue;
    57.         }
    58.     }
    59. }
     
  3. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Thank you, I will try this out. It looks complicated ^^
     
  4. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    I just a basic idea and u can optimize this, for example:
    Code (CSharp):
    1.     // Sprites from assets
    2.     public Sprite[] NumbersSprites = new Sprite[10];
    3.  
    4.     // Or SpriteRenderer
    5.     public Image FirstNumber;
    6.     public Image SecondNumber;
    7.     public Image ThirdNumber;
    8.  
    9.     private void Update()
    10.     {
    11.         timeRemaining -= Time.deltaTime;
    12.         FirstNumber.sprite = NumbersSprites[(int)timeRemaining % 10];
    13.         SecondNumber.sprite = NumbersSprites[(int)timeRemaining / 10 % 10];
    14.         ThirdNumber.sprite = NumbersSprites[(int)timeRemaining / 100];
    15.     }
    2015-10-04_12-23-15.gif
     
  5. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    So the 2 messages you send are 2 different scripts or....??
    Unity doesn't know ''Image''.

    What does this do?
    1. public Image FirstNumber;
    2. public Image SecondNumber;
    3. public Image ThirdNumber;
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Please excuse me hijacking this thread - but what tool do you use to make these gifs?
     
  7. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    You need add "using UnityEngine.UI;".
    Image - https://unity3d.com/ru/learn/tutorials/modules/beginner/ui/ui-image
    FirstNumber and other is you UI sprites:


    https://getsharex.com/
     
    larku likes this.
  8. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
  9. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    You need validate current numbers. For example(i dont test this in unity, just typed here):
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         timeRemaining -= Time.deltaTime;
    4.         int tempValue = (int)timeRemaining / 100;
    5.         if (tempValue == 0)
    6.             ThirdNumber.sprite = NumbersSprites[tempValue];
    7.         else
    8.             // Hide number
    9.             ThirdNumber.sprite = null;
    10.  
    11.         int tempValue_2 = (int)timeRemaining / 10 % 10;
    12.         if (tempValue != 0 && tempValue_2 != 0)
    13.             SecondNumber.sprite = NumbersSprites[tempValue_2];
    14.         else
    15.             // Hide number
    16.             SecondNumber.sprite = null;
    17.  
    18.         tempValue = (int)timeRemaining % 10;
    19.         FirstNumber.sprite = NumbersSprites[tempValue];
    20.     }
     
  10. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    My code now:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PointSystem : MonoBehaviour {
    6.  
    7.     public float pointText;
    8.  
    9.     public Sprite[] NumbersSprites = new Sprite[10];
    10.  
    11.     public Image FirstNumber;
    12.     public Image SecondNumber;
    13.     public Image ThirdNumber;
    14.  
    15.     void Start ()
    16.     {
    17.         pointText = 0f;
    18.     }
    19.  
    20.  
    21.     private void Update ()
    22.     {
    23.         FirstNumber.sprite = NumbersSprites[(int)pointText % 10];
    24.         SecondNumber.sprite = NumbersSprites[(int)pointText / 10 % 10];
    25.         ThirdNumber.sprite = NumbersSprites[(int)pointText / 100];
    26.  
    27.         int tempValue = (int)pointText / 100;
    28.         if (tempValue == 0)
    29.             ThirdNumber.sprite = NumbersSprites[tempValue];
    30.         else
    31.             // Hide number
    32.             ThirdNumber.sprite = null;
    33.      
    34.         int tempValue_2 = (int)pointText / 10 % 10;
    35.         if (tempValue != 0 && tempValue_2 != 0)
    36.             SecondNumber.sprite = NumbersSprites[tempValue_2];
    37.         else
    38.             // Hide number
    39.             SecondNumber.sprite = null;
    40.      
    41.         tempValue = (int)pointText % 10;
    42.         FirstNumber.sprite = NumbersSprites[tempValue];
    43.     }
    44.  
    45.  
    46.     void OnTriggerEnter2D (Collider2D other)
    47.     {
    48.         if (other.gameObject.tag == "Player")
    49.         {
    50.             pointText += 1;
    51.             Debug.Log ("The score should update +1");
    52.         }
    53.     }
    54.  
    55.  
    56.     void OnGUI ()
    57.     {
    58.         GUI.Label(new Rect(-100, 10, 600, 900),
    59.                   "Score : "+pointText);              
    60.     }
    61. }
    62.  
    The code you send disables the source image but than it doesn't disable the color, so the whole image has to be disabled.

    http://i.imgur.com/PNRGylL.gifv

    btw. I use the same software as you use to make a gif, but how do I paste it into a thread like you?
     
  11. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    So Image.sprite = null does work. You need then try this:
    Code (CSharp):
    1. ThirdNumber.enable = false;
    2. //or
    3. ThirdNumber.gameObject.SetActive(false);
    Also you need extra check:
    Code (CSharp):
    1.         int tempValue = (int)pointText / 100;
    2.         if (tempValue == 0)
    3.         {
    4.              if (!ThirdNumber.enable) ThirdNumber.enable = true;
    5.             ThirdNumber.sprite = NumbersSprites[tempValue];
    6. }
    7.         else
    8.             // Hide number
    9.             ThirdNumber.enable = false;
    To past gif use "Upload a File".
     
  12. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    only the second number doesn't appear, but it also doesn't appear if I reach 10+.

    Code (CSharp):
    1. public class PointSystem : MonoBehaviour {
    2.  
    3.     public float pointText;
    4.  
    5.     public Sprite[] NumbersSprites = new Sprite[10];
    6.  
    7.     public Image FirstNumber;
    8.     public Image SecondNumber;
    9.     public Image ThirdNumber;
    10.  
    11.     void Start ()
    12.     {
    13.         pointText = 0f;
    14.     }
    15.  
    16.  
    17.     private void Update ()
    18.     {
    19.         FirstNumber.sprite = NumbersSprites[(int)pointText % 10];
    20.         SecondNumber.sprite = NumbersSprites[(int)pointText / 10 % 10];
    21.         ThirdNumber.sprite = NumbersSprites[(int)pointText / 100];
    22.  
    23.         int tempValue = (int)pointText / 100;
    24.         if (tempValue == 0)
    25.             ThirdNumber.sprite = NumbersSprites[tempValue];
    26.         else
    27.             // Hide number
    28.             ThirdNumber.gameObject.SetActive(false);
    29.    
    30.         int tempValue_2 = (int)pointText / 10 % 10;
    31.         if (tempValue != 0 && tempValue_2 != 0)
    32.             SecondNumber.sprite = NumbersSprites[tempValue_2];
    33.         else
    34.             // Hide number
    35.             SecondNumber.gameObject.SetActive(false);
    36.    
    37.         tempValue = (int)pointText % 10;
    38.         FirstNumber.sprite = NumbersSprites[tempValue];
    39.     }
    40.  
    41.  
    42.     void OnTriggerEnter2D (Collider2D other)
    43.     {
    44.         if (other.gameObject.tag == "Player")
    45.         {
    46.             pointText += 1;
    47.             Debug.Log ("The score should update +1");
    48.         }
    49.     }
    50.  
    51.  
    52.     void OnGUI ()
    53.     {
    54.         GUI.Label(new Rect(-100, 10, 600, 900),
    55.                   "Score : "+pointText);            
    56.     }
    57. }
    58.  
    I also can't use ''enable'' (also in the extra check there is ''enable'')

    2015-10-06_09-53-02.png
    click^^
     
  13. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    You are deactivate gameobject, and never activate it back. I have already said that you need additional check.
     
  14. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    but as I said:
    The script doesn't work like this.
     
  15. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    An answer already?
     
  16. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    Instead of enable you need use SetActive.
     
  17. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I can't use this:

    if (!ThirdNumber.SetActive) ThirdNumber.SetActive(true);
     
  18. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
  19. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
  20. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    First get gameObject from Image and then use activeSelf.
    Code (CSharp):
    1. img.gameObject.activeSelf
     
  21. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Sorry but everything is new to me. I don't really understand what you mean with ''First get gameObject from Image''. Please explain it to me;)
     
  22. DeuS

    DeuS

    Joined:
    Feb 8, 2013
    Posts:
    24
    Try to check this - http://unity3d.com/learn/tutorials/topics/scripting
    For example for activating gameobjects - http://unity3d.com/learn/tutorials/modules/beginner/scripting/activating-gameobjects?playlist=17117
    You have a Image(for example ThirdNumber) - http://docs.unity3d.com/ScriptReference/UI.Image.html
    In docs you see that Image has gameObject variable, you can get it - ThirdNumber.gameObject
    Then you check if this gameobject is active - ThirdNumber.gameObject.activeSelf
     
  23. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Code (CSharp):
    1.         int tempValue = (int)pointText / 100;
    2.         if (tempValue == 0)
    3.         {
    4.             if (ThirdNumber.gameObject.SetActive(false)) ThirdNumber.gameObject.SetActive(true);
    5.             ThirdNumber.sprite = NumbersSprites[tempValue];
    6.         }
    7.         else
    8.             // Hide number
    9.             ThirdNumber.gameObject.SetActive(false);
    10.  
    I did this but I got this error:

    098dddd722eef92ad51844b44883c8fb.png