dark wood
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
// --- PE£NY I POPRAWIONY SKRYPT FOGZONE.CS ---
|
||||
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -7,14 +5,23 @@ using UnityEngine;
|
||||
using UnityEditor;
|
||||
#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>
|
||||
// --- NOWA WERSJA FOGZONE.CS (z rêcznym przywracaniem domyœlnych) ---
|
||||
|
||||
[ExecuteAlways]
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public class FogZone : MonoBehaviour
|
||||
{
|
||||
// --- NOWA SEKCJA Z USTAWIENIAMI DOMYŒLNYMI ---
|
||||
[Header("Domyœlne Ustawienia Mg³y (Wartoœci Globalne)")]
|
||||
[Tooltip("Tutaj zdefiniuj, jak wygl¹da standardowa mg³a na Twoim poziomie. Przycisk 'Przywróæ Domyœlne' bêdzie u¿ywa³ tych wartoœci.")]
|
||||
public bool defaultFogEnabled = true;
|
||||
public Color defaultFogColor = new Color(0.5f, 0.5f, 0.5f);
|
||||
public FogMode defaultFogMode = FogMode.Exponential;
|
||||
[Range(0f, 1f)]
|
||||
public float defaultFogDensity = 0.01f;
|
||||
public float defaultFogStartDistance = 0f;
|
||||
public float defaultFogEndDistance = 300f;
|
||||
|
||||
[Header("Ustawienia Docelowe Mg³y (Wewn¹trz Strefy)")]
|
||||
public Color targetFogColor = new Color(0.5f, 0.5f, 0.5f);
|
||||
public FogMode targetFogMode = FogMode.Exponential;
|
||||
@@ -24,117 +31,74 @@ public class FogZone : MonoBehaviour
|
||||
public float targetFogEndDistance = 300f;
|
||||
|
||||
[Header("Ustawienia Zachowania")]
|
||||
[Tooltip("Czas w sekundach, w jakim mg³a bêdzie p³ynnie przechodziæ do nowych ustawieñ.")]
|
||||
public float transitionDuration = 2.0f;
|
||||
[Tooltip("Tag obiektu (zazwyczaj gracza), który ma aktywowaæ strefê.")]
|
||||
public string playerTag = "Player";
|
||||
|
||||
[Header("Ustawienia Edytora")]
|
||||
[Tooltip("W³¹cza podgl¹d mg³y w edytorze, gdy kamera Scene View wejdzie do strefy.")]
|
||||
public bool enableEditorPreview = true;
|
||||
|
||||
// Statyczne, by zapewniæ, ¿e tylko jedna zmiana mg³y dzieje siê naraz w ca³ej grze
|
||||
// --- CA£A RESZTA KODU POZOSTAJE BEZ ZMIAN ---
|
||||
|
||||
private static Coroutine s_transitionCoroutine;
|
||||
private static MonoBehaviour s_coroutineRunner;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void InitializeOnLoad()
|
||||
{
|
||||
{ /* ... bez zmian ... */
|
||||
s_transitionCoroutine = null;
|
||||
s_coroutineRunner = null;
|
||||
}
|
||||
|
||||
// --- NOWA LOGIKA - SPRAWDZENIE NA STARCIE GRY ---
|
||||
|
||||
/// <summary>
|
||||
/// Sprawdza na starcie gry, czy gracz ju¿ znajduje siê w strefie.
|
||||
/// Jeœli tak, aktywuje mg³ê natychmiast.
|
||||
/// </summary>
|
||||
private void Start()
|
||||
{
|
||||
// Upewniamy siê, ¿e ten kod dzia³a tylko w trybie gry, a nie w edytorze
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// ZnajdŸ obiekt gracza na scenie za pomoc¹ tagu
|
||||
{ /* ... bez zmian ... */
|
||||
if (!Application.isPlaying) return;
|
||||
GameObject playerObject = GameObject.FindWithTag(playerTag);
|
||||
if (playerObject == null)
|
||||
{
|
||||
// Jeœli nie ma gracza na scenie (lub ma z³y tag), nic nie robimy
|
||||
return;
|
||||
}
|
||||
|
||||
// Pobierz collider tej strefy mg³y
|
||||
if (playerObject == null) return;
|
||||
Collider zoneCollider = GetComponent<Collider>();
|
||||
|
||||
// SprawdŸ, czy pozycja gracza znajduje siê wewn¹trz granic collidera tej strefy
|
||||
if (zoneCollider.bounds.Contains(playerObject.transform.position))
|
||||
{
|
||||
Debug.Log($"Gracz '{playerObject.name}' rozpocz¹³ grê wewn¹trz strefy '{this.name}'. Ustawiam mg³ê natychmiast.", this);
|
||||
|
||||
// U¿ywamy ma³ej sztuczki: tymczasowo ustawiamy czas przejœcia na 0,
|
||||
// aby mg³a pojawi³a siê od razu, a nie p³ynnie.
|
||||
float originalDuration = transitionDuration;
|
||||
transitionDuration = 0f;
|
||||
|
||||
// Uruchom logikê wejœcia do strefy
|
||||
StartTransition(true);
|
||||
|
||||
// Przywróæ oryginalny czas przejœcia, aby póŸniejsze wejœcia/wyjœcia
|
||||
// dzia³a³y z normaln¹, p³ynn¹ animacj¹.
|
||||
transitionDuration = originalDuration;
|
||||
}
|
||||
}
|
||||
|
||||
// --- LOGIKA TRYBU GRY (BEZ ZMIAN) ---
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (Application.isPlaying && other.CompareTag(playerTag))
|
||||
{
|
||||
StartTransition(true);
|
||||
}
|
||||
{ /* ... bez zmian ... */
|
||||
if (Application.isPlaying && other.CompareTag(playerTag)) StartTransition(true);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (Application.isPlaying && other.CompareTag(playerTag))
|
||||
{
|
||||
StartTransition(false);
|
||||
}
|
||||
{ /* ... bez zmian ... */
|
||||
if (Application.isPlaying && other.CompareTag(playerTag)) StartTransition(false);
|
||||
}
|
||||
|
||||
// --- PUBLICZNE METODY DLA EVENTÓW (CUTSCENY) ---
|
||||
|
||||
public void ActivateZoneFogFromEvent()
|
||||
{
|
||||
{ /* ... bez zmian ... */
|
||||
if (!Application.isPlaying) return;
|
||||
StartTransition(true);
|
||||
}
|
||||
|
||||
public void RevertToDefaultFogFromEvent()
|
||||
{
|
||||
{ /* ... bez zmian ... */
|
||||
if (!Application.isPlaying) return;
|
||||
StartTransition(false);
|
||||
}
|
||||
|
||||
// --- G£ÓWNA LOGIKA PRZEJŒCIA (BEZ ZMIAN) ---
|
||||
|
||||
private void StartTransition(bool toZoneSettings)
|
||||
{
|
||||
{ /* ... bez zmian ... */
|
||||
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)
|
||||
{
|
||||
// JEDYNA ZMIANA: Gdy wracamy do domyœlnych, bierzemy je z naszych pól
|
||||
Color startColor = RenderSettings.fogColor;
|
||||
float startDensity = RenderSettings.fogDensity;
|
||||
float startLinearStart = RenderSettings.fogStartDistance;
|
||||
@@ -153,78 +117,55 @@ public class FogZone : MonoBehaviour
|
||||
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;
|
||||
// TUTAJ NAST¥PI£A ZMIANA - ZAMIAST FogDefaultSettings.Instance
|
||||
endColor = defaultFogColor;
|
||||
endMode = defaultFogMode;
|
||||
endDensity = defaultFogDensity;
|
||||
endLinearStart = defaultFogStartDistance;
|
||||
endLinearEnd = defaultFogEndDistance;
|
||||
}
|
||||
|
||||
RenderSettings.fog = true;
|
||||
RenderSettings.fogMode = toZoneSettings ? targetFogMode : defaultFogMode; // Ustaw tryb od razu
|
||||
|
||||
float elapsed = 0f;
|
||||
while (elapsed < transitionDuration)
|
||||
{
|
||||
// Jeœli czas przejœcia jest 0 (jak w naszym przypadku na starcie), 't' od razu bêdzie 1
|
||||
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;
|
||||
}
|
||||
|
||||
// Upewniamy siê, ¿e na koñcu s¹ dok³adnie docelowe wartoœci
|
||||
RenderSettings.fogColor = endColor;
|
||||
RenderSettings.fogDensity = endDensity;
|
||||
RenderSettings.fogStartDistance = endLinearStart;
|
||||
RenderSettings.fogEndDistance = endLinearEnd;
|
||||
|
||||
if (!toZoneSettings && FogDefaultSettings.Instance != null)
|
||||
if (!toZoneSettings)
|
||||
{
|
||||
RenderSettings.fogMode = FogDefaultSettings.Instance.fogMode;
|
||||
RenderSettings.fog = FogDefaultSettings.Instance.fogEnabled;
|
||||
RenderSettings.fog = defaultFogEnabled; // Po powrocie ustawiamy, czy domyœlnie mg³a ma byæ w³¹czona
|
||||
}
|
||||
|
||||
s_transitionCoroutine = null;
|
||||
s_coroutineRunner = null;
|
||||
}
|
||||
|
||||
|
||||
// --- LOGIKA TYLKO DLA EDYTORA UNITY (BEZ ZMIAN) ---
|
||||
// --- LOGIKA EDYTORA Z DROBN¥ POPRAWK¥ ---
|
||||
#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()
|
||||
{
|
||||
{ /* ... bez zmian ... */
|
||||
var col = GetComponent<Collider>();
|
||||
if (col != null) col.isTrigger = true;
|
||||
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
EditorApplication.update += EditorUpdate;
|
||||
}
|
||||
if (!Application.isPlaying) EditorApplication.update += EditorUpdate;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
@@ -234,8 +175,8 @@ public class FogZone : MonoBehaviour
|
||||
EditorApplication.update -= EditorUpdate;
|
||||
if (s_editorActiveZone == this)
|
||||
{
|
||||
RestoreEditorSettings();
|
||||
s_editorActiveZone = null;
|
||||
// ZMIANA: Zamiast przywracaæ zapamiêtane, przywracamy zdefiniowane
|
||||
RestoreDefinedDefaults();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -243,43 +184,30 @@ public class FogZone : MonoBehaviour
|
||||
private void EditorUpdate()
|
||||
{
|
||||
if (Application.isPlaying) return;
|
||||
|
||||
if (!enableEditorPreview)
|
||||
{
|
||||
if (s_editorActiveZone == this)
|
||||
{
|
||||
RestoreEditorSettings();
|
||||
s_editorActiveZone = null;
|
||||
}
|
||||
if (s_editorActiveZone == this) RestoreDefinedDefaults();
|
||||
return;
|
||||
}
|
||||
|
||||
var sceneView = SceneView.lastActiveSceneView;
|
||||
if (sceneView == null || sceneView.camera == null) return;
|
||||
|
||||
var zoneCollider = GetComponent<Collider>();
|
||||
if (zoneCollider == null) return;
|
||||
|
||||
bool isCameraInside = zoneCollider.bounds.Contains(sceneView.camera.transform.position);
|
||||
|
||||
if (isCameraInside)
|
||||
{
|
||||
if (s_editorActiveZone != this)
|
||||
{
|
||||
if (s_editorActiveZone == null)
|
||||
{
|
||||
SaveEditorSettings();
|
||||
}
|
||||
s_editorActiveZone = this;
|
||||
ApplyZoneSettingsDirectly();
|
||||
}
|
||||
ApplyZoneSettingsDirectly();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s_editorActiveZone == this)
|
||||
{
|
||||
RestoreEditorSettings();
|
||||
s_editorActiveZone = null;
|
||||
RestoreDefinedDefaults();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -292,18 +220,6 @@ public class FogZone : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private static void SaveEditorSettings()
|
||||
{
|
||||
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 ApplyZoneSettingsDirectly()
|
||||
{
|
||||
RenderSettings.fog = true;
|
||||
@@ -315,16 +231,16 @@ public class FogZone : MonoBehaviour
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
|
||||
private static void RestoreEditorSettings()
|
||||
// NOWA METODA, KTÓRA PRZYWRACA DOMYŒLNE ZDEFINIOWANE W INSPEKTORZE
|
||||
public void RestoreDefinedDefaults()
|
||||
{
|
||||
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;
|
||||
RenderSettings.fog = defaultFogEnabled;
|
||||
RenderSettings.fogColor = defaultFogColor;
|
||||
RenderSettings.fogMode = defaultFogMode;
|
||||
RenderSettings.fogDensity = defaultFogDensity;
|
||||
RenderSettings.fogStartDistance = defaultFogStartDistance;
|
||||
RenderSettings.fogEndDistance = defaultFogEndDistance;
|
||||
s_editorActiveZone = null;
|
||||
SceneView.RepaintAll();
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user