Dynamically modifies the size of the RectTransform of a VerticalLayoutGroup in Uniy. This component has to be attached to, for example, the Content GameObject of a ScrollView. If an new item is added to the container you must call the Adjust method to resize the RectTransform.
Last active
February 18, 2023 05:09
-
-
Save javier-games/2daf8c2b39be9b038b37637ed68a445c to your computer and use it in GitHub Desktop.
Dynamically modify the size of the container of a vertical layout group.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.EventSystems; | |
/// <summary> | |
/// Vertical Layout Group Dynamic Content. | |
/// | |
/// <para> | |
/// Allows you to dynamically modify the size of the container of a vertical | |
/// layout group. Can be easily adapted to an horizontal layout group. | |
/// </para> | |
/// | |
/// <para> By Javier García | @jvrgms | 2019 </para> | |
/// | |
/// </summary> | |
[ExecuteInEditMode] | |
[RequireComponent (typeof (VerticalLayoutGroup))] | |
public class VerticalLayoutGroupDynamicContent: UIBehaviour { | |
#region Class Members | |
VerticalLayoutGroup layoutGroup; // Layout group reference. | |
RectTransform rectTransform; // RectTransform of this GO. | |
#endregion | |
#region Accessors | |
/// <summary> Gets the layout group. </summary> | |
/// <value> The layout group. </value> | |
public VerticalLayoutGroup LayoutGroup { | |
get { | |
if (layoutGroup != null) | |
return layoutGroup; | |
layoutGroup = GetComponent<VerticalLayoutGroup> (); | |
return layoutGroup; | |
} | |
} | |
/// <summary> Gets the rect transform. </summary> | |
/// <value> The rect transform. </value> | |
public RectTransform RectTransform { | |
get { | |
if (rectTransform != null) | |
return rectTransform; | |
rectTransform = GetComponent<RectTransform> (); | |
return rectTransform; | |
} | |
} | |
#endregion | |
#region UI Behaviour Overrides | |
/// <summary> Called when the transform parent changed. </summary> | |
protected override void OnTransformParentChanged () { | |
Adjust (); | |
} | |
/// <summary> Called when the rect transform dimensions chenged. </summary> | |
protected override void OnRectTransformDimensionsChange () { | |
Adjust (); | |
} | |
#endregion | |
#region Class Implementation | |
/// <summary> Adjust the size of the rect transform. </summary> | |
public void Adjust () { | |
// Set to zero if this instance does not have any child. | |
if (transform.childCount == 0) { | |
RectTransform.sizeDelta = new Vector2 (RectTransform.sizeDelta.x, 0); | |
return; | |
} | |
float height; | |
int childCount = transform.childCount; | |
// Obtaning height size. | |
height = LayoutGroup.spacing * (childCount - 1); | |
height += LayoutGroup.padding.bottom; | |
height += LayoutGroup.padding.top; | |
foreach (RectTransform child in transform) | |
height += child.sizeDelta.y; | |
// Applying height size. | |
RectTransform.sizeDelta = new Vector2 (RectTransform.sizeDelta.x, height); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment