campfire/Assets/Scripts/AutoListPositioner.cs

32 lines
1.1 KiB
C#
Raw Normal View History

2020-04-19 22:09:31 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class AutoListPositioner : MonoBehaviour {
public float Padding;
public List<float> SpecificPaddings;
private void Update() {
2020-04-20 02:38:37 +02:00
if (!Application.isPlaying) {
2020-04-19 22:09:31 +02:00
for (int Index = SpecificPaddings.Count; Index < transform.childCount - 1; Index++) {
SpecificPaddings.Add(Padding);
}
RectTransform Parent = GetComponent<RectTransform>();
float Y = Parent.position.y;
for (int Index = 0; Index < transform.childCount; Index++) {
RectTransform Child = transform.GetChild(Index).GetComponent<RectTransform>();
if (Child) {
Child.position = new Vector2(Child.position.x, Y);
if (Index < SpecificPaddings.Count) {
Y -= Child.rect.height + SpecificPaddings[Index];
}
} else {
Debug.LogWarning($"AutoListPositioner {name} has non-UI children!");
}
}
}
}
}