ProjectDDD/Packages/SLUnity/SLUI/SLUILayoutElement.cs
2025-06-25 11:33:17 +09:00

40 lines
1.0 KiB (Stored with Git LFS)
C#

using Superlazy.UI;
using UnityEngine;
using UnityEngine.UI;
[ExecuteAlways]
public class SLUILayoutElement : MonoBehaviour
{
public bool useCanvasSize = false;
private Canvas canvas;
private LayoutElement element;
private SLUILayoutScale scale;
private void Awake()
{
canvas = GetComponentInParent<Canvas>();
if (TryGetComponent(out element) == false)
{
element = gameObject.AddComponent<LayoutElement>();
element.hideFlags = HideFlags.HideAndDontSave;
}
scale = GetComponent<SLUILayoutScale>();
}
private void Update()
{
if (element == null || canvas == null) return;
if (useCanvasSize)
{
element.preferredWidth = canvas.pixelRect.width / canvas.scaleFactor;
element.preferredHeight = canvas.pixelRect.height / canvas.scaleFactor;
if (scale)
{
scale.startSize = new Vector2(element.preferredWidth, element.preferredHeight);
}
}
}
}