Hey guys,
I was just wondering if anyone knew how to write a script that you could attach to a gui texture to make it blink or flash on and off continuously.
Thanks so much,
Johnny
since OnGUI function is a function that is being called by the engine just like the Update function. this must be something easy to acheive.
Try this:
private var blink = false;
private var counter:int = 0;
private var blinkSpeed:int = 10;
public var yourGUITexture:GUITexture;
function Update()
{
if(counter == blinkSpeed)
{
blink = true;
counter = 0;
}
else
blink = false;
counter++;
}
function OnGUI()
{
if(blink)
yourGUITexture.guiTexture.enabled = true;
else
yourGUITexture.guiTexture.enabled = false;
}
Now attach this script to an empty game object(or whatever) in your scene, and drag you
GUITexture to add to the public var âyourGUITextureâ.
I just tried , working
Ok sure,
Sorry. Iâm new to these forums and I donât have much experience.Thanks again though for your help.
You can use the Coroutines and new Unity 4.6 GUI to achieve this very easily. Check this article here
If you just need the code, here you go
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class FlashingTextScript : MonoBehaviour {
Text flashingText;
void Start(){
//get the Text component
flashingText = GetComponent<Text>();
//Call coroutine BlinkText on Start
StartCoroutine(BlinkText());
}
//function to blink the text
public IEnumerator BlinkText(){
//blink it forever. You can set a terminating condition depending upon your requirement
while(true){
//set the Text's text to blank
flashingText.text= "";
//display blank text for 0.5 seconds
yield return new WaitForSeconds(.5f);
//display âI AM FLASHING TEXTâ for the next 0.5 seconds
flashingText.text= "I AM FLASHING TEXT!";
yield return new WaitForSeconds(.5f);
}
}
}
P.S: Even though it seems to be an infinite loop which is generally considered as a bad programming practice, in this case it works quite well as the MonoBehaviour will be destroyed once the object is destroyed. Also, if you dont need it to flash forever, you can add a terminating condition based on your requirements.