Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
23
Question by DaiMangouDev · Jul 15, 2015 at 09:42 PM · c#uitransform

How to set the new Unity UI Rect Transform Anchor Presets via c# script ?

Hi , how can we set the anchor prest to something like .... Top Stretch, as shown in the picture ?

alt text

alt text

i don't the the default values

rtap.jpg (39.8 kB)
inff.jpg (25.2 kB)
Comment
Add comment
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

7 Replies

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

Answer by allenallenallen · Jul 16, 2015 at 04:10 AM

You control the anchor point with these values:

alt text

Using code, you can control the anchors with:

http://docs.unity3d.com/ScriptReference/RectTransform-anchorMin.html http://docs.unity3d.com/ScriptReference/RectTransform-anchorMax.html http://docs.unity3d.com/ScriptReference/RectTransform-pivot.html

All of them are Vector2.

 public RectTransform panelRectTransform;
 
 // Something like this.
 void Start()
 {
    panelRectTransform.anchorMin = new Vector2(1, 0);
    panelRectTransform.anchorMax = new Vector2(0, 1);
    panelRectTransform.pivot = new Vector2(0.5f, 0.5f);
 }


Furthermore, the anchor points and these variable are used the most often so in case you aren't sure what they are:

anchoredPosition controls the positions X and Y positions of the GUI based on the anchor points. sizeDelta controls the width and height of the GUI.

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 DaiMangouDev · Jul 16, 2015 at 02:30 PM 1
Share

great , thanks . I totally understand now

avatar image
5

Answer by FlightOfOne · Jan 26, 2018 at 06:42 PM

  public void SetAndStretchToParentSize(RectTransform _mRect, RectTransform _parent)
         {
             _mRect.anchoredPosition = _parent.position;
             _mRect.anchorMin = new Vector2(1, 0);
             _mRect.anchorMax = new Vector2(0, 1);
             _mRect.pivot = new Vector2(0.5f, 0.5f);
             _mRect.sizeDelta = _parent.rect.size;
             _mRect.transform.SetParent(_parent);
         }
 

In case if anyone wants to extend the RecTransform class:

  public static void SetAndStretchToParentSize(this RectTransform _mRect, RectTransform _parent)
     {
         _mRect.anchoredPosition = _parent.position;
         _mRect.anchorMin = new Vector2(1, 0);
         _mRect.anchorMax = new Vector2(0, 1);
         _mRect.pivot = new Vector2(0.5f, 0.5f);
         _mRect.sizeDelta = _parent.rect.size;
         _mRect.transform.SetParent(_parent);
     }




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
avatar image
3

Answer by U_Ku_Shu · Aug 04, 2016 at 12:22 PM

http://answers.unity3d.com/questions/1225118/solution-set-ui-recttransform-anchor-presets-from.html

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
avatar image
0

Answer by alvmoral · Dec 20, 2018 at 07:14 PM

Try this

    public Vector2 UIGetElementDimension (GameObject mGameObject) {
 
         Vector2 Res = -Vector2.one;
         RectTransform RectTransform = mGameObject.GetComponent<RectTransform>();
         if (RectTransform != null) {
             Res = RectTransform.sizeDelta;          // Read GameObject Dimensions                                                     
         }
         return Res;
         
     }
 
     public void UIMaximizeElement(GameObject mGameObject, GameObject Parent = null, float Left = 0, float Right = 0, float Top = 0, float Bottom = 0) {
 
         RectTransform RectTransform = mGameObject.GetComponent<RectTransform>();                    // Get Rect Transform Component from element
 
         if (RectTransform != null) {
 
             Vector2 ParentSize = -Vector2.one;
             if (mGameObject.transform.parent != null)                                               // Get Size of Parent
                 ParentSize = UIGetElementDimension(mGameObject.transform.parent.gameObject);        
 
             else if (Parent != null)
                 ParentSize = UIGetElementDimension(Parent);
 
             RectTransform.anchorMin = new Vector2(0, 0);                                            // Set Location respect to Axes, same thing then doit manually in Anchor Min and Max in inspector of Rect Transform
             RectTransform.anchorMax = new Vector2(1, 1);                                             
             
             RectTransform.pivot = new Vector2(0.5f, 0.5f);                                          // Pivot in the Middle
 
             if (ParentSize != -Vector2.one) {
 
                 float SizeWidth = ParentSize.x - Left - Right;                                      // Calculate dimensions of Element;
                 float SizeHeight = ParentSize.y - Top - Bottom;
 
                 RectTransform.offsetMin = Vector2.zero;                                             
                 RectTransform.offsetMax = new Vector2(SizeWidth, SizeHeight);                       // Set dimensions 
                 
                 RectTransform.anchoredPosition = new Vector2(SizeWidth / 2 + Left, SizeHeight / 2 + Bottom);   // Anchored Position set automatically Left, Top, Right and Bottom
             }
 
 
         }
 
     }
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
avatar image
0

Answer by unity_iLxAeHlWxpIIXw · Apr 01, 2019 at 11:21 AM

check the link it will help you https://github.com/CG-Tespy/Unity-RectTransform-Preset-Utils

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
  • 1
  • 2
  • ›

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

30 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

Related Questions

How to change the Top and Bottom (rect.yMin and yMax) properties of a rectTransform, in a script? 2 Answers

How do I obtain children on UI canvas text 0 Answers

Multiple Cars not working 1 Answer

Scale UI element On One Side 0 Answers

Camera transformation upon a pressing a key 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