Files
Ipseity-Demo/Packages/OctoCore/Runtime/Scripts/UI/OctoUI.cs
T
2026-02-09 23:53:14 +01:00

105 lines
2.7 KiB
C#

using OctoCore.Components;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace OctoCore.UI
{
/// <summary>
/// UI management module.
/// Handles global UI scaling and screen size detection.
/// </summary>
[System.Serializable]
public class OctoUI : IOctoModule
{
/// <summary>
/// Global scale factor for all UI elements.
/// </summary>
public float GlobalScale = 1;
/// <summary>
/// Current screen size in pixels.
/// </summary>
[OctoReadOnly] public Vector2Int ScreenSize;
private Action<float> OnGlobalScaleChange;
private Action<Vector2Int> OnScreenResize;
public void Init(OctoSettings settings)
{
// Callback OctoEvents
if (OctoEvents.Instance)
OctoEvents.Instance.SetUiInitialized();
}
public void Tick()
{
CheckSize();
}
public void OnSceneChanged(Scene current, Scene next)
{
}
public void OnQuit()
{
}
public void UnsubscribeOnGlobalScaleChange(Action<float> handler)
{
SubscribeOnGlobalScaleChange(handler, false);
}
public void SubscribeOnGlobalScaleChange(Action<float> handler, bool subscribe = true)
{
if (subscribe) OnGlobalScaleChange += handler;
else OnGlobalScaleChange -= handler;
}
public void UnsubscribeOnScreenResize(Action<Vector2Int> handler)
{
SubscribeOnScreenResize(handler, false);
}
public void SubscribeOnScreenResize(Action<Vector2Int> handler, bool subscribe = true)
{
if (subscribe) OnScreenResize += handler;
else OnScreenResize -= handler;
}
public void SetGlobalScale(float scale)
{
GlobalScale = scale;
OnGlobalScaleChange?.Invoke(scale);
}
public void CheckSize()
{
if (ScreenSize.x != Screen.width || ScreenSize.y != Screen.height)
{
ScreenSize.x = Screen.width;
ScreenSize.y = Screen.height;
OnScreenResize?.Invoke(ScreenSize);
}
}
public void SetCursor()
{
bool visible = true;
CursorLockMode lockMode = CursorLockMode.None;
if (OctoPlayer.Instance && !OctoPlayer.Instance.IsPaused)
{
visible = true;
lockMode = CursorLockMode.Locked;
}
Cursor.visible = visible;
Cursor.lockState = lockMode;
}
}
}