- Home /
how to swap image position
hi, I am learning UI with canvas and images. I know how to translate image : name.GetComponent(RectTransform).anchoredPosition = new Vector2(x,y);
Now I would like to swap position of 2 images. I tried this :
name.GetComponent(RectTransform).anchoredPosition = name2.GetComponent(RectTransform).anchoredPosition;
but it doesnt work (which is obvious for experts). Do anyone know how to swap images position with a simple method ? or just how to move a image to another one's position. Thx.
Answer by Denscrivent · Jul 28, 2016 at 04:25 AM
use this instead
name.GetComponent<RectTransform>().AnchoredPosition = new Vector2(name2.GetComponent<RectTransform>().AnchoredPosition.x, name2.GetComponent<RectTransform>().AnchoredPosition.y);
Anchored Position is a Vector and you can't just assign a value of a Vector like assigning int value
Answer by saschandroid · Jul 28, 2016 at 06:13 AM
Vector2 l_temp = name.GetComponent(RectTransform).anchoredPosition;
name.GetComponent(RectTransform).anchoredPosition = name2.GetComponent(RectTransform).anchoredPosition;
name2.GetComponent(RectTransform).anchoredPosition = l_temp;
Answer by ayks · Jul 29, 2016 at 12:27 PM
Thanks for your answers.
I knew the trick to swap basic gameobject position with 1_temp. I tried the answers but the image dont move because the 2 images seems to have the same anchoredposition (both surrounded by their own rect transform).
I found the solution : they have to have the same "rect transform" to make it work, or/and the same "canvas" I dont know. But in my case the responsive dont work fine anymore. So hard to deal with those "images" t_t
Your answer
Follow this Question
Related Questions
How to get height of filled image ? 1 Answer
Moving a UI image by script 1 Answer
Have UI image float over a gameobject position 0 Answers
Unity 4.6: How to fit center byte array png in the Image UI? 1 Answer
UI Layering Problem! 2 Answers