I have a scroll view, in its content object there’s a GridLayoutGroup component(the content object has its own position and scales instead of (0,0,0) or (1,1,1) ), and under it there’re several images. During runtime I want to take out an image and set its parent to other UI object, but I want the image remain its position on screen.
However I tried all the following ways, all ended up with wrong position on screen(the image moved sometimes even off screen).
1 Use SetParent method on the image object(tried both True or false as second parameter):
2 Use SetParent method in the 1st way, then give a position to the image manually, but id does not appear at the mouse position(this code works fine for other objects that are not in a layoutgroup):
imageObject.transform.SetParent(otherObj, True); var p = Camera.main.ScreenToWorldPoint(Input.mousePosition); imageObject.transform.position = new Vector3(p.x, p.y, 0);
3 Set the position to its original value after calling SetParent.
imageObject.transform.SetParent(otherObj, True); var p = imageObject.transform.position; imageObject.transform.position = new Vector3(p.x, p.y, 0);
4 don’t use any code but during runtime in the editor, manually drag the image out to targeted parent object. Still its position is changed.
5 don’t use any code but during runtime in the editor, manually uncheck the GridLayoutGroup to disable it. Still the image position is changed.
Why the 3rd way is not working? I think probably the transform.position of the image object is not used so the value is not used so the value is not where the position is on screen, or something happened in the end of the frame so my reseting of the position is useless.
More info: my canvas render mode is set to “Screen Space-Camera”. But even changed it to overlay, it remained the same result. Canvas scaler mode is “Scale with Screen Size”, screen match mode is “Expand”.
So what should I do to take out the image out of the GridLayoutGroup but let it stays where it is on screen? Thanks!