• 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
17
Question by HarryE22 · Jan 28, 2015 at 09:01 PM · uirecttransform

Access "Left", "Right", "Top" and "Bottom" of RectTransform via script

Hi guys. I have a quick question about RectTransforms.

I need to access the "Left" property in a UI Component FROM SCRIPT so I can s$$anonymous$$ft it along a bit in certain situations. What's the easiest/simplest way to do t$$anonymous$$s? Is it a value that is inferred from other stuff, like the sizeDelta and the position, and if so, what is the best way to manipulate those to get that apparent change?

Thanks, Harry

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
2

Answer by digzou · Feb 07, 2015 at 08:40 AM

Bump. Hope it gives you some idea.

http://forum.unity3d.com/threads/setting-top-and-bottom-on-a-recttransform.265415/

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
27

Answer by Janibasha · Aug 02, 2017 at 12:19 PM

Actually, we cannot access the top, left, right, bottom individually as I know. so "Left", "Right", "Top" and "Bottom" of RectTransform properties so we have offsetMin and offsetMax w$$anonymous$$ch return a vector as follows

PanelSpacerRectTransform.offsetMin = new Vector2(0, 0); // new Vector2(left, bottom); PanelSpacerRectTransform.offsetMax = new Vector2(-360, -0); // new Vector2(-right, -top);

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
77

Answer by Eldoir · Mar 10, 2019 at 02:15 PM

Hi, I know t$$anonymous$$s is an old thread but I landed here in 2019 so I guess t$$anonymous$$s is still valuable. I just created a little script to answer your question. Create a new script called RectTransformExtensions (or whatever you want), and put that in there:

 using UnityEngine;

 public static class RectTransformExtensions
 {
     public static void SetLeft(t$$anonymous$$s RectTransform rt, float left)
     {
         rt.offsetMin = new Vector2(left, rt.offsetMin.y);
     }
 
     public static void SetRight(t$$anonymous$$s RectTransform rt, float right)
     {
         rt.offsetMax = new Vector2(-right, rt.offsetMax.y);
     }
 
     public static void SetTop(t$$anonymous$$s RectTransform rt, float top)
     {
         rt.offsetMax = new Vector2(rt.offsetMax.x, -top);
     }
 
     public static void SetBottom(t$$anonymous$$s RectTransform rt, float bottom)
     {
         rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
     }
 }

Now you can call these methods like t$$anonymous$$s :

 myRectTransform.SetLeft(100);

Hope t$$anonymous$$s helps!

Comment
Add comment · Show 4 · 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 cmann · May 31, 2019 at 09:20 PM 2
Share

Really useful. Thanks.

avatar image Jon_Brant · Feb 18, 2020 at 04:30 PM 0
Share

More utility scripts for the common folder! Thanks!

avatar image Smireles · Sep 13, 2020 at 06:27 AM 1
Share

Thank you!

avatar image Fatamos · Mar 28, 2022 at 10:35 AM 0
Share

Could this be implemented to DoTween? This works perfectly but I would like to add some delay animation to it. Do you think it's possible to tweak this around? Thanks!

avatar image
1

Answer by boorch · Apr 07, 2019 at 11:54 AM

@Eldoir thanks for t$$anonymous$$s!

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
4

Answer by Ay-mak · Apr 07, 2019 at 12:23 PM

The easiest and cleanest solution is to simply use RectTransform.SetInsetAndSizeFromParentEdge. See t$$anonymous$$s page for more information : https://docs.unity3d.com/462/Documentation/ScriptReference/RectTransform.SetInsetAndSizeFromParentEdge.html

If you want your object to fill the entire space of the parent, just do t$$anonymous$$s:

         RectTransform rectTransform = gameObjectButton.GetComponent<RectTransform>();
         rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 0);
         rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
         rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
         rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 0);
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 IgorZ · Aug 10, 2019 at 06:13 AM 1
Share

Well, what it actually did in my case is collapsing the control. To be honest, this method is a complete mystery. I've examined the code and I can not suggest a single case when I'd use it.

Unity is a good engine, but their docs suck. With years the "entry threshold" gets higher and higher. One day, they will simply stop getting new adopters. It will be a sad day for the open/free software community.

  • 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

RectTransform Pivot Point in the new UI? 1 Answer

Editing RectTransform scale 2 Answers

uGUI - resize RectTransform at runtime to fill entire screen 1 Answer

RectTransform.position of Panel always 0 in FullHD 0 Answers

Button not moving in X axis because of animation 0 Answers


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