40 lines
1.0 KiB
C#
40 lines
1.0 KiB
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);
|
|
}
|
|
}
|
|
}
|
|
} |