- Home /
Scale Gameobject with defined size in pixels
I'm still new with Unity (5.0.0f4 Personal).
I'm working on a simple memory game, three levels with different number of cards to find pairs. I want my game to display cards on different screen sizes of Android devices. But as I think, I don't understand enough screenspace and worldspace.
I calculated two positions, one for x and second for y coordinate for each card I want to display. But I have no idea, how to scale my card to the calculated card size (pixels)...
Vector3 pozicijaKarta = Camera.main.ScreenToWorldPoint(new Vector3(xpozicija, ypozicija, 10));
Any help will be appreciated!
You have access to Screen.width and Screen.height, which give you the current height and width of the game screen/window in pixels. You can use this information to scale any elements on the screen. Are the cards actual GameObjects with a Renderer, or are they just Textures drawn on screen?
Cards are GameObjects. The Cards I created in Blender and I'm using 3D animation for open/close on click.
Ok, in that case, it's gonna be very complicated. Theoretically, you could take the Renderer.bounds of a card object, take the eight corner points coordinates, convert those into Screen space, and compare those to the Screen.width and Screen.height to get an idea of their relative scale.
Yes, it's complicated.. I know how to get corner points (Vector3) and convert them to Screenspace(px). But how can I calculate the maximum scale of my cards to fit in any size of screen on devices? Is there any connection between Screenspace and Worldspace in Unity?
You can get the ScreenSpace coordinates of a cube's corner points. You also have the total Screen.width and Screen.height. This is all you need to deter$$anonymous$$e how many cubed will fit at their current size. Take the SP coordinate of one of the two right corners points and subract from it the SP coordinate of one of the two left corner points. This is the width of the cube in ScreenSpace pixels. Divide the Screen.width by the cube width and you know how many times the cube will fit horizontally at it's current size. This can also be done with ViewportSpace, where you use 1.0 ins$$anonymous$$d of Screen.width.
Answer by Pangamini · May 11, 2015 at 07:58 AM
The easiest way for you would be to use uGUI classes, Canvas and Rect Transform. These will do most of the dirty work for you. Just read the GUI Docs. You are not fixed to canvas renderers, so if you want to use 3D cubes, you still can. Take a look at layout components, they will provide super easy to use tools for what you need.
If I understand the concept of Canvas and Rect Transform, then I can't use them. $$anonymous$$y cards are prefab, created and displayed with Instantiate. Canvas and Rect Transform are great for menus. Or maybe I'm wrong?
Answer by Natasa · Oct 20, 2015 at 10:04 PM
@Cherno I did just like you said. Many thanks for your help! I had some troubles to find out how to get all 4 cube's corner points. If anyone else will have the same problem, I'll write simple instructions...
top left: min_x, max_y top right: max_x, min_y bottom left: min_x, max_y bottom right: max_y, min_y