247 lines
8.2 KiB
C#
247 lines
8.2 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
// --- 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;
|
|
[Range(0f, 1f)]
|
|
public float targetFogDensity = 0.02f;
|
|
public float targetFogStartDistance = 0f;
|
|
public float targetFogEndDistance = 300f;
|
|
|
|
[Header("Ustawienia Zachowania")]
|
|
public float transitionDuration = 2.0f;
|
|
public string playerTag = "Player";
|
|
|
|
[Header("Ustawienia Edytora")]
|
|
public bool enableEditorPreview = true;
|
|
|
|
// --- 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;
|
|
}
|
|
|
|
private void Start()
|
|
{ /* ... bez zmian ... */
|
|
if (!Application.isPlaying) return;
|
|
GameObject playerObject = GameObject.FindWithTag(playerTag);
|
|
if (playerObject == null) return;
|
|
Collider zoneCollider = GetComponent<Collider>();
|
|
if (zoneCollider.bounds.Contains(playerObject.transform.position))
|
|
{
|
|
float originalDuration = transitionDuration;
|
|
transitionDuration = 0f;
|
|
StartTransition(true);
|
|
transitionDuration = originalDuration;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{ /* ... bez zmian ... */
|
|
if (Application.isPlaying && other.CompareTag(playerTag)) StartTransition(true);
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{ /* ... bez zmian ... */
|
|
if (Application.isPlaying && other.CompareTag(playerTag)) StartTransition(false);
|
|
}
|
|
|
|
public void ActivateZoneFogFromEvent()
|
|
{ /* ... bez zmian ... */
|
|
if (!Application.isPlaying) return;
|
|
StartTransition(true);
|
|
}
|
|
|
|
public void RevertToDefaultFogFromEvent()
|
|
{ /* ... bez zmian ... */
|
|
if (!Application.isPlaying) return;
|
|
StartTransition(false);
|
|
}
|
|
|
|
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;
|
|
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;
|
|
}
|
|
else
|
|
{
|
|
// 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)
|
|
{
|
|
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)
|
|
{
|
|
RenderSettings.fog = defaultFogEnabled; // Po powrocie ustawiamy, czy domyœlnie mg³a ma byæ w³¹czona
|
|
}
|
|
|
|
s_transitionCoroutine = null;
|
|
s_coroutineRunner = null;
|
|
}
|
|
|
|
// --- LOGIKA EDYTORA Z DROBN¥ POPRAWK¥ ---
|
|
#if UNITY_EDITOR
|
|
private static FogZone s_editorActiveZone = null;
|
|
|
|
private void OnEnable()
|
|
{ /* ... bez zmian ... */
|
|
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)
|
|
{
|
|
// ZMIANA: Zamiast przywracaæ zapamiêtane, przywracamy zdefiniowane
|
|
RestoreDefinedDefaults();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void EditorUpdate()
|
|
{
|
|
if (Application.isPlaying) return;
|
|
if (!enableEditorPreview)
|
|
{
|
|
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)
|
|
{
|
|
s_editorActiveZone = this;
|
|
}
|
|
ApplyZoneSettingsDirectly();
|
|
}
|
|
else
|
|
{
|
|
if (s_editorActiveZone == this)
|
|
{
|
|
RestoreDefinedDefaults();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (!Application.isPlaying && s_editorActiveZone == this)
|
|
{
|
|
ApplyZoneSettingsDirectly();
|
|
}
|
|
}
|
|
|
|
private void ApplyZoneSettingsDirectly()
|
|
{
|
|
RenderSettings.fog = true;
|
|
RenderSettings.fogMode = targetFogMode;
|
|
RenderSettings.fogColor = targetFogColor;
|
|
RenderSettings.fogDensity = targetFogDensity;
|
|
RenderSettings.fogStartDistance = targetFogStartDistance;
|
|
RenderSettings.fogEndDistance = targetFogEndDistance;
|
|
SceneView.RepaintAll();
|
|
}
|
|
|
|
// NOWA METODA, KTÓRA PRZYWRACA DOMYŒLNE ZDEFINIOWANE W INSPEKTORZE
|
|
public void RestoreDefinedDefaults()
|
|
{
|
|
RenderSettings.fog = defaultFogEnabled;
|
|
RenderSettings.fogColor = defaultFogColor;
|
|
RenderSettings.fogMode = defaultFogMode;
|
|
RenderSettings.fogDensity = defaultFogDensity;
|
|
RenderSettings.fogStartDistance = defaultFogStartDistance;
|
|
RenderSettings.fogEndDistance = defaultFogEndDistance;
|
|
s_editorActiveZone = null;
|
|
SceneView.RepaintAll();
|
|
}
|
|
#endif
|
|
} |