MayHeCome/Assets/三问c#/CircadianSystem.cs
2024-12-18 17:55:34 +08:00

56 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}