using UnityEngine.UI;
using UnityEngine.Profiling;
namespace UnityEngine.AzureSky
{
public class AzureDemoUI : MonoBehaviour
{
[Header("Main References")]
/// Reference to the main camera.
[SerializeField] private Camera m_camera;
/// Reference to the AzureCoreSystem component that controls the time and weather.
[SerializeField] private AzureCoreSystem m_azureCoreSystem;
/// Reference to the AzureFogScattering component that renders the fog effect to the camera.
[SerializeField] private AzureFogScatteringRenderer m_fogScattering;
/// Reference to the terrain transform.
[SerializeField] private Transform m_terrainTransform;
/// Reference to the AzureVolumetricLightRenderer component.
[SerializeField] private AzureVolumetricLightRenderer m_volumetricLightRenderer;
/// Reference to the AzureFogScatteringRenderer component.
[SerializeField] private AzureFogScatteringRenderer m_fogScatteringRenderer;
/// Reference to the rain particle system.
[SerializeField] private ParticleSystem m_rainParticle;
[Header("Time UI References")]
/// Reference to the day input field.
[SerializeField] private InputField m_dayInputField;
/// Reference to the day input field.
[SerializeField] private InputField m_monthInputField;
/// Reference to the day input field.
[SerializeField] private InputField m_yearInputField;
/// Reference to the day input field.
[SerializeField] private InputField m_latitudeInputField;
/// Reference to the day input field.
[SerializeField] private InputField m_longitudeInputField;
/// Reference to the day input field.
[SerializeField] private InputField m_utcInputField;
[Header("Stats UI References")]
/// Reference to the fps text.
[SerializeField] private Text m_fpsText;
/// Reference to the ms text.
[SerializeField] private Text m_msText;
/// Reference to the reserved memory text.
[SerializeField] private Text m_reservedMemoryText;
/// Reference to the allocated memory text.
[SerializeField] private Text m_allocatedMemoryText;
/// Reference to the mono memory text.
[SerializeField] private Text m_monoMemoryText;
/// Update the fps counter 4 times per second.
private int m_updateRate = 4;
/// The frame number counter.
private int m_frameCount = 0;
/// The delta time counter.
private float m_deltaTime = 0f;
/// The frame rate per second.
private float m_fps = 0f;
/// The CPU time.
private float m_ms = 0f;
[Header("Weather UI References")]
/// Reference to the weather transition bar image.
[SerializeField] private Image m_weatherTransitionBar;
private Vector3 m_scale = Vector3.one;
private void Start()
{
Screen.SetResolution(1920, 1080, true);
}
private void OnEnable()
{
AzureNotificationCenter.OnWeatherTransitionEnd += OnWeatherTransitionEnd;
}
private void OnDisable()
{
AzureNotificationCenter.OnWeatherTransitionEnd -= OnWeatherTransitionEnd;
}
private void Update()
{
// Set memory stats
m_allocatedMemoryText.text = ((int)(Profiler.GetTotalAllocatedMemoryLong() / 1048576.0f)).ToString() + "MB";
m_reservedMemoryText.text = ((int)(Profiler.GetTotalReservedMemoryLong() / 1048576.0f)).ToString() + "MB";
m_monoMemoryText.text = ((int)(Profiler.GetMonoUsedSizeLong() / 1048576.0f)).ToString() + "MB";
// Set fps stats
m_deltaTime += Time.unscaledDeltaTime;
m_frameCount++;
if (m_deltaTime > 1.0f / m_updateRate)
{
m_fps = m_frameCount / m_deltaTime;
m_ms = m_deltaTime / m_frameCount * 1000.0f;
m_fpsText.text = Mathf.RoundToInt(m_fps).ToString() + "fps";
m_msText.text = m_ms.ToString("0.0") + "ms";
m_deltaTime = 0.0f;
m_frameCount = 0;
}
// Set the weather transition bar
if (m_azureCoreSystem.weatherSystem.isWeatherChanging)
{
m_weatherTransitionBar.transform.localScale = new Vector3(m_azureCoreSystem.weatherSystem.weatherTransitionProgress, 1.0f, 1.0f);
}
// Quit the demo application
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
/// Enables or disables the terrain, according to the toggle state.
public void EnableDisableTerrain(Toggle toggle)
{
m_terrainTransform.gameObject.SetActive(toggle.isOn);
}
/// Sets the screen resolution, according to the dropdown parameter.
public void SetScreenResolution(Dropdown dropdown)
{
switch (dropdown.value)
{
case 0: // 4K
Screen.SetResolution(3840, 2160, true);
break;
case 1: // QUAD HD
Screen.SetResolution(2560, 1440, true);
break;
case 2: // FULL HD
Screen.SetResolution(1920, 1080, true);
break;
case 3: // HD
Screen.SetResolution(1280, 720, true);
break;
}
}
/// Sets the resolution of the fog scattering texture, according to the dropdown parameter.
public void SetFogScatteringResolution(Dropdown dropdown)
{
switch (dropdown.value)
{
case 0: // 4K
m_fogScattering.SetFogScatteringResolution(3840, 2160);
break;
case 1: // QUAD HD
m_fogScattering.SetFogScatteringResolution(2560, 1440);
break;
case 2: // FULL HD
m_fogScattering.SetFogScatteringResolution(1920, 1080);
break;
case 3: // HD
m_fogScattering.SetFogScatteringResolution(1280, 720);
break;
case 4: // qFull HD
m_fogScattering.SetFogScatteringResolution(960, 540);
break;
case 5: // qHD
m_fogScattering.SetFogScatteringResolution(640, 360);
break;
}
}
/// Sets the time of day mode to simple or realistic, according to the dropdown parameter.
public void SetTimeMode(Dropdown dropdown)
{
switch (dropdown.value)
{
case 0: // Simple
m_dayInputField.transform.parent.gameObject.SetActive(false);
m_monthInputField.transform.parent.gameObject.SetActive(false);
m_yearInputField.transform.parent.gameObject.SetActive(false);
m_latitudeInputField.transform.parent.gameObject.SetActive(false);
m_longitudeInputField.transform.parent.gameObject.SetActive(false);
m_utcInputField.transform.parent.gameObject.SetActive(false);
m_azureCoreSystem.timeSystem.timeMode = AzureTimeMode.Simple;
m_azureCoreSystem.timeSystem.latitude = 0.0f;
m_azureCoreSystem.timeSystem.longitude = 0.0f;
m_azureCoreSystem.timeSystem.utc = 0.0f;
m_azureCoreSystem.timeSystem.SetDate(1, 1, 2024);
break;
case 1: // Realistic
m_dayInputField.transform.parent.gameObject.SetActive(true);
m_monthInputField.transform.parent.gameObject.SetActive(true);
m_yearInputField.transform.parent.gameObject.SetActive(true);
m_latitudeInputField.transform.parent.gameObject.SetActive(true);
m_longitudeInputField.transform.parent.gameObject.SetActive(true);
m_utcInputField.transform.parent.gameObject.SetActive(true);
m_azureCoreSystem.timeSystem.timeMode = AzureTimeMode.Realistic;
m_azureCoreSystem.timeSystem.latitude = int.Parse(m_latitudeInputField.text);
m_azureCoreSystem.timeSystem.longitude = int.Parse(m_longitudeInputField.text);
m_azureCoreSystem.timeSystem.utc = int.Parse(m_utcInputField.text);
m_azureCoreSystem.timeSystem.SetDate(int.Parse(m_dayInputField.text), int.Parse(m_monthInputField.text), int.Parse(m_yearInputField.text));
break;
}
}
/// Update the time system when there is a change in the time UI elements.
public void UpdateTimeSettings()
{
m_azureCoreSystem.timeSystem.latitude = int.Parse(m_latitudeInputField.text);
m_azureCoreSystem.timeSystem.longitude = int.Parse(m_longitudeInputField.text);
m_azureCoreSystem.timeSystem.utc = int.Parse(m_utcInputField.text);
m_azureCoreSystem.timeSystem.SetDate(int.Parse(m_dayInputField.text), int.Parse(m_monthInputField.text), int.Parse(m_yearInputField.text));
}
/// Quit the application from a UI button click.
public void ApplicationQuit()
{
Application.Quit();
}
/// Register or Unregister the 'UpdateDynamicGI' to the core update event, according to the parameter toggle.
public void OnUpdateDynamicGIToggleChanged(Toggle toggle)
{
switch (toggle.isOn)
{
case true:
//m_azureCoreSystem.onCoreUpdateEvent.AddListener(m_azureCoreSystem.UpdateDynamicGI);
//UnityEventTools.AddPersistentListener(m_azureCoreSystem.onCoreUpdateEvent, m_azureCoreSystem.UpdateDynamicGI);
//UnityEventTools.RegisterPersistentListener(m_azureCoreSystem.onCoreUpdateEvent, 1, m_azureCoreSystem.UpdateDynamicGI);
m_azureCoreSystem.onCoreUpdateEvent.SetPersistentListenerState(1, Events.UnityEventCallState.RuntimeOnly);
break;
case false:
//m_azureCoreSystem.onCoreUpdateEvent.RemoveListener(m_azureCoreSystem.UpdateDynamicGI);
//UnityEventTools.RemovePersistentListener(m_azureCoreSystem.onCoreUpdateEvent, m_azureCoreSystem.UpdateDynamicGI);
//UnityEventTools.UnregisterPersistentListener(m_azureCoreSystem.onCoreUpdateEvent, 1);
m_azureCoreSystem.onCoreUpdateEvent.SetPersistentListenerState(1, Events.UnityEventCallState.Off);
break;
}
}
/// Register or Unregister the 'UpdateReflectionProbe' to the core update event, according to the parameter toggle.
public void OnUpdateReflectionProbeToggleChanged(Toggle toggle)
{
switch (toggle.isOn)
{
case true:
//m_azureCoreSystem.onCoreUpdateEvent.AddListener(m_azureCoreSystem.UpdateReflectionProbe);
//UnityEventTools.AddPersistentListener(m_azureCoreSystem.onCoreUpdateEvent, m_azureCoreSystem.UpdateReflectionProbe);
//UnityEventTools.RegisterPersistentListener(m_azureCoreSystem.onCoreUpdateEvent, 2, m_azureCoreSystem.UpdateReflectionProbe);
m_azureCoreSystem.onCoreUpdateEvent.SetPersistentListenerState(2, Events.UnityEventCallState.EditorAndRuntime);
break;
case false:
//m_azureCoreSystem.onCoreUpdateEvent.RemoveListener(m_azureCoreSystem.UpdateReflectionProbe);
//UnityEventTools.RemovePersistentListener(m_azureCoreSystem.onCoreUpdateEvent, m_azureCoreSystem.UpdateReflectionProbe);
//UnityEventTools.UnregisterPersistentListener(m_azureCoreSystem.onCoreUpdateEvent, 2);
m_azureCoreSystem.onCoreUpdateEvent.SetPersistentListenerState(2, Events.UnityEventCallState.Off);
break;
}
}
/// Enables or Disables the VSync, according to the toggle passed as parameter.
public void OnVSyncToggleChanged(Toggle toggle)
{
switch (toggle.isOn)
{
case true:
QualitySettings.vSyncCount = 1;
break;
case false:
QualitySettings.vSyncCount = 0;
break;
}
}
/// Enables or disables the volumetric lights rendering, according to the toggle state.
public void EnableDisableVolumetricLights(Toggle toggle)
{
m_volumetricLightRenderer.enabled = toggle.isOn;
}
/// Enables or disables the fog scattering rendering, according to the toggle state.
public void EnableDisableFogScattering(Toggle toggle)
{
m_fogScatteringRenderer.enabled = toggle.isOn;
}
/// Enables or disables the particles sub emitters nodule, according to the toggle state.
public void EnableDisableParticleSubEmitters(Toggle toggle)
{
var sub = m_rainParticle.subEmitters;
sub.enabled = toggle.isOn;
}
/// Set the global weather from a button click event.
public void SetGlobalWeather(int index)
{
m_azureCoreSystem.weatherSystem.SetGlobalWeather(index);
}
/// Instantiate the first thunder index from the thunder list using a button click event.
public void InstantiateThunder()
{
m_azureCoreSystem.weatherSystem.InstantiateThunderPrefab(0);
//m_azureCoreSystem.weatherSystem.InstantiateThunderPrefab(0, new Vector3(0.0f, 600.0f, 1250.0f));
}
/// Set the timeline using a slider as parameter.
public void SetTimeline(Slider slider)
{
m_azureCoreSystem.timeSystem.timeline = slider.value;
Debug.Log($"m_azureCoreSystem.timeSystem.timeline={slider.value}");
}
/// Set the camera altitude using a slider as parameter.
public void SetCameraAltitude(Slider slider)
{
m_camera.transform.position = new Vector3(0.0f, slider.value, 0.0f);
}
/// Event triggered when a weather transition ends.
private void OnWeatherTransitionEnd(AzureWeatherSystem azureWeatherSystem)
{
m_weatherTransitionBar.transform.localScale = new Vector3(0.0f, 1.0f, 1.0f);
}
}
}