Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Phedg1 · Nov 19, 2015 at 09:18 AM · editorrecttransformanchor

How To Make Anchor Snap To Own Rect Transform In Editor

How can I make the anchor points easily and accurately snap to the bounds of an objects rect transform? Where "object" is the object that the anchor points are being adjusted on? It seems that this is a regular thing people try to do, only, it never lines up quire right. alt text

Also, how can I move a rect transform and the anchor points for the same object at the same time? I can resize the both at the same time, but how can I reposition the rect transform and have the anchors follow? alt text

Thanks

anchorspng-2.png (31.5 kB)
anchors-4.png (3.5 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Develax · Mar 26, 2020 at 11:13 AM 0
Share

Also, how can I move a rect transform and the anchor points for the same object at the same time?

Ctrl + Shift then drag one of the achors.

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Phedg1 · Nov 22, 2015 at 08:34 AM

It finally occurred to me that I could make some "edit mode" scripts that did both these things. I've included them below, feel free to use them. The features of the script are:

  • Snap anchors to corners of Rect

  • Snap anchors to corners when re-sizing or moving (both via mouse dragging)

  • X, Y, Width, Height variables shown for editing (X, Y control the pivot location, irrelevant to the current anchors)

  • Custom anchor X, Y (as a single point) relative to parent, to substitute for anchor positions being in corners.


anchorTool.cs

 using UnityEngine;
 using System.Collections;
 [ExecuteInEditMode]
 public class anchorTool : MonoBehaviour {
     public bool manualRefresh = true;
     public Rect anchorRect;
     public Vector2 anchorVector;
     private Rect anchorRectOld;
     private Vector2 anchorVectorOld;
     private RectTransform ownRectTransform;
     private RectTransform parentRectTransform;
     private Vector2 pivotOld;
     private Vector2 offsetMinOld;
     private Vector2 offsetMaxOld;
 
     void Update () {
         #if UNITY_EDITOR
         ownRectTransform = gameObject.GetComponent<RectTransform>();
         parentRectTransform = transform.parent.gameObject.GetComponent<RectTransform>();
         if (ownRectTransform.offsetMin != offsetMinOld || ownRectTransform.offsetMax != offsetMaxOld) {
             CalculateCurrentWH();
             CalculateCurrentXY();
         }
         if (ownRectTransform.pivot != pivotOld || anchorVector != anchorVectorOld) {
             CalculateCurrentXY();
             pivotOld = ownRectTransform.pivot;
             anchorVectorOld = anchorVector;
         }
         if (anchorRect != anchorRectOld) {
             AnchorsToCorners();
             anchorRectOld = anchorRect;
         }
         if (manualRefresh) {
             manualRefresh = false;
             CalculateCurrentWH();
             CalculateCurrentXY();
             AnchorsToCorners();
         }
         #endif
     }
 
     public void StopDrag() {
         CalculateCurrentWH();
         CalculateCurrentXY();
         AnchorsToCorners();
     }
 
     private void CalculateCurrentXY() {
         float pivotX = anchorRect.width * ownRectTransform.pivot.x;
         float pivotY = anchorRect.height * (1 - ownRectTransform.pivot.y);
         Vector2 newXY = new Vector2(ownRectTransform.anchorMin.x * parentRectTransform.rect.width + ownRectTransform.offsetMin.x + pivotX - parentRectTransform.rect.width * anchorVector.x,
                                   - (1 - ownRectTransform.anchorMax.y) * parentRectTransform.rect.height + ownRectTransform.offsetMax.y - pivotY + parentRectTransform.rect.height * (1 - anchorVector.y));
         anchorRect.x = newXY.x;
         anchorRect.y = newXY.y;
         anchorRectOld = anchorRect;
     }
 
     private void CalculateCurrentWH() {
         anchorRect.width = ownRectTransform.rect.width;
         anchorRect.height = ownRectTransform.rect.height;
         anchorRectOld = anchorRect;
     }
 
     private void AnchorsToCorners() {
         float pivotX = anchorRect.width * ownRectTransform.pivot.x;
         float pivotY = anchorRect.height * (1 - ownRectTransform.pivot.y) ;
         ownRectTransform.anchorMin = new Vector2(0f, 1f);
         ownRectTransform.anchorMax = new Vector2(0f, 1f);
         ownRectTransform.offsetMin = new Vector2(anchorRect.x / ownRectTransform.localScale.x, anchorRect.y / ownRectTransform.localScale.y - anchorRect.height);
         ownRectTransform.offsetMax = new Vector2(anchorRect.x / ownRectTransform.localScale.x + anchorRect.width, anchorRect.y / ownRectTransform.localScale.y);
         ownRectTransform.anchorMin = new Vector2(ownRectTransform.anchorMin.x + anchorVector.x + (ownRectTransform.offsetMin.x - pivotX) / parentRectTransform.rect.width * ownRectTransform.localScale.x,
                                                  ownRectTransform.anchorMin.y - (1 - anchorVector.y) + (ownRectTransform.offsetMin.y + pivotY) / parentRectTransform.rect.height * ownRectTransform.localScale.y);
         ownRectTransform.anchorMax = new Vector2(ownRectTransform.anchorMax.x + anchorVector.x + (ownRectTransform.offsetMax.x - pivotX) / parentRectTransform.rect.width * ownRectTransform.localScale.x,
                                                  ownRectTransform.anchorMax.y - (1 - anchorVector.y) + (ownRectTransform.offsetMax.y + pivotY) / parentRectTransform.rect.height * ownRectTransform.localScale.y);
         ownRectTransform.offsetMin = new Vector2((0 - ownRectTransform.pivot.x) * anchorRect.width * (1 - ownRectTransform.localScale.x), (0 - ownRectTransform.pivot.y) * anchorRect.height * (1 - ownRectTransform.localScale.y));
         ownRectTransform.offsetMax = new Vector2((1 - ownRectTransform.pivot.x) * anchorRect.width * (1 - ownRectTransform.localScale.x), (1 - ownRectTransform.pivot.y) * anchorRect.height * (1 - ownRectTransform.localScale.y));
 
         offsetMinOld = ownRectTransform.offsetMin;
         offsetMaxOld = ownRectTransform.offsetMax;
     }
 }
 
 //X and Y set the position of the Pivot relative to the parent Rect
 //Anchor X and Y set where on the parent Rect the Pivot is relative to
 //Where (0, 0) is the bottom left corner of parent Rect and (1, 1) the top right


anchorToolEditor.cs

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(anchorTool))]
 class anchorToolEditor : Editor {
     void OnSceneGUI() {
         if (Event.current.type == EventType.MouseUp && Event.current.button == 0) {
             anchorTool myTarget = (anchorTool)target;
             myTarget.StopDrag();
         }
     }
 }
 
 //This script must be placed in a folder called "Editor" in the root of the "Assets"
 //Otherwise the script will not work as intended

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OleksandrMartysh · Mar 01, 2017 at 05:56 PM 0
Share

Hey. Thank you for those .cs files. But they dont work for me. U made two files, placed them into Assets/Editor but nothing happens. Inspector was nt changed at all - no additional buttons for controling anchors :( I tried to move my rectTransform object with mouse, but only Xpos and Ypos were changed, not anchors.

avatar image snair692 OleksandrMartysh · Sep 27, 2018 at 03:55 AM 0
Share

Simple -- the editor script goes in the editor folder, the first script "anchorTool.cs" goes elsewhere and actually gets attached to the gameobjects you are wanting to use this feature with.

avatar image
2

Answer by stephane.lallee · Mar 02, 2016 at 04:36 AM

@ Phedg1 , I took your code and made a more convenient version. One editor script only, will do the job for all the rectTransforms in the scene.

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using UnityEngine;
 
 [InitializeOnLoad]
 public class AnchorToolsEditor : EditorWindow
 {
     static AnchorToolsEditor()
     {
         SceneView.onSceneGUIDelegate += OnScene;
     }
 
     private static void OnScene(SceneView sceneview)
     {
         if (Event.current.type == EventType.MouseUp && Event.current.button == 0)
         {
             UpdateAnchors();
         }
     }
 
     public void OnDestroy()
     {
         SceneView.onSceneGUIDelegate -= OnScene;
     }
 
     static public Rect anchorRect;
     static public Vector2 anchorVector;
     static private Rect anchorRectOld;
     static private Vector2 anchorVectorOld;
     static private RectTransform currentRectTransform;
     static private RectTransform parentRectTransform;
     static private Vector2 pivotOld;
     static private Vector2 offsetMinOld;
     static private Vector2 offsetMaxOld;
 
     static public void UpdateAnchors()
     {
         TryToGetRectTransform();
         if (currentRectTransform != null && parentRectTransform != null && ShouldStick())
         {
             //Debug.Log("[Anchors Tools] Updating");
             Stick();
         }
     }
 
     static private bool ShouldStick()
     {
         return (
             currentRectTransform.offsetMin != offsetMinOld ||
             currentRectTransform.offsetMax != offsetMaxOld ||
             currentRectTransform.pivot != pivotOld ||
             anchorVector != anchorVectorOld ||
             anchorRect != anchorRectOld
             );
     }
 
     static private void Stick()
     {
         CalculateCurrentWH();
         CalculateCurrentXY();
 
         CalculateCurrentXY();
         pivotOld = currentRectTransform.pivot;
         anchorVectorOld = anchorVector;
 
         AnchorsToCorners();
         anchorRectOld = anchorRect;
 
         UnityEditor.EditorUtility.SetDirty(currentRectTransform.gameObject);
     }
 
     static private void TryToGetRectTransform()
     {
         currentRectTransform = UnityEditor.Selection.activeGameObject.GetComponent<RectTransform>();
         parentRectTransform = currentRectTransform.parent.gameObject.GetComponent<RectTransform>();
 
     }
 
     static private void CalculateCurrentXY()
     {
         float pivotX = anchorRect.width * currentRectTransform.pivot.x;
         float pivotY = anchorRect.height * (1 - currentRectTransform.pivot.y);
         Vector2 newXY = new Vector2(currentRectTransform.anchorMin.x * parentRectTransform.rect.width + currentRectTransform.offsetMin.x + pivotX - parentRectTransform.rect.width * anchorVector.x,
                                   -(1 - currentRectTransform.anchorMax.y) * parentRectTransform.rect.height + currentRectTransform.offsetMax.y - pivotY + parentRectTransform.rect.height * (1 - anchorVector.y));
         anchorRect.x = newXY.x;
         anchorRect.y = newXY.y;
         anchorRectOld = anchorRect;
     }
 
     static private void CalculateCurrentWH()
     {
         anchorRect.width = currentRectTransform.rect.width;
         anchorRect.height = currentRectTransform.rect.height;
         anchorRectOld = anchorRect;
     }
 
     static private void AnchorsToCorners()
     {
         float pivotX = anchorRect.width * currentRectTransform.pivot.x;
         float pivotY = anchorRect.height * (1 - currentRectTransform.pivot.y);
         currentRectTransform.anchorMin = new Vector2(0f, 1f);
         currentRectTransform.anchorMax = new Vector2(0f, 1f);
         currentRectTransform.offsetMin = new Vector2(anchorRect.x / currentRectTransform.localScale.x, anchorRect.y / currentRectTransform.localScale.y - anchorRect.height);
         currentRectTransform.offsetMax = new Vector2(anchorRect.x / currentRectTransform.localScale.x + anchorRect.width, anchorRect.y / currentRectTransform.localScale.y);
         currentRectTransform.anchorMin = new Vector2(currentRectTransform.anchorMin.x + anchorVector.x + (currentRectTransform.offsetMin.x - pivotX) / parentRectTransform.rect.width * currentRectTransform.localScale.x,
                                                  currentRectTransform.anchorMin.y - (1 - anchorVector.y) + (currentRectTransform.offsetMin.y + pivotY) / parentRectTransform.rect.height * currentRectTransform.localScale.y);
         currentRectTransform.anchorMax = new Vector2(currentRectTransform.anchorMax.x + anchorVector.x + (currentRectTransform.offsetMax.x - pivotX) / parentRectTransform.rect.width * currentRectTransform.localScale.x,
                                                  currentRectTransform.anchorMax.y - (1 - anchorVector.y) + (currentRectTransform.offsetMax.y + pivotY) / parentRectTransform.rect.height * currentRectTransform.localScale.y);
         currentRectTransform.offsetMin = new Vector2((0 - currentRectTransform.pivot.x) * anchorRect.width * (1 - currentRectTransform.localScale.x), (0 - currentRectTransform.pivot.y) * anchorRect.height * (1 - currentRectTransform.localScale.y));
         currentRectTransform.offsetMax = new Vector2((1 - currentRectTransform.pivot.x) * anchorRect.width * (1 - currentRectTransform.localScale.x), (1 - currentRectTransform.pivot.y) * anchorRect.height * (1 - currentRectTransform.localScale.y));
 
         offsetMinOld = currentRectTransform.offsetMin;
         offsetMaxOld = currentRectTransform.offsetMax;
     }
 }
 ////This script must be placed in a folder called "Editor" in the root of the "Assets"
 ////Otherwise the script will not work as intended
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jcuriel · Mar 31, 2017 at 05:56 PM 0
Share

This is awesome! Thanks! Now I need to find a way for Unity to record the changes when animating. Currently it seems to only keep track of the Anchored Position, but doesn't key the Anchor $$anonymous$$in and $$anonymous$$ax when they change. Great Script though!

avatar image the_mitya · Jan 30, 2019 at 09:20 PM 0
Share

Thank you! I find it very useful for me!

avatar image huulong · Apr 28, 2019 at 03:38 PM 0
Share

Thanks, it's working!

But I don't always want to stick the anchors to the rect boundaries, so I added a toggle to enable that behaviour: stickAnchorsToRect = EditorGUILayout.Toggle("Stick Anchors to Rect", stickAnchorsToRect); saved in my prefs EditorPrefs.SetBool("AnchorToolsEditor.stickAnchorsToRect", stickAnchorsToRect);. I also fixed the Undo by replacing the legacy SetDirty(now only for non-scene objects) with Undo.RecordObject(currentRectTransform, "Stick Anchors");. @jcuriel it may fix your issue with animation recording. Finally, I (actually my IDE) noticed that anchorVector is never assigned to and is always 0. Can we remove it completely from the calculation? The current one seems to work without, even if the parent has a particular position.

If you want to check the full script, I've pushed it on BitBucket. It may change a bit later as I add new stuff, so if you want the exact version I was using when I posted this comment, check this commit.

As I said, I may add more stuff later, but for now I have nothing important enough to make another answer (and for bugfixes, I'd rather the authors edit their answers than creating a new one anyway).

avatar image
1

Answer by achimmihca · Nov 20, 2019 at 05:44 PM

Based on the solution here, I created some MenuItem that will perform "move anchors to corners" via keyboard shortcut: https://gist.github.com/achimmihca/4f053a81983c91bdf661214e1b88f65b

You might also be interested in the other way around, i.e., "move corners to anchors": https://gist.github.com/achimmihca/a8d92347f2fe88050fae5f381eff9a6d

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrVentures · Mar 18 at 10:32 PM 0
Share

Worked great, thanks!

avatar image
0

Answer by PelusoWarro · Apr 15, 2020 at 01:40 PM

this is what you are looking for. thank me later https://github.com/ceyhuntasci/UnityEasyAnchor/blob/master/Source/Editor/RectTransformContextMenu.cs

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

should i avoid using anchor stretch? 0 Answers

Anchors missing 0 Answers

Canvas anchor points are only near center 0 Answers

Unity RectTransform y value changing to height after exiting play mode? 0 Answers

RectTransform values on the editor change randomly... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges