- Home /
How to position UI Button in World Space so that it will be visible on Screen?
I want to create an UI-Button via script and position it in Worldspace so that it will be visible on Screen. I have a canvas that with the renderer mode set to world space. I can create a button via script. In the next step I want to position the button relative to the screen size but I dont know how convert the screen data into an actual 3D Position. I hope you guys understand what I mean and thank you for answering.
Try making a prefab of the button you want to use and place it where you want it and record the coordinates then delete the prefab. Then in a script you can instantiate the prefabbed button at a certain time using any number of methods, on the coordinates you recorded previously. Edit: When I say record the coordinates, I mean either memorize or write down(I write down) the position on the x, y, and z transform and it's offset for future reference. Also I should mention for this to work properly your also going to have to cause the instantiated buttonPrefab to become a parent of the canvas UI element when instantiated, and the Canvas scalar componenent must be set to Scale with Screen Size.
Answer by JRyu · Feb 03, 2015 at 11:36 PM
The answer that I found is to use the camera and the function ScreenToWorldPoint. The Method takes a Vector3 as an argumen. I created a new vector with the x and y coordinates that I want the UI Object to have on my screen and then I give it a z coordinate that I choose (The z does matter because of the size of the UI object, the further away from the camera the smaller it appears. I had to find what is best for my Objects). The method returns me a vector3 that represents this position in world-space. I use that position to instantiate the object.
Your answer
Follow this Question
Related Questions
Is there a way to do different things depending on whether the button is being held or tapped? 2 Answers
Problem moving Rigidbody with UI buttons 2 Answers
Unity can't use the script. 0 Answers
Build resolution errors 0 Answers
How to change buttons' positions 0 Answers