Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Created May 28, 2024 18:20
Show Gist options
  • Save yasirkula/2ef6c9cf7d83c035fa29ac3a691e77c9 to your computer and use it in GitHub Desktop.
Save yasirkula/2ef6c9cf7d83c035fa29ac3a691e77c9 to your computer and use it in GitHub Desktop.
Snap a RectTransform's anchors to its corner points in Unity
using UnityEditor;
using UnityEngine;
public class SnapRectTransformAnchorsToCorners : MonoBehaviour
{
[MenuItem("CONTEXT/RectTransform/Snap Anchors To Corners", priority = 50)]
private static void Execute(MenuCommand command)
{
RectTransform rectTransform = command.context as RectTransform;
RectTransform parent = rectTransform.parent as RectTransform;
Vector2 parentSize = parent.rect.size;
Vector2 parentMin = parent.rect.min;
// Find corner point coordinates in parent's local space
Vector3[] corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
corners[0] = parent.InverseTransformPoint(corners[0]); // Bottom left
corners[2] = parent.InverseTransformPoint(corners[2]); // Top right
Undo.RecordObject(rectTransform, "Anchor Modification");
rectTransform.anchorMin = new Vector2((corners[0].x - parentMin.x) / parentSize.x, (corners[0].y - parentMin.y) / parentSize.y);
rectTransform.anchorMax = new Vector2((corners[2].x - parentMin.x) / parentSize.x, (corners[2].y - parentMin.y) / parentSize.y);
rectTransform.sizeDelta = Vector2.zero;
rectTransform.anchoredPosition = Vector2.zero;
}
[MenuItem("CONTEXT/RectTransform/Snap Anchors To Corners", validate = true)]
private static bool Validate(MenuCommand command)
{
RectTransform rectTransform = command.context as RectTransform;
return rectTransform.parent != null && !PrefabUtility.IsPartOfImmutablePrefab(rectTransform);
}
}
@yasirkula
Copy link
Author

How To

Simply create a folder called Editor inside your Project window and add this script inside it. Then, right click a RectTransform in the Inspector to execute this operation. The RectTransform must not be rotated and must have a scale value of (1,1,1).

SnapRectTransformAnchorsToCorners

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment