95 lines
2.8 KiB
C#
95 lines
2.8 KiB
C#
using System;
|
||
using Games.Events;
|
||
using Module.Log;
|
||
using UnityEngine;
|
||
|
||
public class FogDensityLerp : MonoBehaviour
|
||
{
|
||
private static bool _usingFogLerp;
|
||
private FogSettingData _baseFogSettings;
|
||
private bool _isCurrent;
|
||
public FogSettingData maxFogSettings;
|
||
public float maxHeight = 20f;
|
||
public float minHeight = 10f;
|
||
|
||
private void LateUpdate()
|
||
{
|
||
var mainPlayer = ObjManager.Instance.MainPlayer;
|
||
if (mainPlayer != null)
|
||
{
|
||
var ratio = Mathf.Clamp01((mainPlayer.Position.y - minHeight) / (maxHeight - minHeight));
|
||
_baseFogSettings.Lerp(maxFogSettings, ratio);
|
||
}
|
||
}
|
||
|
||
private void Awake()
|
||
{
|
||
if (_usingFogLerp)
|
||
{
|
||
LogModule.ErrorLog(string.Format("发现重复的雾效密度调节器,于物体{0}", transform.GetHierarchyName()));
|
||
enabled = false;
|
||
}
|
||
else
|
||
{
|
||
_usingFogLerp = true;
|
||
_isCurrent = true;
|
||
LogModule.WarningLog(string.Format("当前场景使用雾效密度调节器进行雾效调节,于物体{0}", transform.GetHierarchyName()));
|
||
}
|
||
}
|
||
|
||
// 注:如果编辑器中Awake不成功,则不会出现Start效果
|
||
private void Start()
|
||
{
|
||
if (_isCurrent)
|
||
{
|
||
_baseFogSettings = new FogSettingData();
|
||
_baseFogSettings.Read();
|
||
EventDispatcher.Instance.Add(Games.Events.EventId.FogSettingUpdate, OnFogSettingUpdate);
|
||
OnFogSettingUpdate(null);
|
||
}
|
||
}
|
||
|
||
private void OnFogSettingUpdate(object args)
|
||
{
|
||
enabled = PlayerPreferenceData.SystemQualityFog;
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
if (_isCurrent)
|
||
{
|
||
_usingFogLerp = false;
|
||
_baseFogSettings.Write();
|
||
}
|
||
EventDispatcher.Instance.Remove(Games.Events.EventId.FogSettingUpdate, OnFogSettingUpdate);
|
||
}
|
||
|
||
[Serializable]
|
||
public class FogSettingData
|
||
{
|
||
public float density;
|
||
public float endDistance;
|
||
public float startDistance;
|
||
|
||
public void Read()
|
||
{
|
||
startDistance = RenderSettings.fogStartDistance;
|
||
endDistance = RenderSettings.fogEndDistance;
|
||
density = RenderSettings.fogDensity;
|
||
}
|
||
|
||
public void Write()
|
||
{
|
||
RenderSettings.fogStartDistance = startDistance;
|
||
RenderSettings.fogEndDistance = endDistance;
|
||
RenderSettings.fogDensity = density;
|
||
}
|
||
|
||
public void Lerp(FogSettingData other, float ratio)
|
||
{
|
||
RenderSettings.fogStartDistance = Mathf.Lerp(startDistance, other.startDistance, ratio);
|
||
RenderSettings.fogEndDistance = Mathf.Lerp(endDistance, other.endDistance, ratio);
|
||
RenderSettings.fogDensity = Mathf.Lerp(density, other.density, ratio);
|
||
}
|
||
}
|
||
} |