Advertisement
apieceoffruit

AnchorToCorners

Jul 8th, 2021 (edited)
1,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4.  
  5. using System.Collections;
  6. using System.IO;
  7. public class AnchorToCorners : MonoBehaviour
  8. {
  9.         [MenuItem("Custom Editor Tools/Anchors to Corners %[")]
  10.         static void AnchorsToCorners()
  11.         {
  12.                 RectTransform t = Selection.activeTransform as RectTransform;
  13.                 RectTransform pt = Selection.activeTransform.parent as RectTransform;
  14.        
  15.                 if (t == null || pt == null)
  16.                         return;
  17.        
  18.                 Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
  19.                                             t.anchorMin.y + t.offsetMin.y / pt.rect.height);
  20.                 Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
  21.                                             t.anchorMax.y + t.offsetMax.y / pt.rect.height);
  22.        
  23.                 t.anchorMin = newAnchorsMin;
  24.                 t.anchorMax = newAnchorsMax;
  25.                 t.offsetMin = t.offsetMax = new Vector2(0, 0);
  26.         }
  27.    
  28.         [MenuItem("Custom Editor Tools/Corners to Anchors %]")]
  29.         static void CornersToAnchors()
  30.         {
  31.                 RectTransform t = Selection.activeTransform as RectTransform;
  32.        
  33.                 if (t == null)
  34.                         return;
  35.        
  36.                 t.offsetMin = t.offsetMax = new Vector2(0, 0);
  37.         }
  38.    
  39.         [MenuItem("Custom Editor Tools/Add Padding")]
  40.         static void AddPadding()
  41.         {
  42.                 foreach (var activeTransform in Selection.transforms) {
  43.  
  44.                         RectTransform t = activeTransform as RectTransform;
  45.                         if (t == null)
  46.                                 return;
  47.        
  48.                         t.anchorMin = new Vector2(0, 0);
  49.                         t.anchorMax = new Vector2(1, 1);
  50.                         var padding = 5;
  51.                         t.offsetMin = new Vector2(padding, padding);
  52.                         t.offsetMax = new Vector2(-padding, -padding);
  53.                 }
  54.         }
  55.  
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement