Liberi
An exergame built for kids with CP!
Blinker.cs
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using Janus;
5 using System.Linq;
6 using System.Reflection;
7 using System;
8 
12 [AddComponentMenu("Liberi/Blinker")]
13 [Script(ScriptRole.View)]
14 public class Blinker : MonoBehaviour
15 {
19  public float BlinkInterval = .04f;
23  public Renderer[] Unblinkables;
27  public List<SpriteRenderer> ExtraBlinkables = new List<SpriteRenderer>();
31  public bool FlashRed;
32 
33  float _blinkTimeLeft;
34 
38  public void Blink (float duration)
39  {
40  if (FlashRed)
41  StartCoroutine(FlashRedAsync(duration));
42  else
43  StartCoroutine(BlinkAsync(duration));
44  }
45 
49  public void StopBlinking ()
50  {
51  _blinkTimeLeft = 0f;
52  }
53 
54  IEnumerator BlinkAsync (float duration)
55  {
56  if (duration < BlinkInterval)
57  yield break;
58 
59  bool blinkState = false;
60 
61  _blinkTimeLeft = Mathf.Max(_blinkTimeLeft, duration);
62 
63  // We only blink renderers that are enabled before the blinking starts.
64  var blinkables = GetComponentsInChildren<Renderer>()
65  .Except(Unblinkables)
66  .Concat(ExtraBlinkables.ToArray())
67  .Where(rend => rend.enabled).ToArray();
68 
69  while (_blinkTimeLeft > 0f)
70  {
71  foreach (var rend in blinkables)
72  {
73  if (rend == null)
74  continue;
75  rend.enabled = blinkState;
76  }
77 
78  blinkState = !blinkState;
79  _blinkTimeLeft -= BlinkInterval;
80 
81  yield return new WaitForSeconds(BlinkInterval);
82  }
83 
84  foreach (var rend in blinkables)
85  {
86  if (rend == null)
87  continue;
88 
89  rend.enabled = true;
90  }
91  }
92 
93  IEnumerator FlashRedAsync (float duration)
94  {
95  if (duration < BlinkInterval)
96  yield break;
97 
98  Color blinkColor = Color.white;
99 
100  _blinkTimeLeft = Mathf.Max(_blinkTimeLeft, duration);
101 
102  SpriteRenderer[] blinkables = GetComponentsInChildren<SpriteRenderer>()
103  .Except(Unblinkables.Select(rend => (SpriteRenderer)rend))
104  .Concat(ExtraBlinkables.Select(rend => (SpriteRenderer)rend)).ToArray();
105 
106  Color originalColor = blinkables[0].color;
107 
108  while (_blinkTimeLeft > 0f)
109  {
110  foreach (var rend in blinkables)
111  {
112  if (rend == null)
113  continue;
114  rend.color = blinkColor;
115  }
116 
117  blinkColor = blinkColor == originalColor ? Color.red : originalColor;
118  _blinkTimeLeft -= BlinkInterval;
119 
120  yield return new WaitForSeconds(BlinkInterval);
121  }
122 
123  foreach (var rend in blinkables)
124  {
125  if (rend == null)
126  continue;
127 
128  rend.color = originalColor;
129  }
130  }
131 }
List< SpriteRenderer > ExtraBlinkables
Targeted SpriteRenderers that are not necesarily children, but want to blink with this blinker...
Definition: Blinker.cs:27
void Blink(float duration)
Blink the object for the given duration.
Definition: Blinker.cs:38
void StopBlinking()
Stop blinking the object. This resets the visibility of all renderers.
Definition: Blinker.cs:49
bool FlashRed
Flash SpriteRenderers Color to red instead of disabling renderers.
Definition: Blinker.cs:31
float BlinkInterval
The interval at which renderer visibility toggles.
Definition: Blinker.cs:19
Blinks the renderers in an object on and off. Good for damage effects.
Definition: Blinker.cs:14
Renderer[] Unblinkables
An array of renderers to exclude from the blink effect.
Definition: Blinker.cs:23