Search Unity

Flashing Image / Texture

Discussion in 'Scripting' started by Cosmas, Apr 5, 2010.

  1. Cosmas

    Cosmas

    Joined:
    Jan 9, 2010
    Posts:
    133
    Hey all,

    I'm trying to flash a simple PNG with an alpha on and off a few times once an event occurs, but I don't quite understand the logic.

    Would I need to black out the entire alpha every lets say 1 second?

    And since I'm basing this off of time, what function should I use?

    Thanks
     
  2. jayde

    jayde

    Joined:
    Jan 19, 2010
    Posts:
    27
    Anytime you want to do something with timing like this, coroutines are your best friend. You can do something like this with the code:
    Code (csharp):
    1.  
    2. IEnumerator Flash(int flashCount)
    3. {
    4.     for(int i=0; i < flashCount; i++)
    5.     {
    6.         renderer.material.color = Color.white;
    7.         yield return new WaitForSeconds(1);
    8.         renderer.material.color = Color.clear;
    9.         yield return new WaitForSeconds(1);
    10.     }
    11. }
    And then you simply fire this off in your code by calling
    Code (csharp):
    1. StartCoroutine(Flash(YOURNUMBER));
     
  3. Cosmas

    Cosmas

    Joined:
    Jan 9, 2010
    Posts:
    133
    Code (csharp):
    1. if(p1shots == 0)
    2. {
    3. GUI.Label(new Rect(halfWidth - 184, halfHeight - 27, reloadImg.width, reloadImg.height), reloadImg);
    4. }
    At the moment I'm simply creating a Label on the fly.
    So.. with a coroutine, I would need to create, then destroy the object every 1 second? Or can I easily manipulate the transparency.
     
  4. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. Color setAlpha = new Color(1f, 0f, 0f, 0f);
    2.  
    3. private void Update()
    4.     {
    5.         StartCoroutine(BloodSplatter());
    6.     }
    7.  
    8. private IEnumerator BloodSplatter()
    9.     {
    10.         if (damaged)
    11.         {
    12.             setAlpha.a = 0.2f;
    13.             damageImage.color = setAlpha;
    14.            
    15.             yield return new WaitForSeconds(0.1f);
    16.             damaged = false;
    17.         }
    18.         else
    19.         {
    20.             setAlpha.a = 0f;
    21.             damageImage.color = setAlpha;
    22.         }
    23.     }
    This should work perfectly fine.... If any more questions, post them here