Firstborn/Assets/SWAN Dev/DynamicUI/DTransformRectStretch.cs

61 lines
2.4 KiB
C#
Raw Normal View History

/// <summary>
/// Created by SWAN DEV
/// </summary>
using UnityEngine;
/// DynamicUI UGUI easy stretch for setting the size of RectTransform with respect to the parent RectTransform.
/// ***This scirpt forcibly set the rectTransforms as Stretch-Stretch and then apply the values(Left, Right, Top, Down).
public class DTransformRectStretch : MonoBehaviour
{
[Tooltip("The RectTransforms to be stretched with the settings in this mono script.")]
/// The RectTransforms to be stretched with the settings in this mono script.
public RectTransform[] m_RectTransforms;
[Tooltip("The left space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform.")]
/// The left space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform.
public float m_Left;
[Tooltip("The right space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform.")]
/// The right space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform.
public float m_Right;
[Tooltip("The top space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform.")]
/// The top space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform.
public float m_Top;
[Tooltip("The bottom space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform")]
/// The bottom space(unit: pixel) with respect to the sizeDelta(rect) of the parent RectTransform.
public float m_Bottom;
[Tooltip("Destroy this script from gameObject after executed?")]
/// Destroy this script from gameObject after executed?
public bool m_DestroyOnComplete = true;
void OnEnable()
{
if (m_RectTransforms != null && m_RectTransforms.Length > 0)
{
for (int i = 0; i < m_RectTransforms.Length; i++)
{
if (m_RectTransforms[i] != null)
{
SetStretch(m_RectTransforms[i], m_Left, m_Right, m_Top, m_Bottom);
}
}
}
if (m_DestroyOnComplete) Destroy(this);
}
public static void SetStretch(RectTransform targetRT, float left = 0, float right = 0, float top = 0, float bottom = 0)
{
targetRT.anchorMax = Vector2.one;
targetRT.anchorMin = Vector2.zero;
targetRT.offsetMax = new Vector2(-right, -top);
targetRT.offsetMin = new Vector2(left, bottom);
}
}