Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

GUI Label - Blinking Text

Discussion in 'Extensions & OnGUI' 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,060
    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:
    48
    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:
    5
    Thanks, GluedBrain :)