• 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
Question by javierGarri · Feb 06, 2015 at 04:18 PM · rectanchorpresetpresets

Hi, How do I modify the Preset from the Rect Transform's anchors by code? Thank you!

Hello, I'm trying to modify the anchor's preset at runtime. If I do that in the inspector it's ok, but I don't know how to do it by code. I tried to modify the anchors Min and Max parameters, but it changes my image position and I don't want that. Thank you!

Comment

People who like this

0 Show 5
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 fafase · Feb 06, 2015 at 04:41 PM 0
Share

I think I provided an answer to a similar question just about an hour ago. Did you try it?

http://answers.unity3d.com/questions/792642/unity-46-beta-changing-anchors-position-through-sc.html

avatar image Mmmpies · Feb 06, 2015 at 04:49 PM 0
Share

I think moving the image anchors at runtime will move the image. You can do it in the editor because that isn't calculating the offsets of a new screen resolution. What you're asking it to do is be in this position offset from the parent panel by the anchors. If you move the anchors at runtime then the image recalculates it's offsets from the anchors based on how you had it in the editor.

What are you trying to do with the image, just move it's location/size?

avatar image javierGarri · Feb 06, 2015 at 05:01 PM 0
Share

I have a scrollRect that changes its bottom position at runtime, and some images inside the scrollRect. So in order to keep the images at a correct size for every screen size, their anchors must be where their corners are when the App starts. But because of that, when the scroll's bottom goes down the size of the images modifies along with it. In order to avoid that I wanted to chage their anchors to the UpMiddle edge...that way they don't change their size when their parent's bottom changes.

avatar image javierGarri · Feb 06, 2015 at 05:02 PM 0
Share

Yes thanks, I replied it, I tried but it doesn't work. What you said makes the anchors fit the position of the corners. But the problem is that by setting anchorMin and anchorMax to (0,0), both corners and anchors go to (0,0)...so then it makes no sense to modify the offset cause there is no offset anymore.

avatar image Mmmpies · Feb 06, 2015 at 05:08 PM 0
Share

Do you have a verticalLayoutGroup on the ScrollRects content panel. It'll be a lot easier letting the UI handle the width of your images.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by karma0413 · Feb 06, 2015 at 05:01 PM

You use:

 // g is the referenced gameObject that has this rectTransform on it.
 Vector2 positionOnScreen = new Vector2 {200f,20f};
 g.GetComponent <RectTransform> ().anchoredposition = positionOnScreen;
 

That is for setting the 2d position, you can also set it's 3d position, anchors, and pivot all using that same GetComponent command. To set the Anchor for example we look here at the manual documentation for RectTransform : Link for RectTransform and we see that it's variable is called anchorMax and there is another one called anchorMin. You would just set it the same way:

 // In the documentation you can clearly see when you click on anchorMin, that it want's a Vector2 variable
 //... it wants an X and a Y
 
 
 Vector2 = myPersonalAnchorForTheTopLeft = new Vector2 (20f,20f);
 g.GetComponent <RectTransform> ().anchorMin = myPersonalAnchorForTheTopLeft;


Remember that rectTransform.... collider.... scripts... lights....meshes.. ALL OF this stuff that you put onto gameobject in your scene are called Components. Some of these components can be called directly; like: this.transform.position

I think RectTransform has to be called using the GetComponent command.

And then once you have located it's reference, you can then not ONLY set values in here, and get the values from here....But, later you can also call functions and tell that RectTransform to do stuff if you want it to. ( more advanced )

Comment

People who like this

0 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 javierGarri · Feb 12, 2015 at 05:47 PM 0
Share

Thank you for your answer, I think I managed to do something similar to what I wanted!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Checking if SpriteAtlas corresponds a preset 1 Answer

Questions about UGUI Rect Transform Anchors. How to make UI objects scale, correctly, with screen? 1 Answer

[Unity 4.6 Beta] Anchor snap to button (New UI System) 3 Answers

How to use multiple anchor presets? 2 Answers

[SOLUTION] set UI RectTransform Anchor Presets from code c# 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