/** * --- FADE --- */ public void FadeOut(GameObject GO, float time, bool shouldDestroyAfterFadeout = false) { if (animatedGOs.ContainsKey(GO) == true) { return; } if (GO == null) { return; } Renderer[] rendererComponents = GO.GetComponentsInChildren <Renderer>(true); foreach (Renderer renderer in rendererComponents) { ColorRenderer colorRenderer = new ColorRenderer(); if (colorRenderer.SetRenderer(renderer) == true) { colorRenderer.SetColor(new Color(colorRenderer.GetColor().r, colorRenderer.GetColor().g, colorRenderer.GetColor().b, 1f)); } } animatedGOs.Add(GO, this.StartCoroutine(FadeOutCoroutine(GO, time, shouldDestroyAfterFadeout))); }
IEnumerator FadeOutCoroutine(GameObject GO, float time, bool shouldDestroy = false) { // store here all renderers with their initial colors of the object and its children Dictionary <ColorRenderer, float> renderers = new Dictionary <ColorRenderer, float>(); // add all renderers of the object and its children Renderer[] rendererComponents = GO.GetComponentsInChildren <Renderer>(true); foreach (Renderer renderer in rendererComponents) { ColorRenderer colorRenderer = new ColorRenderer(); if (colorRenderer.SetRenderer(renderer) == true) { renderers.Add(colorRenderer, colorRenderer.GetColor().a); } } // animated all renderers from for (float currTime = time; currTime >= 0; currTime -= Time.deltaTime) { foreach (ColorRenderer cr in renderers.Keys) { Color c = cr.GetColor(); c.a = ConvertRangeClamped(0, time, 0, renderers[cr], currTime); cr.SetColor(c); } yield return(null); } // fade out 100% foreach (ColorRenderer cr in renderers.Keys) { Color c = cr.GetColor(); c.a = 0; cr.SetColor(c); } animatedGOs.Remove(GO); if (shouldDestroy == true) { Destroy(GO); } }
IEnumerator BlinkCoroutine(GameObject GO, Color targetColor, float time) { Color targetColorCopy = new Color(targetColor.r, targetColor.g, targetColor.b, targetColor.a); // store here all renderers with their initial colors of the object and its children Dictionary <ColorRenderer, Color> renderers = new Dictionary <ColorRenderer, Color>(); // add all renderers of the object and its children Renderer[] rendererComponents = GO.GetComponentsInChildren <Renderer>(); foreach (Renderer renderer in rendererComponents) { ColorRenderer colorRenderer = new ColorRenderer(); if (colorRenderer.SetRenderer(renderer) == true) { renderers.Add(colorRenderer, colorRenderer.GetColor()); colorRenderer.SetColor(targetColorCopy); } } // animated all renderers from for (float currTime = time; currTime >= 0; currTime -= Time.deltaTime) { float normalizedTime = ConvertRangeClamped(0, time, 0, 1, currTime); foreach (ColorRenderer cr in renderers.Keys) { Color origColor = renderers[cr]; cr.SetColor(Color.Lerp(origColor, targetColor, normalizedTime)); } yield return(null); } // restore to original color 100% foreach (ColorRenderer cr in renderers.Keys) { Color origColor = renderers[cr]; cr.SetColor(origColor); } animatedGOs.Remove(GO); }