Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Light2D simple intensity tween
#region MIT License
// # Released under MIT License
//
// Copyright (c) 2021 Byron Wright.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System.Collections;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
namespace RowdyRobot.Utils
{
/// <summary>
/// This behavior controls the intensity of a Light2D component using the defined parameters.
/// </summary>
[RequireComponent(typeof(Light2D))]
public class LightController : MonoBehaviour
{
[SerializeField]
private Light2D lightToControl;
[Header("Controls")]
[Tooltip("The starting intensity of the light to animate from.")]
[SerializeField]
private float fromIntensity;
[Tooltip("The intensity you want the light to animate to.")]
[SerializeField]
private float toIntensity = 1.0f;
[Tooltip("Defaults to linear. Values are relative to the from/to intensity.")]
[SerializeField]
private AnimationCurve lightIntensityAnimationCurve = new AnimationCurve
(
new Keyframe(0.0f, 0.0f),
new Keyframe(1.0f, 1.0f)
);
[Tooltip("How long it should take for the light's intensity to reach \"toIntensity\".")]
[SerializeField]
private float animationDuration = 1.0f;
[Tooltip("Should the animate play immediately. You will need to call \"StartAnimationIntensity()\" from script if disabled.")]
[SerializeField]
private bool startOnWake = true;
private Coroutine _animationCoroutine;
private void Awake()
{
if (lightToControl != null)
{
return;
}
// if a light hasn't been assigned through the editor we grab the one attached.
lightToControl = GetComponent<Light2D>();
}
private void OnEnable()
{
if (startOnWake)
{
SwitchOn();
}
}
public void SwitchOn()
{
// if called multiple times, reset it.
StopIntensityAnimation();
_animationCoroutine = StartCoroutine(AnimateIntensity());
}
public void SwitchOff()
{
StopIntensityAnimation();
_animationCoroutine = StartCoroutine(ReverseIntensityAnimation());
}
/// <summary>
/// Stop an already running animation.
/// </summary>
public void StopIntensityAnimation()
{
if (_animationCoroutine != null)
{
StopCoroutine(_animationCoroutine);
_animationCoroutine = null;
}
}
private IEnumerator ReverseIntensityAnimation()
{
var timeElapsed = animationDuration;
var deltaIntensity = toIntensity - fromIntensity;
while (timeElapsed > 0.0f)
{
var intensity =
lightIntensityAnimationCurve.Evaluate(timeElapsed / animationDuration) * deltaIntensity +
fromIntensity;
lightToControl.intensity = intensity;
timeElapsed -= Time.deltaTime;
yield return null;
}
lightToControl.intensity = fromIntensity;
}
private IEnumerator AnimateIntensity()
{
var timeElapsed = 0.0f;
var deltaIntensity = toIntensity - fromIntensity;
while (timeElapsed < animationDuration)
{
var intensity =
lightIntensityAnimationCurve.Evaluate(timeElapsed / animationDuration) * deltaIntensity +
fromIntensity;
lightToControl.intensity = intensity;
timeElapsed += Time.deltaTime;
yield return null;
}
lightToControl.intensity = toIntensity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment