- Home /
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!
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
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?
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 Up$$anonymous$$iddle edge...that way they don't change their size when their parent's bottom changes.
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 anchor$$anonymous$$in and anchor$$anonymous$$ax 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.
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.
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 )
Thank you for your answer, I think I managed to do something similar to what I wanted!
Your answer
Follow this Question
Related Questions
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
change anchor point on object 1 Answer