- Home /
How to move a RectTransform on Pos X through code
I've spent a good amount of time trying to work this out. I haven't read any posts that clarify this issue, and the documentation is completely baffling to me.
So, how do you move a RectTransform on the Pos X through code? I know how to edit the width and height of a panel or image, but I have no idea how to move it on the x or y. I've tried a lot of different things and got weird results. I suspect it has something to do with the pivot of the panel?
Answer by devstudents · May 23, 2016 at 05:39 AM
ah, instead of this:
playerInventoryRectTransform.localPosition = Vector3.zero;
I should have used this:
playerInventoryRectTransform.anchoredPosition = Vector3.zero;
It works now, but readers let me know if I'm still missing something.
If anyone still comes across this thread, this is the best answer. DO NOT use local position. I was using this to change position for a character select and it worked fine in the editor but not in my build! After tons of head scratching and re-installing, it all came down to using the anchored position ins$$anonymous$$d of local. Thanks for the answer! I can finally sleep now.
Thank you!!! I wonder why they allow local position if it works like crap and ins$$anonymous$$d this work as local position should.
Certainly works! Just one thing, anchoredPosition is a Vector2, not a Vector3.
Answer by Marcus9 · Apr 18, 2017 at 04:32 PM
I had a similar problem. I was trying to instantiate a prefab and then have it populate the parent object. I could not move it using localPosition or change the size using sizeDelta. To fix the issue, I used RectTransform.SetInsetAndSizeFromParentEdge().
See example code below:
// GameObject obj is the parent object;
GameObject togglePrefab = Resources.Load("Prefabs/Toggle") as GameObject;
GameObject toggle = GameObject.Instantiate(togglePrefab, obj.transform) as GameObject;
RectTransform rt = toggle.GetComponent<RectTransform>();
rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, rt.rect.width);
rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, rt.rect.height);
Answer by corn · May 22, 2016 at 06:59 AM
You can modify its local position.
RectTransform myRectTransform = GetComponent<RectTransform>();
myRectTransform.localPosition += Vector3.right;
I've realised that my issue is co$$anonymous$$g from the fact that I'm parenting the panel to a canvas. Since the panel inherits the position of the canvas, it's basically impossible for me to get the Pos X and Pos Y to where I want them. I tried changing the canvas mode from screen overlay to world, but it didn't work. Here's my code:
//loads inventory prefab (a panel with the inventory script)
inventory = Resources.Load("Inventory") as GameObject;
//instantiates the prefab into the world and assigns it to a second gameObject variable
playerInventory = Instantiate(inventory).gameObject;
playerInventory.name = "Inventory";
//sets the parent to be the Canvas
playerInventory.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform);
//access the rectTransform of the palyerInventory
playerInventoryRectTransform = playerInventory.GetComponent<RectTransform>();
//attempt to make the rect transform x and y pos zero. hasn't work so far.
playerInventoryRectTransform.localPosition = Vector3.zero;
It seems to me the localPosition is in pixels, while the position is in units. Great catch, it works for me.
Answer by MaximilianPs · Aug 17, 2019 at 09:15 AM
I've gone completely crazy before getting a solution. xD
I was looking for a system to set the Anchor on the Top-Left, and passing value like: X; 10, Y: -10 Z: 0
So I've made a method to use in a for/loop cycle, I will pass a GameObject and a Vector3 with the same parameter just like I would do in inspector.
void tfSetup(GameObject go, Vector3 pos)
{
go.AddComponent<RectTransform>();
go.AddComponent<TextMeshProUGUI>();
go.AddComponent<TMP_Text>();
RectTransform rt = go.GetComponent<RectTransform>();
rt.SetParent(goParent.transform);
rt.anchoredPosition.Set(0, 0);
rt.anchorMin.Set(0, 1);
rt.anchorMax.Set(0, 1);
rt.pivot.Set(0, 0);
rt.transform.localPosition = pos;
}
Answer by donmetapod · Sep 27, 2018 at 04:56 PM
For me this code works quite well:
public void MoveLeft(RectTransform panel)
{
StartCoroutine(Move(panel, new Vector2(-1255, 0)));
}
IEnumerator Move(RectTransform rt, Vector2 targetPos)
{
float step = 0;
while (step < 1)
{
rt.offsetMin = Vector2.Lerp(rt.offsetMin, targetPos, step += Time.deltaTime);
rt.offsetMax = Vector2.Lerp(rt.offsetMax, targetPos, step += Time.deltaTime);
yield return new WaitForEndOfFrame();
}
}
The only note here is that I had to change the panel parameter from a ui panel to an empty game object for this to work properly.
Не удивляет, что процесс проходит за .5 секунды вместо 1й секунды?