Search Unity

  1. Going to #UniteBerlin? Bring a buddy & get 25% off the 2nd ticket! More info here.
    Dismiss Notice
  2. Famed game designer and creator of The Sims, SimCIty, and Spore is looking for a 3D artist to join their team. Join the challenge.
    Dismiss Notice
  3. Unity 2017.4 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. We've opened a new World Building sub-forum to faciltate all your world building discussions! Drop in and ask any ProBuilder, ProGrids, or PolyBrush questions.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[Script] Simple script that automatically adjust anchor to GUI object size (Rect Transform)

Discussion in 'Unity UI & TextMesh Pro' started by porygon, Sep 21, 2014.

  1. porygon

    porygon

    Joined:
    Dec 28, 2013
    Posts:
    3
    I have been playing around with the new GUI in 4.6. I have found out that there is no automation that allows the user to surround a GUI object (panel, button, etc.) with anchors on all sides. This would effectively make GUI object scales and stay at the same position relatively to the screen size. Therefore, I wrote a simple script that help me do that, hope this help,

    Note: This only applies to GUI object with parents.. do not apply it on the root canvas



    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class UGUIMenu : MonoBehaviour
    6. {
    7.     [MenuItem ("UI/Anchor Around Object")]
    8.     static void uGUIAnchorAroundObject()
    9.     {
    10.         var o = Selection.activeGameObject;
    11.         if (o != null && o.GetComponent<RectTransform>() != null)
    12.         {
    13.             var r = o.GetComponent<RectTransform>();
    14.             var p = o.transform.parent.GetComponent<RectTransform>();
    15.            
    16.             var offsetMin = r.offsetMin;
    17.             var offsetMax = r.offsetMax;
    18.             var _anchorMin = r.anchorMin;
    19.             var _anchorMax = r.anchorMax;
    20.            
    21.             var parent_width = p.rect.width;      
    22.             var parent_height = p.rect.height;  
    23.            
    24.             var anchorMin = new Vector2(_anchorMin.x + (offsetMin.x / parent_width),
    25.                                         _anchorMin.y + (offsetMin.y / parent_height));
    26.             var anchorMax = new Vector2(_anchorMax.x + (offsetMax.x / parent_width),
    27.                                         _anchorMax.y + (offsetMax.y / parent_height));
    28.            
    29.             r.anchorMin = anchorMin;
    30.             r.anchorMax = anchorMax;
    31.            
    32.             r.offsetMin = new Vector2(0, 0);
    33.             r.offsetMax = new Vector2(1, 1);
    34.             r.pivot = new Vector2(0.5f, 0.5f);
    35.            
    36.         }
    37.     }
    38. }
    39.  
     

    Attached Files:

  2. Text

    Text

    Joined:
    Sep 22, 2014
    Posts:
    2
    Hi thank you for script but you should change this : r.offsetMax = new Vector2(1, 1);
    into this r.offsetMax = new Vector2(0, 0); overwise ui element scales slitly and after performing this script
    several times ui element complitly changes it size .
     
    chrisoishi and LVBen like this.
  3. LVBen

    LVBen

    Joined:
    Apr 1, 2013
    Posts:
    16
    Awesome! Thank you so much to both of you! This is going to save me lots of time adjusting anchors!
     
  4. prototype868

    prototype868

    Joined:
    Jan 23, 2016
    Posts:
    1
    Thank you very much! I have tested it on Unity 5.3.4f1 and it works perfectly.
    This is the tool number one for me, I am really happy for your kindness :)
     
  5. gvaughan

    gvaughan

    Joined:
    Jan 28, 2010
    Posts:
    32
    Works great for me too! I'm using it at runtime to readjust after dragging an element - much cleaner than the mess I started writing :)
     
  6. RomuloFerreira

    RomuloFerreira

    Joined:
    Apr 22, 2014
    Posts:
    1
    This is really awesome, thank you :D
     
  7. Smidgens

    Smidgens

    Joined:
    Nov 2, 2014
    Posts:
    3
    5.6.0. Works like a charm, and just saved me hours of work aligning everything correctly. Cheers!
     
  8. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    302
    I would also add:

    Code (csharp):
    1. Undo.RecordObject(r, "Set anchors around object");
    under line 13 so that undo/redo works and Unity also recognises that the scene is dirty!
     
    George-Dolbier likes this.
  9. Azes923

    Azes923

    Joined:
    Jun 7, 2017
    Posts:
    2
    Works perfectly with Unity 2017 3.03f! Thanks for the awesome script!
     
  10. sjakur

    sjakur

    Joined:
    Apr 24, 2016
    Posts:
    3
    omg i love you!!
     
  11. Xevetor

    Xevetor

    Joined:
    Oct 14, 2017
    Posts:
    2
    can't believe there's still no default
     
  12. gamefish

    gamefish

    Joined:
    Jul 5, 2013
    Posts:
    7
    Great code, but how should I modify the code to let the sub UI element preserve it's size or ratio when screen size changed?
     
  13. gamefish

    gamefish

    Joined:
    Jul 5, 2013
    Posts:
    7
    This looks nasty but it works for my needs, just change
    Code (CSharp):
    1. r.anchorMin = anchorMin;
    2. r.anchorMax = anchorMax;
    3.  
    4. r.offsetMin = new Vector2(0, 0);
    5. r.offsetMax = new Vector2(1, 1);
    to:

    Code (CSharp):
    1. r.anchorMin = (anchorMin+anchorMax)/2;
    2. r.anchorMax = (anchorMin+anchorMax)/2;
    3.  
    4. Vector2 objSize = r.offsetMax-r.offsetMin;
    5. r.offsetMin = -objSize/2;
    6. r.offsetMax = objSize/2;
    you can preserve element aspect ratio
     
    Last edited: Apr 16, 2018 at 5:07 PM