最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

【Unity,C#】控制方向光模拟昼夜变化的脚本

IT圈 admin 12浏览 0评论

【Unity,C#】控制方向光模拟昼夜变化的脚本

Unity.C#.模拟昼夜变化的脚本

效果

如何实现

  • 创建TextPro用于实时显示时间

  • 简单配置

  • 创建空对象

  • 加入脚本

  • 脚本代码
using System;
using TMPro;
using UnityEngine;public class TimeController : MonoBehaviour
{/// <summary>/// 显示24小时制时间/// </summary>[SerializeField]private TextMeshProUGUI txtTime;/// <summary>/// 定向光/// </summary>[SerializeField]private Light sunLight;/// <summary>/// 时间的增长步长/// </summary>[SerializeField]private float timeStep = 1000;/// <summary>/// 起始小时/// </summary>[SerializeField]private int startHour = 12;/// <summary>/// 日升小时/// </summary>[SerializeField]private int sunriseHour = 6;/// <summary>/// 日落小时/// </summary>[SerializeField]private int sunsetHour = 18;private DateTime currentTime;private TimeSpan sunriseTime;private TimeSpan sunsetTime;void Start(){currentTime = DateTime.Now.Date + TimeSpan.FromHours(startHour);sunriseTime = TimeSpan.FromHours(sunriseHour);sunsetTime = TimeSpan.FromHours(sunsetHour);}void Update(){updateTime();updateDirectionLight();}/// <summary>/// 更新光照角度/// </summary>private void updateDirectionLight(){float lightDegree = 0f;if (currentTime.TimeOfDay > sunriseTime && currentTime.TimeOfDay < sunsetTime)//白天{TimeSpan sunrise2SunsetInterval = getTimeInterval(sunriseTime, sunsetTime);TimeSpan sunrise2CurrentInterval = getTimeInterval(sunriseTime, currentTime.TimeOfDay);//算出比率:当前时间差和总时间double percentage = sunrise2CurrentInterval.TotalMilliseconds / sunrise2SunsetInterval.TotalMilliseconds;lightDegree = Mathf.Lerp(0, 180, (float)percentage);}else//夜晚{TimeSpan sunset2SunriseInterval = getTimeInterval(sunsetTime, sunriseTime);TimeSpan sunset2CurrentInterval = getTimeInterval(sunsetTime, currentTime.TimeOfDay);//算出比率:当前时间差和总时间double percentage = sunset2CurrentInterval.TotalMilliseconds / sunset2SunriseInterval.TotalMilliseconds;lightDegree = Mathf.Lerp(180, 360, (float)percentage);}//根据当前时间, 将表示日光的定向光旋转到对应的角度,围绕X轴旋转sunLight.transform.rotation = Quaternion.AngleAxis(lightDegree, Vector3.right);}/// <summary>/// 计算时间差/// </summary>/// <param name="firstTime"></param>/// <param name="secondTime"></param>/// <returns></returns>private TimeSpan getTimeInterval(TimeSpan firstTime, TimeSpan secondTime){TimeSpan interval = secondTime - firstTime;if (interval.TotalSeconds < 0){interval += TimeSpan.FromHours(24);}return interval;}/// <summary>/// 更新时间/// </summary>private void updateTime(){currentTime = currentTime.AddSeconds(Time.deltaTime * timeStep);if (txtTime != null){txtTime.text = currentTime.ToString("HH:mm");}}}

【Unity,C#】控制方向光模拟昼夜变化的脚本

Unity.C#.模拟昼夜变化的脚本

效果

如何实现

  • 创建TextPro用于实时显示时间

  • 简单配置

  • 创建空对象

  • 加入脚本

  • 脚本代码
using System;
using TMPro;
using UnityEngine;public class TimeController : MonoBehaviour
{/// <summary>/// 显示24小时制时间/// </summary>[SerializeField]private TextMeshProUGUI txtTime;/// <summary>/// 定向光/// </summary>[SerializeField]private Light sunLight;/// <summary>/// 时间的增长步长/// </summary>[SerializeField]private float timeStep = 1000;/// <summary>/// 起始小时/// </summary>[SerializeField]private int startHour = 12;/// <summary>/// 日升小时/// </summary>[SerializeField]private int sunriseHour = 6;/// <summary>/// 日落小时/// </summary>[SerializeField]private int sunsetHour = 18;private DateTime currentTime;private TimeSpan sunriseTime;private TimeSpan sunsetTime;void Start(){currentTime = DateTime.Now.Date + TimeSpan.FromHours(startHour);sunriseTime = TimeSpan.FromHours(sunriseHour);sunsetTime = TimeSpan.FromHours(sunsetHour);}void Update(){updateTime();updateDirectionLight();}/// <summary>/// 更新光照角度/// </summary>private void updateDirectionLight(){float lightDegree = 0f;if (currentTime.TimeOfDay > sunriseTime && currentTime.TimeOfDay < sunsetTime)//白天{TimeSpan sunrise2SunsetInterval = getTimeInterval(sunriseTime, sunsetTime);TimeSpan sunrise2CurrentInterval = getTimeInterval(sunriseTime, currentTime.TimeOfDay);//算出比率:当前时间差和总时间double percentage = sunrise2CurrentInterval.TotalMilliseconds / sunrise2SunsetInterval.TotalMilliseconds;lightDegree = Mathf.Lerp(0, 180, (float)percentage);}else//夜晚{TimeSpan sunset2SunriseInterval = getTimeInterval(sunsetTime, sunriseTime);TimeSpan sunset2CurrentInterval = getTimeInterval(sunsetTime, currentTime.TimeOfDay);//算出比率:当前时间差和总时间double percentage = sunset2CurrentInterval.TotalMilliseconds / sunset2SunriseInterval.TotalMilliseconds;lightDegree = Mathf.Lerp(180, 360, (float)percentage);}//根据当前时间, 将表示日光的定向光旋转到对应的角度,围绕X轴旋转sunLight.transform.rotation = Quaternion.AngleAxis(lightDegree, Vector3.right);}/// <summary>/// 计算时间差/// </summary>/// <param name="firstTime"></param>/// <param name="secondTime"></param>/// <returns></returns>private TimeSpan getTimeInterval(TimeSpan firstTime, TimeSpan secondTime){TimeSpan interval = secondTime - firstTime;if (interval.TotalSeconds < 0){interval += TimeSpan.FromHours(24);}return interval;}/// <summary>/// 更新时间/// </summary>private void updateTime(){currentTime = currentTime.AddSeconds(Time.deltaTime * timeStep);if (txtTime != null){txtTime.text = currentTime.ToString("HH:mm");}}}
发布评论

评论列表 (0)

  1. 暂无评论