new cut scene
This commit is contained in:
@@ -5,27 +5,35 @@ 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
|
||||
[ExecuteAlways]
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public class URPFogZone_Master : MonoBehaviour
|
||||
{
|
||||
// --- Publiczne pola ---
|
||||
[Header("Ustawienia docelowe mg³y")]
|
||||
#region Ustawienia Publiczne
|
||||
[Header("Ustawienia Docelowe Mg³y")]
|
||||
public bool targetFogEnabled = true;
|
||||
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 Przejœcia (tylko w trybie gry)")]
|
||||
public float transitionDuration = 2.0f;
|
||||
public string triggerTag = "MainCamera";
|
||||
#endregion
|
||||
|
||||
// --- Prywatne i statyczne pola ---
|
||||
#region Pola Prywatne i Statyczne
|
||||
private static bool s_defaultsSaved = false;
|
||||
private static bool s_defaultFogEnabled;
|
||||
private static Color s_defaultFogColor;
|
||||
@@ -34,48 +42,32 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
private static float s_defaultFogStartDistance;
|
||||
private static float s_defaultFogEndDistance;
|
||||
|
||||
// NOWOŒÆ: Œledzi, która strefa jest obecnie aktywna.
|
||||
private static URPFogZone_Master s_activeZone = null;
|
||||
|
||||
private Coroutine transitionCoroutine;
|
||||
private Collider zoneCollider;
|
||||
#endregion
|
||||
|
||||
#region Logika dla Edytora
|
||||
#if UNITY_EDITOR
|
||||
private bool isEditorCameraInZone = false;
|
||||
#endif
|
||||
|
||||
// --- Cykl ¿ycia ---
|
||||
private void OnEnable()
|
||||
// Statyczny konstruktor. Wywo³ywany dziêki atrybutowi [InitializeOnLoad] nad klas¹.
|
||||
static URPFogZone_Master()
|
||||
{
|
||||
zoneCollider = GetComponent<Collider>();
|
||||
SaveDefaultFogSettings();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.update += EditorUpdate;
|
||||
#endif
|
||||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
private static void OnPlayModeStateChanged(PlayModeStateChange state)
|
||||
{
|
||||
// Jeœli ten obiekt by³ ostatni¹ aktywn¹ stref¹, przywróæ domyœlne ustawienia.
|
||||
if (s_activeZone == this)
|
||||
if (state == PlayModeStateChange.ExitingPlayMode)
|
||||
{
|
||||
if (transitionCoroutine != null) StopCoroutine(transitionCoroutine);
|
||||
RestoreDefaultSettings();
|
||||
s_defaultsSaved = false;
|
||||
s_activeZone = null;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.update -= EditorUpdate;
|
||||
#endif
|
||||
}
|
||||
|
||||
// --- Logika dla trybu edycji ---
|
||||
#if UNITY_EDITOR
|
||||
private void OnValidate()
|
||||
{
|
||||
if (Application.isPlaying) return;
|
||||
// Zastosuj zmiany tylko jeœli ta strefa jest aktywna
|
||||
if (s_activeZone == this)
|
||||
{
|
||||
ApplyTargetSettingsDirectly();
|
||||
@@ -89,29 +81,61 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
var sceneView = SceneView.lastActiveSceneView;
|
||||
if (sceneView == null || sceneView.camera == null) return;
|
||||
|
||||
if (zoneCollider == null) zoneCollider = GetComponent<Collider>();
|
||||
if (zoneCollider == null) return;
|
||||
|
||||
bool isCurrentlyInside = zoneCollider.bounds.Contains(sceneView.camera.transform.position);
|
||||
|
||||
// WEJŒCIE do strefy
|
||||
if (isCurrentlyInside && s_activeZone != this)
|
||||
{
|
||||
s_activeZone = this;
|
||||
isEditorCameraInZone = true;
|
||||
SaveDefaultFogSettings();
|
||||
ApplyTargetSettingsDirectly();
|
||||
}
|
||||
// WYJŒCIE ze strefy
|
||||
else if (!isCurrentlyInside && s_activeZone == this)
|
||||
{
|
||||
s_activeZone = null;
|
||||
isEditorCameraInZone = false;
|
||||
RestoreDefaultSettings();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
// --- Logika dla trybu gry ---
|
||||
#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);
|
||||
}
|
||||
@@ -119,18 +143,51 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (!Application.isPlaying || !other.CompareTag(triggerTag)) return;
|
||||
// Przywróæ domyœlne tylko jeœli opuszczamy TÊ aktywn¹ strefê.
|
||||
|
||||
if (s_activeZone == this)
|
||||
{
|
||||
s_activeZone = null;
|
||||
StartTransition(false);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
// --- Wspólne metody ---
|
||||
// =================================================================================
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
// =================================================================================
|
||||
|
||||
#region Metody G³ówne
|
||||
private static void SaveDefaultFogSettings()
|
||||
{
|
||||
if (s_defaultsSaved) return;
|
||||
|
||||
s_defaultFogEnabled = RenderSettings.fog;
|
||||
s_defaultFogColor = RenderSettings.fogColor;
|
||||
s_defaultFogMode = RenderSettings.fogMode;
|
||||
@@ -140,6 +197,22 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
s_defaultsSaved = true;
|
||||
}
|
||||
|
||||
private static void RestoreDefaultSettings()
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
private void ApplyTargetSettingsDirectly()
|
||||
{
|
||||
RenderSettings.fog = targetFogEnabled;
|
||||
@@ -148,20 +221,7 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
RenderSettings.fogDensity = targetFogDensity;
|
||||
RenderSettings.fogStartDistance = targetFogStartDistance;
|
||||
RenderSettings.fogEndDistance = targetFogEndDistance;
|
||||
#if UNITY_EDITOR
|
||||
SceneView.RepaintAll();
|
||||
#endif
|
||||
}
|
||||
|
||||
private static void RestoreDefaultSettings()
|
||||
{
|
||||
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
|
||||
@@ -169,11 +229,14 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
|
||||
private void StartTransition(bool toTarget)
|
||||
{
|
||||
if (transitionCoroutine != null) StopCoroutine(transitionCoroutine);
|
||||
if (transitionCoroutine != null)
|
||||
{
|
||||
StopCoroutine(transitionCoroutine);
|
||||
}
|
||||
transitionCoroutine = StartCoroutine(TransitionFogCoroutine(toTarget));
|
||||
}
|
||||
|
||||
IEnumerator TransitionFogCoroutine(bool toTarget)
|
||||
private IEnumerator TransitionFogCoroutine(bool toTarget)
|
||||
{
|
||||
float elapsed = 0f;
|
||||
Color startColor = RenderSettings.fogColor;
|
||||
@@ -186,20 +249,46 @@ public class URPFogZone_Master : MonoBehaviour
|
||||
float finalStartDist = toTarget ? targetFogStartDistance : s_defaultFogStartDistance;
|
||||
float finalEndDist = toTarget ? targetFogEndDistance : s_defaultFogEndDistance;
|
||||
|
||||
RenderSettings.fog = toTarget ? targetFogEnabled : s_defaultFogEnabled;
|
||||
RenderSettings.fogMode = toTarget ? targetFogMode : s_defaultFogMode;
|
||||
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)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = (transitionDuration > 0) ? Mathf.Clamp01(elapsed / transitionDuration) : 1f;
|
||||
// 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