// Update is called once per frame void Update() { // If we are currently applying a tint... if (_tintState != TintFlashState.NoTint) { // Modify it by the current rate of change and apply it _tintColor.a = Mathf.Clamp01(_tintColor.a + _tintSpeed * Time.deltaTime); _material.SetColor(TintProperty, _tintColor); } switch (_tintState) { case TintFlashState.IncreasingTint: // If we are currently increasing the tint, stop when it reaches 1 if (_tintColor.a >= 1) { // When it has reached 1, start decreasing the tint again _tintState = TintFlashState.RemovingTint; _tintSpeed = -_tintSpeed; } break; case TintFlashState.RemovingTint: // If we are currently decreasing the tint, stop when it reaches 0 if (_tintColor.a <= 0) { _tintState = TintFlashState.NoTint; _tintSpeed = 0; } break; } }
/** * Applies a tint for a fixed duration. The intensity of the tint will rise to maximum until the half time of the * duration. Afterwards it decreases to zero. */ public void FlashTint(Color tintColor, float duration) { _tintColor = tintColor; _tintColor.a = 0; // The alpha value controls the intensity of the tint in this shader. Start with no tint. // Compute a rate of change of tint so that maximum tint is reached halfway of the duration. _tintSpeed = 1 / (duration / 2); _tintState = TintFlashState.IncreasingTint; }