123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
-
- // =================================
- // Namespaces.
- // =================================
- using UnityEngine;
- // =================================
- // Define namespace.
- // =================================
- namespace MirzaBeig
- {
- namespace ParticleSystems
- {
- // =================================
- // Classes.
- // =================================
-
- [RequireComponent(typeof(Light))]
- public class AnimatedLight : MonoBehaviour
- {
- // =================================
- // Nested classes and structures.
- // =================================
- // ...
- // =================================
- // Variables.
- // =================================
- // ...
- new Light light;
- public float time { get; set; }
- public float duration = 1.0f;
- bool evaluating = true;
- public Gradient colourOverLifetime;
- public AnimationCurve intensityOverLifetime = new AnimationCurve(
- new Keyframe(0.0f, 0.0f),
- new Keyframe(0.5f, 1.0f),
- new Keyframe(1.0f, 0.0f));
- // ...
- public bool loop = true;
- public bool autoDestruct = false;
- // ...
- Color startColour;
- float startIntensity;
- // =================================
- // Functions.
- // =================================
- // ...
- void Awake()
- {
- light = GetComponent<Light>();
- }
- // ...
- void Start()
- {
- startColour = light.color;
- startIntensity = light.intensity;
- light.color = startColour * colourOverLifetime.Evaluate(0.0f);
- light.intensity = startIntensity * intensityOverLifetime.Evaluate(0.0f);
- }
- // ...
- void OnEnable()
- {
- }
- void OnDisable()
- {
- // Reset for next OnEnable if required.
- light.color = startColour;
- light.intensity = startIntensity;
- time = 0.0f;
- evaluating = true;
- light.color = startColour * colourOverLifetime.Evaluate(0.0f);
- light.intensity = startIntensity * intensityOverLifetime.Evaluate(0.0f);
- }
- // ...
- void Update()
- {
- if (evaluating)
- {
- if (time < duration)
- {
- time += Time.deltaTime;
- if (time > duration)
- {
- if (autoDestruct)
- {
- Destroy(gameObject);
- }
- else if (loop)
- {
- time = 0.0f;
- }
- else
- {
- time = duration;
- evaluating = false;
- }
- }
- }
- // ...
- if (time <= duration)
- {
- float normalizedTime = time / duration;
- light.color = startColour * colourOverLifetime.Evaluate(normalizedTime);
- light.intensity = startIntensity * intensityOverLifetime.Evaluate(normalizedTime);
- }
- }
- }
- // =================================
- // End functions.
- // =================================
- }
- // =================================
- // End namespace.
- // =================================
- }
- }
- // =================================
- // --END-- //
- // =================================
|