Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
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 shift it along a bit in certain situations. What's the easiest/simplest way to do this? 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
72

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

Hi, I know this is an old thread but I landed here in 2019 so I guess this 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(this RectTransform rt, float left)
     {
         rt.offsetMin = new Vector2(left, rt.offsetMin.y);
     }
 
     public static void SetRight(this RectTransform rt, float right)
     {
         rt.offsetMax = new Vector2(-right, rt.offsetMax.y);
     }
 
     public static void SetTop(this RectTransform rt, float top)
     {
         rt.offsetMax = new Vector2(rt.offsetMax.x, -top);
     }
 
     public static void SetBottom(this RectTransform rt, float bottom)
     {
         rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
     }
 }

Now you can call these methods like this :

 myRectTransform.SetLeft(100);

Hope this 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

$$anonymous$$ore 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 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
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 which 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
3

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

The easiest and cleanest solution is to simply use RectTransform.SetInsetAndSizeFromParentEdge. See this 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 this:

         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 exa$$anonymous$$ed 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.

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
1

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

@Eldoir thanks for this!

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

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

Transform to RectTransform Conversion 1 Answer

Incorrect RectTransform width value when trying to read child RectTransform width. 0 Answers

Health Bar at Player Transform 4 Answers

How do I set rectTransform properties of one image to another which differ in anchor and parent transforms (script)? 0 Answers

Setting negative value for rectTransform sizedelta !!! 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