I'm trying to update the sizeDelta of a RectTransform in OnValidate. I'm trying to grab the computed values from a Layout Group.
Here's what I've got:
#if UNITY_EDITOR
void OnValidate()
{
UnityEditor.EditorApplication.delayCall += Resize;
}
private void Resize()
{
if (this == null) return;
RectTransform parentRect = (RectTransform)gameObject.transform;
RectTransform textRect = (RectTransform)tabText.transform;
textRect.sizeDelta = new Vector2(parentRect.rect.height, parentRect.rect.width);
layoutElement.preferredHeight = parentRect.rect.height;
}
#endif
At first this seemed to be working, but it only works the first time OnValidate runs. Subsequence invocations of Resize() sets the sizeDelta to Vector2(0, 0).
I'm wondering what causes this and if there's a way to avoid it. If I remove the delayCall, the values are correct, but I get warnings:
SendMessage cannot be called during Awake, CheckConsistency, or OnValidate
I'd like to do this at edit time since there's no need to compute this stuff at runtime.