1.AnimationCurve介绍
AnimationCurve是Unity3D里一个非常实用的功能。作用是编辑一条任意变化的曲线用在任何你想用在的地方。 如曲线地形,曲线轨迹等。
本篇利用AnimationCurve模拟闪电闪烁效果
2.视频效果
3.代码
面板上的AnimCurve就是自定义的动画曲线
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CastleLightning : MonoBehaviour
{
[Header("动画曲线")] public AnimationCurve animCurve;
[Header("场景灯")] public Light sceneLight;
[Header("黑天亮度")] public float darkLight=0.2f;
[Header("黑天持续时间")] public float darkTime=10f;
private float animDur;
private bool isDark = false;
void Start()
{
animDur = animCurve.keys[animCurve.length - 1].time;
}
private float curDarkTime = 0;
private float curveTime = 0;
private float curveValue = 0;
void Update()
{
if (!isDark)
{
curveTime += Time.deltaTime;
// 获取 AnimationCurve 中对应时间的值
curveValue = animCurve.Evaluate(curveTime);
// 应用亮度到目标灯光
sceneLight.intensity = curveValue;
if (curveTime >= animDur)
{
curveTime = 0;
//进入黑夜
isDark = true;
sceneLight.intensity = darkLight;
}
}
else
{
curDarkTime += Time.deltaTime;
if (curDarkTime > darkTime)
{
curDarkTime = 0;
isDark = false;
}
}
}
}