56 lines
1.3 KiB
C#
56 lines
1.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.Rendering.Universal;
|
||
public class CircadianSystem : MonoBehaviour
|
||
{
|
||
private Light2D globalLight; // 2D 全局光源
|
||
public Gradient lightColor; // 灯光颜色随时间变化
|
||
[Header("范围1-4")]
|
||
public AnimationCurve lightIntensity; // 灯光强度曲线
|
||
|
||
[Header("模拟时间")]
|
||
[Range(0, 24)] public float currentTime = 12f; // 当前时间(0-24小时)
|
||
[Header("一天的时长(秒)")]
|
||
public float dayDuration = 24f;
|
||
|
||
private float timeMultiplier
|
||
{
|
||
get { return 24f / dayDuration; }
|
||
}
|
||
private void Awake()
|
||
{
|
||
globalLight = transform.Find("全局光").GetComponent<Light2D>();
|
||
}
|
||
void Start()
|
||
{
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
UpdateTime();
|
||
UpdateLighting();
|
||
}
|
||
|
||
// 更新时间
|
||
private void UpdateTime()
|
||
{
|
||
currentTime += Time.deltaTime * timeMultiplier;
|
||
if (currentTime >= 24f) currentTime = 0f; // 时间循环
|
||
}
|
||
|
||
// 更新灯光状态
|
||
private void UpdateLighting()
|
||
{
|
||
float normalizedTime = currentTime / 24f; // 归一化时间(0-1)
|
||
|
||
// 设置灯光颜色
|
||
globalLight.color = lightColor.Evaluate(normalizedTime);
|
||
|
||
// 设置灯光强度
|
||
globalLight.intensity = lightIntensity.Evaluate(normalizedTime*4);
|
||
}
|
||
}
|
||
|
||
|