103 lines
3.3 KiB
C#
103 lines
3.3 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using Lean.Pool;
|
||
using Sirenix.OdinInspector;
|
||
|
||
namespace SLSUtilities.Effects
|
||
{
|
||
public class LightGradient : MonoBehaviour, IPoolable
|
||
{
|
||
[Required] public Light targetLight;
|
||
|
||
[Tooltip("在特效生成时是否立刻启用渐变,注意,必须通过对象池生成。\n为true时,渐变效果从targetLight的默认intensity开始,否则需要调用EnableFade方法启用渐变")]
|
||
public bool playOnSpawn = true;
|
||
|
||
[Tooltip("光源渐变的生命周期")] public float life = 1f;
|
||
|
||
[Tooltip("是否应用强度变化")] public bool applyIntensityFade = true;
|
||
[Tooltip("初始强度")] [ShowIf("applyIntensityFade")] public float initialIntensity = 1;
|
||
[Tooltip("渐变曲线")] [ShowIf("applyIntensityFade")] public AnimationCurve intensityFadeCurve;
|
||
|
||
[Tooltip("是否应用范围变化")] public bool applyRangeFade = false;
|
||
[Tooltip("初始范围")][ShowIf("applyRangeFade")] public float initialRange = 1;
|
||
[Tooltip("范围曲线")][ShowIf("applyRangeFade")] public AnimationCurve rangeFadeCurve;
|
||
|
||
[Tooltip("是否使用光源颜色渐变")] public bool useLightColorGradient = false;
|
||
[Tooltip("光源颜色渐变")][ShowIf("useLightColorGradient")] public Gradient lightColorGradient;
|
||
|
||
[HideInEditorMode]
|
||
[SerializeField]
|
||
private bool isFading;
|
||
[HideInEditorMode]
|
||
[SerializeField]
|
||
private float time;
|
||
|
||
private void Reset()
|
||
{
|
||
targetLight = GetComponent<Light>();
|
||
intensityFadeCurve = AnimationCurve.EaseInOut(0, 1, 1, 0);
|
||
rangeFadeCurve = AnimationCurve.EaseInOut(0, 1, 1, 0);
|
||
}
|
||
|
||
public void OnSpawn()
|
||
{
|
||
if (playOnSpawn && !isFading)
|
||
{
|
||
EnableFade(initialIntensity, initialRange);
|
||
}
|
||
}
|
||
|
||
public void OnDespawn()
|
||
{
|
||
isFading = false;
|
||
}
|
||
|
||
public void EnableFade(float intensity, float initialRange)
|
||
{
|
||
if (targetLight == null) throw new NullReferenceException("Target Light is null");
|
||
|
||
time = 0;
|
||
isFading = true;
|
||
if (applyIntensityFade)
|
||
{
|
||
this.initialIntensity = intensity;
|
||
targetLight.intensity = intensity;
|
||
}
|
||
|
||
if (applyRangeFade)
|
||
{
|
||
this.initialRange = initialRange;
|
||
targetLight.range = initialRange;
|
||
}
|
||
}
|
||
|
||
|
||
void Update()
|
||
{
|
||
if (!isFading) return;
|
||
|
||
time += Time.deltaTime;
|
||
|
||
if (applyIntensityFade)
|
||
{
|
||
targetLight.intensity = initialIntensity * intensityFadeCurve.Evaluate(time / life);
|
||
}
|
||
|
||
if (applyRangeFade)
|
||
{
|
||
targetLight.range = initialRange * rangeFadeCurve.Evaluate(time / life);
|
||
}
|
||
|
||
if (useLightColorGradient)
|
||
{
|
||
targetLight.color = lightColorGradient.Evaluate(time / life);
|
||
}
|
||
|
||
if (time > life)
|
||
{
|
||
time = 0;
|
||
isFading = false;
|
||
}
|
||
}
|
||
}
|
||
} |