Unity遊戲時間管理類、光照隨時間變化
阿新 • • 發佈:2020-12-16
技術標籤:Unity2D教程
遊戲的時間與系統時間肯定是不一樣的,我採取的是現實世界的一秒鐘為遊戲內的一分鐘。
實際維護上一次儲存的遊戲時間和載入存檔的系統時間,那麼當前的遊戲世界就是【當前的系統時間-載入存檔的系統時間】的秒數作為分鐘數加到儲存的遊戲時間即可。
/*
* Author : Jk_Chen
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameTime
{
public int Day, Hour, Minute;
public GameTime() { }
public GameTime(int Day,int Hour,int Minute) {
this.Day = Day;
this.Hour = Hour;
this.Minute = Minute;
}
/// <summary>
/// 開始遊戲時的系統時間
/// </summary>
public static System.DateTime StartTime;
/// <summary>
/// 開始遊戲時的遊戲時間
/// </summary>
public static GameTime StartSaveGameTime;
public static GameTime Now
{
get
{
return StartSaveGameTime + System.Convert.ToInt32((System.DateTime.Now - StartTime).TotalSeconds * GameConfig.timeMultiple);
}
}
/// <summary>
/// 總秒數
/// </summary>
public int MinuteCount{
get{
return Day * 24 * 60 + Hour * 60 + Minute;
}
}
/// <summary>
/// 當前天的總秒數
/// </summary>
public int MinuteCountIgnoreDay
{
get
{
return Hour * 60 + Minute;
}
}
public static GameTime MinutesToGameTine(int minutes)
{
return new GameTime(minutes / 24 / 60, minutes / 60 % 24, minutes % 60);
}
/// <summary>
/// 帶上日期的比較,如果不帶日期直接用><=
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public int CompareTo(GameTime time)
{
int t1 = MinuteCount;
int t2 = time.MinuteCount;
if (t1 == t2) return 0;
if (t1 > t2) return 1;
return -1;
}
public static bool operator <(GameTime a, GameTime b)
{
if (a.Hour != b.Hour) return a.Hour < b.Hour;
return a.Minute < b.Minute;
}
public static bool operator >(GameTime a, GameTime b)
{
return b < a;
}
public static bool operator ==(GameTime a, GameTime b)
{
return !(a < b) && !(b < a);
}
public static bool operator !=(GameTime a, GameTime b)
{
return a < b || b < a;
}
public static bool operator >=(GameTime a, GameTime b)
{
return !(a < b);
}
public static bool operator <=(GameTime a, GameTime b)
{
return !(b < a);
}
public static GameTime operator +(GameTime a, int minute)
{
GameTime res = new GameTime(a.Day, a.Hour, a.Minute);
res.Minute += minute;
if (res.Minute >= 60)
{
res.Hour += res.Minute / 60;
res.Minute %= 60;
}
if (res.Hour >= 24)
{
res.Day += res.Hour / 24;
res.Hour %= 24;
}
return res;
}
/// <summary>
/// 返回不考慮日期的時間間隔,單位為秒數
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static int operator -(GameTime a, GameTime b)
{
return a.MinuteCountIgnoreDay - b.MinuteCountIgnoreDay;
}
public override string ToString()
{
return string.Format("第{0}日 {1:D2}:{2:D2}", Day, Hour, Minute);
}
}
配置類內容
// 一格亮度為0.1光強
public static float maxLightIntensity = 1;
public static float minLightIntensity = 0.15f;
// 每天的光照強度變化時間點
public static GameTime beginLight = new GameTime(0, 4, 30);
public static GameTime endLight = new GameTime(0, 18, 30);
public static GameTime beginMaxLight = new GameTime(0, 9, 30);
public static GameTime endMaxLight = new GameTime(0, 14, 30);
// 時間倍速流動(測試用)
public static float timeMultiple = 30;
光照類比較簡單就不多說了
/*
* Author : Jk_Chen
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
public class GlobalManager : MonoBehaviour
{
new Light2D light;
GameTime beginLight, endLight;
GameTime beginMaxLight, endMaxLight;
void Awake()
{
light = transform.Find("Light").GetComponent<Light2D>();
beginLight = GameConfig.beginLight;
endLight = GameConfig.endLight;
beginMaxLight = GameConfig.beginMaxLight;
endMaxLight = GameConfig.endMaxLight;
}
public GameTime now;
void Update()
{
if (Helper.DealInterval(1))
{
GameTime now = GameTime.Now;
if(now >= endLight || now <= beginLight)//深夜
{
light.intensity = GameConfig.minLightIntensity;
}
else if(now >= beginMaxLight && now <= endMaxLight)//正午
{
light.intensity = GameConfig.maxLightIntensity;
}
else if(now < beginMaxLight)//上午
{
light.intensity = GameConfig.minLightIntensity + (GameConfig.maxLightIntensity - GameConfig.minLightIntensity) * (now - beginLight) / (beginMaxLight - beginLight);
}
else//下午
{
light.intensity = GameConfig.minLightIntensity + (GameConfig.maxLightIntensity - GameConfig.minLightIntensity) * (now - endLight) / (endMaxLight - endLight);
}
}
}
}