OctoCore 0.0.1
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user