// Fade the sprite to fully transparent public void FadeToTransparent(float time) { if (time <= float.Epsilon) // imediate { SetActive(false); _alphaMode = eAlphaMode.transparent; return; } switch (_alphaMode) { case eAlphaMode.fadingToTransparent: // we are already fading to opaque _startingFadeTime = CalculateFakeStartTime(_fadeLength, time); break; case eAlphaMode.fadingToOpaque: _alphaMode = eAlphaMode.fadingToTransparent; // we're fading to transparent now // here, as we're mid-way through a fade, we're faking that the fade started in the past so we get no jump in alpha value _startingFadeTime = CalculateFakeStartTimeForDirectionChange(_fadeLength, time); break; case eAlphaMode.opaque: _alphaMode = eAlphaMode.fadingToTransparent; // we're fading to transparent _startingFadeTime = Time.time; break; default: break; } _fadeLength = time; }
public void Update() { if (IsActive()) { float newAlpha = ((Time.time - _startingFadeTime) / _fadeLength); switch (_alphaMode) { case eAlphaMode.fadingToTransparent: newAlpha = 1f - newAlpha; if (newAlpha <= 0f) { SetActive(false); _alphaMode = eAlphaMode.transparent; } else { _sprite.alpha = newAlpha; } break; case eAlphaMode.fadingToOpaque: if (newAlpha > 1f) { newAlpha = 1f; _alphaMode = eAlphaMode.opaque; } _sprite.alpha = newAlpha; break; default: break; } } }