intro new Gog Zpne fix
This commit is contained in:
31
Assets/Scripts/FogDefaultSettings.cs
Normal file
31
Assets/Scripts/FogDefaultSettings.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// JEDYNE <20>RÓD£O PRAWDY o domyœlnej mgle.
|
||||
/// UmieϾ ten skrypt na JEDNYM obiekcie w scenie, np. "_FogManager".
|
||||
/// </summary>
|
||||
public class FogDefaultSettings : MonoBehaviour
|
||||
{
|
||||
public static FogDefaultSettings Instance { get; private set; }
|
||||
|
||||
[Header("Globalne Domyœlne Ustawienia Mg³y URP")]
|
||||
public bool fogEnabled = true;
|
||||
public Color fogColor = new Color(0.7f, 0.7f, 0.7f);
|
||||
public FogMode fogMode = FogMode.Exponential;
|
||||
[Range(0f, 1f)]
|
||||
public float fogDensity = 0.01f;
|
||||
public float fogStartDistance = 50f;
|
||||
public float fogEndDistance = 1000f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/FogDefaultSettings.cs.meta
Normal file
2
Assets/Scripts/FogDefaultSettings.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e008175d2b553845ae2b295a6bf9a1c
|
||||
@@ -5,72 +5,190 @@ using UnityEngine;
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
// Atrybut [InitializeOnLoad] musi byæ tutaj, nad deklaracj¹ klasy.
|
||||
// Dziêki niemu statyczny konstruktor tej klasy zostanie wywo³any automatycznie przez edytor.
|
||||
#if UNITY_EDITOR
|
||||
[InitializeOnLoad]
|
||||
#endif
|
||||
/// <summary>
|
||||
/// Zarz¹dza lokaln¹ stref¹ mg³y URP. Zmienia globalne ustawienia RenderSettings
|
||||
/// z p³ynnym przejœciem. Dzia³a w trybie gry (triggery, eventy) oraz w edytorze (podgl¹d).
|
||||
/// </summary>
|
||||
[ExecuteAlways]
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public class URPFogZone_Master : MonoBehaviour
|
||||
public class FogZone : MonoBehaviour
|
||||
{
|
||||
#region Ustawienia Publiczne
|
||||
[Header("Ustawienia Docelowe Mg³y")]
|
||||
public bool targetFogEnabled = true;
|
||||
[Header("Ustawienia Docelowe Mg³y (Wewn¹trz Strefy)")]
|
||||
public Color targetFogColor = new Color(0.5f, 0.5f, 0.5f);
|
||||
public FogMode targetFogMode = FogMode.Exponential;
|
||||
|
||||
[Header("Ustawienia dla trybu Exponential / Exp2")]
|
||||
[Range(0f, 1f)]
|
||||
public float targetFogDensity = 0.02f;
|
||||
|
||||
[Header("Ustawienia dla trybu Linear")]
|
||||
public float targetFogStartDistance = 0f;
|
||||
public float targetFogEndDistance = 300f;
|
||||
|
||||
[Header("Ustawienia Przejœcia (tylko w trybie gry)")]
|
||||
[Header("Ustawienia Zachowania")]
|
||||
[Tooltip("Czas w sekundach, w jakim mg³a bêdzie p³ynnie przechodziæ do nowych ustawieñ.")]
|
||||
public float transitionDuration = 2.0f;
|
||||
public string triggerTag = "MainCamera";
|
||||
#endregion
|
||||
[Tooltip("Tag obiektu (zazwyczaj gracza), który ma aktywowaæ strefê.")]
|
||||
public string playerTag = "Player";
|
||||
|
||||
#region Pola Prywatne i Statyczne
|
||||
private static bool s_defaultsSaved = false;
|
||||
private static bool s_defaultFogEnabled;
|
||||
private static Color s_defaultFogColor;
|
||||
private static FogMode s_defaultFogMode;
|
||||
private static float s_defaultFogDensity;
|
||||
private static float s_defaultFogStartDistance;
|
||||
private static float s_defaultFogEndDistance;
|
||||
[Header("Ustawienia Edytora")]
|
||||
[Tooltip("W³¹cza podgl¹d mg³y w edytorze, gdy kamera Scene View wejdzie do strefy.")]
|
||||
public bool enableEditorPreview = true;
|
||||
|
||||
private static URPFogZone_Master s_activeZone = null;
|
||||
// Statyczne, by zapewniæ, ¿e tylko jedna zmiana mg³y dzieje siê naraz w ca³ej grze
|
||||
private static Coroutine s_transitionCoroutine;
|
||||
private static MonoBehaviour s_coroutineRunner;
|
||||
|
||||
private Coroutine transitionCoroutine;
|
||||
private Collider zoneCollider;
|
||||
#endregion
|
||||
|
||||
#region Logika dla Edytora
|
||||
#if UNITY_EDITOR
|
||||
// Statyczny konstruktor. Wywo³ywany dziêki atrybutowi [InitializeOnLoad] nad klas¹.
|
||||
static URPFogZone_Master()
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void InitializeOnLoad()
|
||||
{
|
||||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
||||
s_transitionCoroutine = null;
|
||||
s_coroutineRunner = null;
|
||||
}
|
||||
|
||||
private static void OnPlayModeStateChanged(PlayModeStateChange state)
|
||||
// --- LOGIKA TRYBU GRY ---
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (state == PlayModeStateChange.ExitingPlayMode)
|
||||
if (Application.isPlaying && other.CompareTag(playerTag))
|
||||
{
|
||||
s_defaultsSaved = false;
|
||||
s_activeZone = null;
|
||||
StartTransition(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (Application.isPlaying) return;
|
||||
if (s_activeZone == this)
|
||||
if (Application.isPlaying && other.CompareTag(playerTag))
|
||||
{
|
||||
ApplyTargetSettingsDirectly();
|
||||
StartTransition(false);
|
||||
}
|
||||
}
|
||||
|
||||
// --- PUBLICZNE METODY DLA EVENTÓW (CUTSCENY) ---
|
||||
|
||||
public void ActivateZoneFogFromEvent()
|
||||
{
|
||||
if (!Application.isPlaying) return;
|
||||
StartTransition(true);
|
||||
}
|
||||
|
||||
public void RevertToDefaultFogFromEvent()
|
||||
{
|
||||
if (!Application.isPlaying) return;
|
||||
StartTransition(false);
|
||||
}
|
||||
|
||||
// --- G£ÓWNA LOGIKA PRZEJŒCIA ---
|
||||
|
||||
private void StartTransition(bool toZoneSettings)
|
||||
{
|
||||
if (s_transitionCoroutine != null && s_coroutineRunner != null)
|
||||
{
|
||||
s_coroutineRunner.StopCoroutine(s_transitionCoroutine);
|
||||
}
|
||||
|
||||
s_coroutineRunner = this;
|
||||
s_transitionCoroutine = StartCoroutine(TransitionFog(toZoneSettings));
|
||||
}
|
||||
|
||||
private IEnumerator TransitionFog(bool toZoneSettings)
|
||||
{
|
||||
Color startColor = RenderSettings.fogColor;
|
||||
float startDensity = RenderSettings.fogDensity;
|
||||
float startLinearStart = RenderSettings.fogStartDistance;
|
||||
float startLinearEnd = RenderSettings.fogEndDistance;
|
||||
|
||||
Color endColor;
|
||||
FogMode endMode;
|
||||
float endDensity;
|
||||
float endLinearStart;
|
||||
float endLinearEnd;
|
||||
|
||||
if (toZoneSettings)
|
||||
{
|
||||
endColor = targetFogColor;
|
||||
endMode = targetFogMode;
|
||||
endDensity = targetFogDensity;
|
||||
endLinearStart = targetFogStartDistance;
|
||||
endLinearEnd = targetFogEndDistance;
|
||||
RenderSettings.fogMode = endMode;
|
||||
}
|
||||
else
|
||||
{
|
||||
var defaults = FogDefaultSettings.Instance;
|
||||
if (defaults == null)
|
||||
{
|
||||
Debug.LogError("Nie znaleziono FogDefaultSettings w scenie!", this);
|
||||
yield break;
|
||||
}
|
||||
endColor = defaults.fogColor;
|
||||
endMode = defaults.fogMode;
|
||||
endDensity = defaults.fogDensity;
|
||||
endLinearStart = defaults.fogStartDistance;
|
||||
endLinearEnd = defaults.fogEndDistance;
|
||||
}
|
||||
|
||||
RenderSettings.fog = true;
|
||||
|
||||
float elapsed = 0f;
|
||||
while (elapsed < transitionDuration)
|
||||
{
|
||||
float t = (transitionDuration > 0) ? Mathf.Clamp01(elapsed / transitionDuration) : 1f;
|
||||
|
||||
RenderSettings.fogColor = Color.Lerp(startColor, endColor, t);
|
||||
RenderSettings.fogDensity = Mathf.Lerp(startDensity, endDensity, t);
|
||||
RenderSettings.fogStartDistance = Mathf.Lerp(startLinearStart, endLinearStart, t);
|
||||
RenderSettings.fogEndDistance = Mathf.Lerp(startLinearEnd, endLinearEnd, t);
|
||||
|
||||
elapsed += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
RenderSettings.fogColor = endColor;
|
||||
RenderSettings.fogDensity = endDensity;
|
||||
RenderSettings.fogStartDistance = endLinearStart;
|
||||
RenderSettings.fogEndDistance = endLinearEnd;
|
||||
|
||||
if (!toZoneSettings && FogDefaultSettings.Instance != null)
|
||||
{
|
||||
RenderSettings.fogMode = FogDefaultSettings.Instance.fogMode;
|
||||
RenderSettings.fog = FogDefaultSettings.Instance.fogEnabled;
|
||||
}
|
||||
|
||||
s_transitionCoroutine = null;
|
||||
s_coroutineRunner = null;
|
||||
}
|
||||
|
||||
|
||||
// --- LOGIKA TYLKO DLA EDYTORA UNITY ---
|
||||
#if UNITY_EDITOR
|
||||
private static bool s_editorSettingsSaved = false;
|
||||
private static FogZone s_editorActiveZone = null;
|
||||
|
||||
private static bool s_prevFogEnabled;
|
||||
private static Color s_prevFogColor;
|
||||
private static FogMode s_prevFogMode;
|
||||
private static float s_prevFogDensity;
|
||||
private static float s_prevFogStartDist;
|
||||
private static float s_prevFogEndDist;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
var col = GetComponent<Collider>();
|
||||
if (col != null) col.isTrigger = true;
|
||||
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
EditorApplication.update += EditorUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
EditorApplication.update -= EditorUpdate;
|
||||
if (s_editorActiveZone == this)
|
||||
{
|
||||
RestoreEditorSettings();
|
||||
s_editorActiveZone = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,217 +196,90 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
{
|
||||
if (Application.isPlaying) return;
|
||||
|
||||
// *** POPRAWKA JEST TUTAJ ***
|
||||
// Jeœli podgl¹d jest wy³¹czony, ale ta strefa jest wci¹¿ aktywna, przywróæ ustawienia
|
||||
if (!enableEditorPreview)
|
||||
{
|
||||
if (s_editorActiveZone == this)
|
||||
{
|
||||
RestoreEditorSettings();
|
||||
s_editorActiveZone = null;
|
||||
}
|
||||
return; // Zakoñcz dzia³anie metody, jeœli podgl¹d jest wy³¹czony
|
||||
}
|
||||
|
||||
var sceneView = SceneView.lastActiveSceneView;
|
||||
if (sceneView == null || sceneView.camera == null) return;
|
||||
|
||||
if (zoneCollider == null) zoneCollider = GetComponent<Collider>();
|
||||
var zoneCollider = GetComponent<Collider>();
|
||||
if (zoneCollider == null) return;
|
||||
|
||||
bool isCurrentlyInside = zoneCollider.bounds.Contains(sceneView.camera.transform.position);
|
||||
bool isCameraInside = zoneCollider.bounds.Contains(sceneView.camera.transform.position);
|
||||
|
||||
if (isCurrentlyInside && s_activeZone != this)
|
||||
if (isCameraInside)
|
||||
{
|
||||
s_activeZone = this;
|
||||
SaveDefaultFogSettings();
|
||||
ApplyTargetSettingsDirectly();
|
||||
if (s_editorActiveZone != this)
|
||||
{
|
||||
if (s_editorActiveZone == null)
|
||||
{
|
||||
SaveEditorSettings();
|
||||
}
|
||||
s_editorActiveZone = this;
|
||||
ApplyZoneSettingsDirectly();
|
||||
}
|
||||
}
|
||||
else if (!isCurrentlyInside && s_activeZone == this)
|
||||
else
|
||||
{
|
||||
s_activeZone = null;
|
||||
RestoreDefaultSettings();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
#region Cykl ¯ycia Obiektu
|
||||
private void OnEnable()
|
||||
{
|
||||
zoneCollider = GetComponent<Collider>();
|
||||
zoneCollider.isTrigger = true;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
EditorApplication.update += EditorUpdate;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (s_activeZone == this)
|
||||
{
|
||||
if (transitionCoroutine != null) StopCoroutine(transitionCoroutine);
|
||||
RestoreDefaultSettings();
|
||||
s_activeZone = null;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.update -= EditorUpdate;
|
||||
#endif
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Logika Trybu Gry
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!Application.isPlaying || !other.CompareTag(triggerTag)) return;
|
||||
|
||||
SaveDefaultFogSettings();
|
||||
s_activeZone = this;
|
||||
StartTransition(true);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (!Application.isPlaying || !other.CompareTag(triggerTag)) return;
|
||||
|
||||
if (s_activeZone == this)
|
||||
{
|
||||
s_activeZone = null;
|
||||
StartTransition(false);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
// =================================================================================
|
||||
#region NOWOή: Publiczne Metody do sterowania przez Eventy (dla Cutscen)
|
||||
|
||||
/// <summary>
|
||||
/// Aktywuje mg³ê z tej strefy. Podepnij tê funkcjê pod event na pocz¹tku cutsceny.
|
||||
/// </summary>
|
||||
public void ActivateZoneFog()
|
||||
{
|
||||
Debug.Log($"[FogZone] Rêczna aktywacja mg³y dla strefy: {gameObject.name}", this);
|
||||
SaveDefaultFogSettings();
|
||||
s_activeZone = this;
|
||||
StartTransition(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Przywraca domyœlne ustawienia mg³y. Podepnij tê funkcjê pod event na koñcu cutsceny.
|
||||
/// </summary>
|
||||
public void RevertToDefaultFog()
|
||||
{
|
||||
// Sprawdzamy, czy TA strefa jest aktywna. To wa¿ne zabezpieczenie.
|
||||
if (s_activeZone == this)
|
||||
{
|
||||
Debug.Log($"[FogZone] Rêczne przywracanie domyœlnej mg³y ze strefy: {gameObject.name}", this);
|
||||
s_activeZone = null;
|
||||
StartTransition(false);
|
||||
if (s_editorActiveZone == this)
|
||||
{
|
||||
RestoreEditorSettings();
|
||||
s_editorActiveZone = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
// =================================================================================
|
||||
|
||||
#region Metody G³ówne
|
||||
private static void SaveDefaultFogSettings()
|
||||
private void OnValidate()
|
||||
{
|
||||
if (s_defaultsSaved) return;
|
||||
|
||||
s_defaultFogEnabled = RenderSettings.fog;
|
||||
s_defaultFogColor = RenderSettings.fogColor;
|
||||
s_defaultFogMode = RenderSettings.fogMode;
|
||||
s_defaultFogDensity = RenderSettings.fogDensity;
|
||||
s_defaultFogStartDistance = RenderSettings.fogStartDistance;
|
||||
s_defaultFogEndDistance = RenderSettings.fogEndDistance;
|
||||
s_defaultsSaved = true;
|
||||
if (!Application.isPlaying && s_editorActiveZone == this)
|
||||
{
|
||||
ApplyZoneSettingsDirectly();
|
||||
}
|
||||
}
|
||||
|
||||
private static void RestoreDefaultSettings()
|
||||
private static void SaveEditorSettings()
|
||||
{
|
||||
if (!s_defaultsSaved) return;
|
||||
|
||||
RenderSettings.fog = s_defaultFogEnabled;
|
||||
RenderSettings.fogColor = s_defaultFogColor;
|
||||
RenderSettings.fogMode = s_defaultFogMode;
|
||||
RenderSettings.fogDensity = s_defaultFogDensity;
|
||||
RenderSettings.fogStartDistance = s_defaultFogStartDistance;
|
||||
RenderSettings.fogEndDistance = s_defaultFogEndDistance;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
SceneView.RepaintAll();
|
||||
#endif
|
||||
if (s_editorSettingsSaved) return;
|
||||
s_prevFogEnabled = RenderSettings.fog;
|
||||
s_prevFogColor = RenderSettings.fogColor;
|
||||
s_prevFogMode = RenderSettings.fogMode;
|
||||
s_prevFogDensity = RenderSettings.fogDensity;
|
||||
s_prevFogStartDist = RenderSettings.fogStartDistance;
|
||||
s_prevFogEndDist = RenderSettings.fogEndDistance;
|
||||
s_editorSettingsSaved = true;
|
||||
}
|
||||
|
||||
private void ApplyTargetSettingsDirectly()
|
||||
private void ApplyZoneSettingsDirectly()
|
||||
{
|
||||
RenderSettings.fog = targetFogEnabled;
|
||||
RenderSettings.fog = true;
|
||||
RenderSettings.fogMode = targetFogMode;
|
||||
RenderSettings.fogColor = targetFogColor;
|
||||
RenderSettings.fogDensity = targetFogDensity;
|
||||
RenderSettings.fogStartDistance = targetFogStartDistance;
|
||||
RenderSettings.fogEndDistance = targetFogEndDistance;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
|
||||
private static void RestoreEditorSettings()
|
||||
{
|
||||
if (!s_editorSettingsSaved) return;
|
||||
RenderSettings.fog = s_prevFogEnabled;
|
||||
RenderSettings.fogColor = s_prevFogColor;
|
||||
RenderSettings.fogMode = s_prevFogMode;
|
||||
RenderSettings.fogDensity = s_prevFogDensity;
|
||||
RenderSettings.fogStartDistance = s_prevFogStartDist;
|
||||
RenderSettings.fogEndDistance = s_prevFogEndDist;
|
||||
s_editorSettingsSaved = false;
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private void StartTransition(bool toTarget)
|
||||
{
|
||||
if (transitionCoroutine != null)
|
||||
{
|
||||
StopCoroutine(transitionCoroutine);
|
||||
}
|
||||
transitionCoroutine = StartCoroutine(TransitionFogCoroutine(toTarget));
|
||||
}
|
||||
|
||||
private IEnumerator TransitionFogCoroutine(bool toTarget)
|
||||
{
|
||||
float elapsed = 0f;
|
||||
Color startColor = RenderSettings.fogColor;
|
||||
float startDensity = RenderSettings.fogDensity;
|
||||
float startStartDist = RenderSettings.fogStartDistance;
|
||||
float startEndDist = RenderSettings.fogEndDistance;
|
||||
|
||||
Color finalColor = toTarget ? targetFogColor : s_defaultFogColor;
|
||||
float finalDensity = toTarget ? targetFogDensity : s_defaultFogDensity;
|
||||
float finalStartDist = toTarget ? targetFogStartDistance : s_defaultFogStartDistance;
|
||||
float finalEndDist = toTarget ? targetFogEndDistance : s_defaultFogEndDistance;
|
||||
|
||||
if (toTarget)
|
||||
{
|
||||
RenderSettings.fog = targetFogEnabled;
|
||||
RenderSettings.fogMode = targetFogMode;
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderSettings.fogMode = s_defaultFogMode;
|
||||
}
|
||||
|
||||
if (transitionDuration <= 0f)
|
||||
{
|
||||
RenderSettings.fogColor = finalColor;
|
||||
RenderSettings.fogDensity = finalDensity;
|
||||
RenderSettings.fogStartDistance = finalStartDist;
|
||||
RenderSettings.fogEndDistance = finalEndDist;
|
||||
if (!toTarget) RenderSettings.fog = s_defaultFogEnabled;
|
||||
yield break;
|
||||
}
|
||||
|
||||
while (elapsed < transitionDuration)
|
||||
{
|
||||
// U¿ywam unscaledDeltaTime, aby dzia³a³o poprawnie w cutscenach
|
||||
elapsed += Time.unscaledDeltaTime;
|
||||
float t = Mathf.Clamp01(elapsed / transitionDuration);
|
||||
|
||||
RenderSettings.fogColor = Color.Lerp(startColor, finalColor, t);
|
||||
RenderSettings.fogDensity = Mathf.Lerp(startDensity, finalDensity, t);
|
||||
RenderSettings.fogStartDistance = Mathf.Lerp(startStartDist, finalStartDist, t);
|
||||
RenderSettings.fogEndDistance = Mathf.Lerp(startEndDist, finalEndDist, t);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
if (!toTarget)
|
||||
{
|
||||
RenderSettings.fog = s_defaultFogEnabled;
|
||||
}
|
||||
|
||||
transitionCoroutine = null;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user