using System; using System.Collections.Generic; using UnityEngine; namespace DTT.Utils.Extensions { /// /// Provides extension methods for rect transform components. /// public static class RectTransformExtensions { /// /// Represents values for a rect anchor setting. /// private struct RectSetting { /// /// The anchor's max values. /// public Vector2 anchorMax; /// /// The anchor's min values. /// public Vector2 anchorMin; /// /// The pivot values. /// public Vector2 pivot; /// /// Initializes the rectangle setting. /// /// The minimum x value. /// The maximum x value. /// The minimum y value. /// The maximum y value. /// The pivot value on the x axis. /// The pivot value on the y axis. public RectSetting(float xMin, float xMax, float yMin, float yMax, float xPivot, float yPivot) { anchorMax = new Vector2(xMax, yMax); anchorMin = new Vector2(xMin, yMin); pivot = new Vector2(xPivot, yPivot); } } /// /// Holds the preset values used for each anchor setting. /// private static readonly Dictionary _anchorPresets = new Dictionary { { RectAnchor.TOP_LEFT, new RectSetting( 0f, 0f, 1f,1f, 0f,1f )}, { RectAnchor.TOP_CENTER, new RectSetting( 0.5f, 0.5f, 1f,1f,0.5f,1f )}, { RectAnchor.TOP_RIGHT, new RectSetting( 1f,1f,1f,1f, 1f,1f )}, { RectAnchor.MIDDLE_LEFT, new RectSetting( 0f,0f, 0.5f, 0.5f,0f,0.5f )}, { RectAnchor.MIDDLE_CENTER, new RectSetting( 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f )}, { RectAnchor.MIDDLE_RIGHT, new RectSetting( 1f,1f,0.5f,0.5f, 1f, 0.5f )}, { RectAnchor.BOTTOM_LEFT, new RectSetting( 0f,0f,0f,0f, 0f, 0f )}, { RectAnchor.BOTTOM_CENTER, new RectSetting( 0.5f, 0.5f, 0f, 0f, 0.5f, 0f )}, { RectAnchor.BOTTOM_RIGHT, new RectSetting( 1f,1f,0f,0f, 1f, 0f )}, { RectAnchor.STRETCH_TOP, new RectSetting( 0f, 1f, 1f,1f,0.5f,1f )}, { RectAnchor.STRETCH_MIDDLE, new RectSetting( 0f,1f,0.5f,0.5f, 0.5f, 0.5f )}, { RectAnchor.STRETCH_BOTTOM, new RectSetting( 0f,1f,0f,0f, 0.5f, 0f )}, { RectAnchor.STRETCH_LEFT, new RectSetting( 0f,0f,0f,1f, 0f,0.5f )}, { RectAnchor.STRETCH_CENTER, new RectSetting( 0.5f, 0.5f, 0f, 1f, 0.5f, 0.5f )}, { RectAnchor.STRETCH_RIGHT, new RectSetting( 1f,1f, 0f, 1f, 1f, 0.5f )}, { RectAnchor.STRETCH_FULL, new RectSetting( 0f, 1f, 0f, 1f, 0.5f, 0.5f )}, }; /// /// Sets the anchor values. /// /// The rectangle transform. /// The minimum x value. /// The maximum x value. /// The minimum y value. /// The maximum y value. public static void SetAnchor(this RectTransform transform, float xMin, float xMax, float yMin, float yMax) { if (transform == null) throw new ArgumentNullException(nameof(transform)); transform.anchorMin = new Vector2(xMin, yMin); transform.anchorMax = new Vector2(xMax, yMax); } /// /// Sets the anchor to a given setting. /// /// The rectangle transform. /// The anchor setting to use. /// Whether the pivot should also be set based on the new setting. /// Whether to set the position after the setting has been applied. public static void SetAnchor( this RectTransform transform, RectAnchor anchor, bool setPivot = false, bool setPosition = false) { if (transform == null) throw new ArgumentNullException(nameof(transform)); RectSetting setting = _anchorPresets[anchor]; SetAnchor(transform, setting.anchorMin.x, setting.anchorMax.x, setting.anchorMin.y,setting.anchorMax.y); if (setPivot) transform.pivot = setting.pivot; if (setPosition) transform.anchoredPosition = Vector2.zero; } /// /// Returns the world rectangle of a rectangle transform. /// /// The rectangle transform. /// The world rectangle. public static Rect GetWorldRect(this RectTransform transform) { if (transform == null) throw new ArgumentNullException(nameof(transform)); Vector3[] corners = new Vector3[4]; transform.GetWorldCorners(corners); Vector3 bottomLeft = corners[0]; Vector2 size = new Vector2( transform.lossyScale.x * transform.rect.size.x, transform.lossyScale.y * transform.rect.size.y); return new Rect(bottomLeft, size); } /// /// Returns the rectangle transform. Will return null if a normal transform is used. /// /// The component of which to get the rectangle transform. /// The rectangle transform instance. public static RectTransform GetRectTransform(this Component component) { if (component == null) throw new ArgumentNullException(nameof(component)); return component.transform as RectTransform; } /// /// Returns the rectangle transform. Will return null if a normal transform is used. /// /// The game object of which to get the rectangle transform. /// The rectangle transform instance. public static RectTransform GetRectTransform(this GameObject gameObject) { if (gameObject == null) throw new ArgumentNullException(nameof(gameObject)); return gameObject.transform as RectTransform; } } }