AlicizaX/Client/Assets/UILayoutGroupFix.cs
2025-12-05 19:04:53 +08:00

33 lines
989 B
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace GameBase
{
[DisallowMultipleComponent]
public class UILayoutGroupFix : MonoBehaviour
{
[SerializeField] private bool fixOnEnable = true;
[SerializeField] private bool fixWithDelay = true;
const float fixDelay = 0.1f;
void OnEnable()
{
if (!fixWithDelay && fixOnEnable) { LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>()); }
else if (fixWithDelay) { StartCoroutine(FixDelay()); }
}
public void FixLayout()
{
if (!fixWithDelay) { LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>()); }
else { StartCoroutine(FixDelay()); }
}
IEnumerator FixDelay()
{
yield return new WaitForSecondsRealtime(fixDelay);
LayoutRebuilder.MarkLayoutForRebuild(GetComponent<RectTransform>());
}
}
}