OctoCore 0.0.1
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 810bd977c701b444698d99ea57294f02
|
||||
@@ -0,0 +1,86 @@
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace OctoCore.UI
|
||||
{
|
||||
[RequireComponent(typeof(RectTransform))]
|
||||
public class OctoUIAnchor : MonoBehaviour
|
||||
{
|
||||
[SerializeField, OctoReadOnly] RectTransform _rectTransform;
|
||||
[SerializeField] TextAnchor _anchor;
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
name = "Anchor_" + _anchor.ToString();
|
||||
|
||||
if (!_rectTransform)
|
||||
_rectTransform = GetComponent<RectTransform>();
|
||||
|
||||
if (_rectTransform)
|
||||
{
|
||||
switch (_anchor)
|
||||
{
|
||||
case TextAnchor.UpperLeft:
|
||||
_rectTransform.pivot = new Vector2(0, 1);
|
||||
break;
|
||||
|
||||
case TextAnchor.UpperCenter:
|
||||
_rectTransform.pivot = new Vector2(.5f, 1);
|
||||
break;
|
||||
|
||||
case TextAnchor.UpperRight:
|
||||
_rectTransform.pivot = new Vector2(1, 1);
|
||||
break;
|
||||
|
||||
case TextAnchor.MiddleLeft:
|
||||
_rectTransform.pivot = new Vector2(0, .5f);
|
||||
break;
|
||||
|
||||
case TextAnchor.MiddleCenter:
|
||||
_rectTransform.pivot = new Vector2(.5f, .5f);
|
||||
break;
|
||||
|
||||
case TextAnchor.MiddleRight:
|
||||
_rectTransform.pivot = new Vector2(1, .5f);
|
||||
break;
|
||||
|
||||
case TextAnchor.LowerLeft:
|
||||
_rectTransform.pivot = new Vector2(0, 0);
|
||||
break;
|
||||
|
||||
case TextAnchor.LowerCenter:
|
||||
_rectTransform.pivot = new Vector2(.5f, 0);
|
||||
break;
|
||||
|
||||
case TextAnchor.LowerRight:
|
||||
_rectTransform.pivot = new Vector2(1, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (OctoCore.Instance)
|
||||
{
|
||||
OctoCore.Instance.UI.SubscribeOnGlobalScaleChange(SetScale);
|
||||
SetScale(OctoCore.Instance.UI.GlobalScale);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (OctoCore.Instance)
|
||||
OctoCore.Instance.UI.UnsubscribeOnGlobalScaleChange(SetScale);
|
||||
}
|
||||
|
||||
public void SetScale(float scale)
|
||||
{
|
||||
if (!_rectTransform)
|
||||
return;
|
||||
|
||||
_rectTransform.localScale = Vector3.one * scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9e3f3e4f661e9d24395c654b7a77169b
|
||||
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OctoCore.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// UI Manager component for managing canvas scaling.
|
||||
/// Automatically adjusts canvas scale factor based on screen size.
|
||||
/// </summary>
|
||||
[ExecuteAlways]
|
||||
public class OctoUIManager : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// References to UI settings and canvas scaler.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public CanvasScaler CanvasScaler;
|
||||
}
|
||||
|
||||
[SerializeField] private bool dontDestroy = true;
|
||||
[SerializeField] private bool updateInEditor;
|
||||
[SerializeField] private References references;
|
||||
|
||||
private Action<Vector2Int> onScreenResize;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private Vector2Int currentScreenSize;
|
||||
#endif
|
||||
|
||||
void Awake()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying && !updateInEditor) return;
|
||||
#endif
|
||||
|
||||
if (dontDestroy)
|
||||
DontDestroyOnLoad(this);
|
||||
|
||||
if (OctoCore.Instance)
|
||||
OctoCore.Instance.UI.SubscribeOnScreenResize(onScreenResize = OnScreenResize);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying && !updateInEditor) return;
|
||||
#endif
|
||||
|
||||
RescaleCanvas();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying && updateInEditor)
|
||||
RescaleCanvas(new Vector2Int(Screen.width, Screen.height));
|
||||
#endif
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying && !updateInEditor) return;
|
||||
#endif
|
||||
|
||||
if (onScreenResize != null && OctoCore.Instance)
|
||||
{
|
||||
OctoCore.Instance.UI.UnsubscribeOnScreenResize(onScreenResize);
|
||||
onScreenResize = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnScreenResize(Vector2Int screenSize)
|
||||
{
|
||||
RescaleCanvas(screenSize);
|
||||
}
|
||||
|
||||
public void RescaleCanvas()
|
||||
{
|
||||
if (!OctoCore.Instance || !OctoCore.Instance.Settings) return;
|
||||
|
||||
RescaleCanvas(OctoCore.Instance.UI.ScreenSize, OctoCore.Instance.Settings.Ui.ReferenceSize);
|
||||
}
|
||||
|
||||
public void RescaleCanvas(Vector2Int screenSize)
|
||||
{
|
||||
if (!OctoCore.Instance || !OctoCore.Instance.Settings) return;
|
||||
|
||||
RescaleCanvas(screenSize, OctoCore.Instance.Settings.Ui.ReferenceSize);
|
||||
}
|
||||
|
||||
private void RescaleCanvas(Vector2Int screenSize, Vector2Int referenceSize)
|
||||
{
|
||||
if (!references.CanvasScaler) return;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying && updateInEditor)
|
||||
{
|
||||
if (currentScreenSize == screenSize) return;
|
||||
currentScreenSize = screenSize;
|
||||
}
|
||||
#endif
|
||||
|
||||
float scaleFactor = Mathf.Min((float)screenSize.x / referenceSize.x, (float)screenSize.y / referenceSize.y);
|
||||
if (scaleFactor == 0) return;
|
||||
|
||||
references.CanvasScaler.scaleFactor = scaleFactor;
|
||||
|
||||
if (OctoCore.Instance && OctoCore.Instance.Development.Log)
|
||||
Debug.Log($"Canvas scale factor updated: {scaleFactor}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 867a31b76989f4b439835acbf722b1d9
|
||||
Reference in New Issue
Block a user