Skip to content

Commit d9bc79e

Browse files
Refresh LayoutGroup3d during gameplay
1 parent bb56a02 commit d9bc79e

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

LayoutGroups/LayoutGroup3d.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using System.Collections.Generic;
23

34
namespace Wrj
45
{
@@ -17,12 +18,24 @@ public class LayoutGroup3d : MonoBehaviour {
1718
private bool _cachedDepthCentering = true;
1819
public float depthSpacing;
1920
private float _cachedDepthSpacing;
20-
private Transform[] _children;
21+
private List<Transform> _children;
2122

2223
void Update ()
2324
{
24-
// If the children have changed, force an update on everything
25-
Transform[] children = GetComponentsInChildren<Transform>();
25+
if (Application.isPlaying) return;
26+
Refresh();
27+
}
28+
29+
public void Refresh()
30+
{
31+
List<Transform> children = new List<Transform>();
32+
foreach (Transform child in transform)
33+
{
34+
if (child.gameObject.activeInHierarchy)
35+
{
36+
children.Add(child);
37+
}
38+
}
2639
if (_children != children)
2740
{
2841
_children = children;
@@ -35,9 +48,9 @@ void Update ()
3548
{
3649
_cachedHorizontalSpacing = horizontalSpacing;
3750
_cachedHorizontalCentering = horizontalCentering;
38-
Vector3 leftmostPos = (horizontalCentering) ? transform.localPosition.With(x: -(horizontalSpacing * (transform.childCount - 1)) * .5f) : Vector3.zero;
51+
Vector3 leftmostPos = (horizontalCentering) ? transform.localPosition.With(x: -(horizontalSpacing * (children.Count - 1)) * .5f) : Vector3.zero;
3952
float appliedSpacing = 0f;
40-
foreach (Transform element in transform)
53+
foreach (Transform element in children)
4154
{
4255
element.localPosition = element.localPosition.With(x: leftmostPos.x + appliedSpacing);
4356
appliedSpacing += horizontalSpacing;
@@ -48,9 +61,9 @@ void Update ()
4861
{
4962
_cachedVerticalSpacing = verticalSpacing;
5063
_cachedVerticalCentering = verticalCentering;
51-
Vector3 topmostPos = (verticalCentering) ? transform.localPosition.With(y: -(verticalSpacing * (transform.childCount - 1)) * .5f) : Vector3.zero;
64+
Vector3 topmostPos = (verticalCentering) ? transform.localPosition.With(y: -(verticalSpacing * (children.Count - 1)) * .5f) : Vector3.zero;
5265
float appliedSpacing = 0f;
53-
foreach (Transform element in transform)
66+
foreach (Transform element in children)
5467
{
5568
element.localPosition = element.localPosition.With(y: topmostPos.y + appliedSpacing);
5669
appliedSpacing += verticalSpacing;
@@ -61,9 +74,9 @@ void Update ()
6174
{
6275
_cachedDepthSpacing = depthSpacing;
6376
_cachedDepthCentering = depthCentering;
64-
Vector3 farmostPos = (depthCentering) ? transform.localPosition.With(z: -(depthSpacing * (transform.childCount - 1)) * .5f) : Vector3.zero;
77+
Vector3 farmostPos = (depthCentering) ? transform.localPosition.With(z: -(depthSpacing * (children.Count - 1)) * .5f) : Vector3.zero;
6578
float appliedSpacing = 0f;
66-
foreach (Transform element in transform)
79+
foreach (Transform element in children)
6780
{
6881
element.localPosition = element.localPosition.With(z: farmostPos.z + appliedSpacing);
6982
appliedSpacing += depthSpacing;
-61 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)