Search Unity

GUI Label - Blinking Text

Discussion in 'Immediate Mode GUI (IMGUI)' started by gr33nl1nk, Jan 30, 2009.

  1. gr33nl1nk

    gr33nl1nk

    Joined:
    Dec 19, 2008
    Posts:
    92
    How do I make blinking text with GUI Label? or is it easier to do it with an image? if I intend to use GUI Label (say, for localization purpose), do I have to script it manually or there is a function which make life easier? thanks.
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Use a boolean and a coroutine to flip it on and off.

    Code (csharp):
    1.  
    2. //Warning: Forum Code!
    3.  
    4. private var displayLabel = false;
    5.  
    6. function Start() {
    7. FlashLabel();
    8. }
    9.  
    10. function FlashLabel() {
    11.    
    12. // Fancy pants flash of label on and off   
    13. while (1) {
    14.  displayLabel = true;
    15.  yield WaitForSeconds(.5);
    16.  displayLabel = false;
    17.  yield WaitForSeconds(.5); 
    18. }
    19.  
    20. }
    21.  
    22. function OnGUI() {
    23.  
    24.  if (displayLabel == true)
    25.  GUILayout.Label("I AM FLASHING");
    26.  
    27. }
    28.  
    29.  
    Something like that. Being able to drive your GUI with an in-line state machine is flexible and powerful. More I use the Unity GUI system the more it grows on me.
     
    julien-conan likes this.
  3. gr33nl1nk

    gr33nl1nk

    Joined:
    Dec 19, 2008
    Posts:
    92
    cooooolll, thank you!
     
  4. mmangual_83

    mmangual_83

    Joined:
    Nov 14, 2013
    Posts:
    8
    Thanks a lot man it works!!!
     
  5. Mai Kel

    Mai Kel

    Joined:
    Nov 5, 2013
    Posts:
    14
    I normally do this with a timer and comparing the module.
    Code (CSharp):
    1. int t=0;
    2. ...
    3.        
    4. void OnGUI(){
    5.     t++;
    6.                 if (t % 100 < 50) {
    7.                         GUI.Label (new Rect (Screen.width / 2, Screen.height / 1.4f, 100, 100), "PRESS START", startSt);
    8.                 }
    9. }
     
  6. GluedBrain

    GluedBrain

    Joined:
    Apr 12, 2013
    Posts:
    47
    You can use the Coroutines and new Unity 4.6 GUI to achieve this very easily. Check this article here

    [Blinking Text - TGC][1]


    [1]: http://www.thegamecontriver.com/2015/01/flashing-blinking-text-unity-46-new-gui.html

    If you just need the code, here you go

    Code (CSharp):
    1.   using UnityEngine;
    2.   using System.Collections;
    3.   using UnityEngine.UI;
    4.   using UnityEngine.EventSystems;
    5.  
    6.   public class FlashingTextScript : MonoBehaviour {
    7.  
    8.   Text flashingText;
    9.  
    10.   void Start(){
    11.   //get the Text component
    12.   flashingText = GetComponent<Text>();
    13.   //Call coroutine BlinkText on Start
    14.   StartCoroutine(BlinkText());
    15.   }
    16.  
    17.   //function to blink the text
    18.   public IEnumerator BlinkText(){
    19.   //blink it forever. You can set a terminating condition depending upon your requirement
    20.   while(true){
    21.   //set the Text's text to blank
    22.   flashingText.text= "";
    23.   //display blank text for 0.5 seconds
    24.   yield return new WaitForSeconds(.5f);
    25.   //display “I AM FLASHING TEXT” for the next 0.5 seconds
    26.   flashingText.text= "I AM FLASHING TEXT!";
    27.   yield return new WaitForSeconds(.5f);
    28.   }
    29.   }
    30.  
    31.   }
     
  7. julien-conan

    julien-conan

    Joined:
    Jun 20, 2015
    Posts:
    8
    Thanks, GluedBrain :)
     
  8. TwoGether

    TwoGether

    Joined:
    Apr 6, 2015
    Posts:
    3
    Thanks @GluedBrain - just used your method you mentioned and it worked like a charm:

    For those who want to see a different script - here is mine and how I used it

    So when player dies, the text I entered in the Inspector for _gameOverText
    [SerializeField]
    private Text _gameOverText;

    Will appear and start blinking every 0.5 seconds


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class UIManager : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private Text _scoreText;
    10.     [SerializeField]
    11.     private Text _gameOverText;
    12.  
    13.     public static int scoreValue = 0;//when you start game, score is 0
    14.     [SerializeField]
    15.     public Image _LivesImg;
    16.     [SerializeField]
    17.     private Sprite[] _liveSprites;
    18.  
    19.     private void Start()
    20.     {
    21.         _gameOverText.gameObject.SetActive(false);
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         _scoreText.text = "Score: " + scoreValue;
    27.     }
    28.  
    29.     public void UpdateLives(int currentLives)
    30.     {
    31.         _LivesImg.sprite = _liveSprites[currentLives];
    32.         if (currentLives == 0)
    33.         {
    34.             StartCoroutine(displayGameOver());
    35.         }
    36.     }
    37.  
    38.     IEnumerator displayGameOver()
    39.     {
    40.         while (true)
    41.         {
    42.             _gameOverText.gameObject.SetActive(true);
    43.             yield return new WaitForSeconds(.5f);
    44.             _gameOverText.gameObject.SetActive(false);
    45.             yield return new WaitForSeconds(.5f);
    46.             //break;
    47.         }
    48.     }
    49. }
    50.  
     
  9. SuleimanAbdullah

    SuleimanAbdullah

    Joined:
    Mar 1, 2019
    Posts:
    5
    Thank you GluedBrain