- Home /
how Can I make a UI Image Blink On And Off
I'm trying to make a UI turn signal indicator using a UI image that I want to blink on and off with a click, click sound. I can do it with an animation but is there a C# method that is a better way to do this so that when the game object is set active it will be blinking and making the classic "Click, Click" sound of a turn signal indicator?
Answer by wilmer_lin · Dec 15, 2015 at 10:07 PM
I did something similar and made a Blink.cs class. Then attached it to whatever UI object I wanted to blink. Change the timing with the interval, etc. In this example, I just used AudioSource.PlayClipAtPoint to play your specified AudioClip at world origin.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
// this toggles a component (usually an Image or Renderer) on and off for an interval to simulate a blinking effect
public class Blink : MonoBehaviour {
// this is the UI.Text or other UI element you want to toggle
public MaskableGraphic imageToToggle;
public float interval = 1f;
public float startDelay = 0.5f;
public bool currentState = true;
public bool defaultState = true;
bool isBlinking = false;
public AudioClip clip;
void Start()
{
imageToToggle.enabled = defaultState;
StartBlink();
}
public void StartBlink()
{
// do not invoke the blink twice - needed if you need to start the blink from an external object
if (isBlinking)
return;
if (imageToToggle !=null)
{
isBlinking = true;
InvokeRepeating("ToggleState", startDelay, interval);
}
}
public void ToggleState()
{
imageToToggle.enabled = !imageToToggle.enabled;
// plays the clip at (0,0,0)
if (clip)
AudioSource.PlayClipAtPoint(clip,Vector3.zero);
}
}
Wondering, isn't this missing a way to stop the blinking?
@joseGuate97 the way I would have done this was use a update void where if whichever button pressed, blink.enabled = false; This code wont allow the blinking to start again but it can be repaired by usages of bools
Answer by RadioKnife · Dec 15, 2015 at 10:02 PM
You can use Coroutine to achieve this. It would go something like this
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BlinkingImage : MonoBehaviour {
Image image;
void Start ()
{
image = GetComponent<Image>();
StartBlinking();
}
IEnumerator Blink()
{
while (true)
{
switch(image.color.a.ToString())
{
case "0":
image.color = new Color(image.color.r, image.color.g, image.color.b, 1);
//Play sound
yield return new WaitForSeconds(0.5f);
break;
case "1":
image.color = new Color(image.color.r, image.color.g, image.color.b, 0);
//Play sound
yield return new WaitForSeconds(0.5f);
break;
}
}
}
void StartBlinking()
{
StopAllCoroutines();
StartCoroutine("Blink");
}
void StopBlinking()
{
StopAllCoroutines();
}
}
Just a comment: I tried both methods, and prefer the first answer for the following reasons:
I have other Coroutines in my code, so I changed the above "StopAllCoroutines()" to "StopCoroutine(myblink)" only, so the others can still run.
If you're flashing multiple images, the IEnumerator doesn't handle the ti$$anonymous$$g properly, such that the first image listed will flash correctly, but since it's a constant loop the other two don't pick up the ti$$anonymous$$g. I'm pretty new to C# so I may be doing something that's not the most efficient. But the first answer works like a charm!!!
Answer by LordDarkon76 · Dec 15, 2015 at 09:59 PM
You can start a Coroutine, that toggle the alpha of your object and reproduce the sound then wait the time that you need.
Answer by KnightRiderGuy · Dec 16, 2015 at 03:26 PM
Thanks @wilmer_lin and @RadioKnife Both of these were excellent answers and I loved them both. I ultimately could only choose one answer as the correct one even though they are both correct and both work. I ended up choosing the top answer simply because of the ease of use with the inspector values of being able to drag and drop and tweak parameters in the inspector.
I awarded points to both of you though because such help was well deserved on both parts, Much MUCH thanks guys :)
Your answer
Follow this Question
Related Questions
Destroy UI Image 1 Answer
Sprite image not showing up when assigned to Texture2D 0 Answers
Health Bar Messages 3 Answers
2D Image not appearing in game but only in scene 1 Answer
Unity to Check Arduino LED Status 0 Answers