- Home /
EZ still isn't positioning right
still having trouble trying to fix my objects into the right places for different screen sizes for android.
i've tried using the following code (given by robertu) in the start function but it partly works. it fixes my objects in place for every screen size but it wont allow me to place them in the right place so its no use?? unless im doing something wrong.
// Unity Answers
int width = (int)((float)Screen.width / 1280 * 100);
int height = (int)((float)Screen.height / 800 * -100);
EZScreenPlacement ezsp = GetComponent();
ezsp.PositionOnScreen(width, height, 10);
Here is the link to download my work...i've probably deleted this code in my work tho, im not sure.
https://www.dropbox.com/s/9vzdirpvktkxlkw/EZ_Positioning.unitypackage
p.s. thank you robertu for all the effort you put in to answering my questions.
When I struggle with things like this, I start with the simplest example I can and then build on it. So I suggest you reedit your question above to include two (low-res) images with two of the device aspect rations you want. Add a single graphic to each image correctly placed and then describe what to you is the correct placement. I'm still traveling and have only a limited access to Unity, but I'll respond when I can...or perhaps someone else on the list can give you an answer.
Note the EZGUI position script may not be the best answer. There are other ways to place items.
yeah I'm starting to realise the EZGUI position isnt that good.
and i done the same, i removed everything from my project except a few buttons so i can concentrate on this irritating problem. ok take your time, if i find anything ill post it here to let you know
I'm back in town and can help you, but before I do, I need a clear description of the correct layout. As mentioned in my comment above maybe the place to start is with a single object and two screen shots...one with each aspect ratio and a clear description of where it should be placed.
EZGUI's EZScreenPlacement script is about pixel placement relative to different anchor points. Until I understand how you want items placed (and how it varies between resolutions), I cannot decide even if EZScreenPlaement is the right tool.
![alt text][1]
ok i've reduced it down to only 2 items. I want the directional buttons to be place in the bottom left hand corner and the coin icon in the top right hand as illustrated in the image attached. Is that clear enough or are you after some other type of information?
thank you [1]: /storage/temp/10807-screen+shot+2013-05-08+at+00.10.04.png
Answer by robertbu · May 14, 2013 at 04:02 PM
Not sure where you live in the world, but it was Mother's day here on Sunday, and activities left me less time than I expected. As for your problem. This is how I would setup your UI:
All UI elements should be on their own layer (different than your game layer), so define a new layer and give it to all your UI elements.
Consider using a separate camera for displaying these UI elements. Setup the projection as orthographic, the background as depth only, and set the culling mask to the layer of your elements, and the Depth to a value higher than your game camera. I would position the camera pointing down the 'Z' axis, so that (0,0) is in the center of the screen.
If you setup a separate camera, you will need to set the UI Camera's array in the UI manager to this camera only.
Pick a default resolution for your game. This is the resolution you will author your graphics for.
Set the orthographic size of the camera. I like to setup things up so that a fraction (1/100) of the size maps into pixels, so a size of 10.0 would represent 1000 pixels. There are precise ways of doing this, but here is a quick and dirty solution that gets close: 1) put a sprite in your project centered at (0,0,0), 2) size to match the screen size divided by 100. So if your default screen size was 1024 x 768, the graphic size (width, height in the UI Button or Packed Sprite script) would be 10.24 x 7.68. Now adjust the orthographic size (looking at the Game window) until the graphic just fits the camera.
In Photoshop, author your graphics so they are correctly sized for the default screen size of your game.
Bring them into Unity. Once attached to the script (using the UI Control Editor or the Timeline Editor). Build your atlas, and briefly run the game. Make sure your screen size is set to the default size in the game window, turn 'Pixel Perfect' on then immediately turn it back off. This will size the graphic to be pixel perfect for your default screen size. Note you can also use Tools/AB Software /Size Sprites for this task.
If you've setup the above, the sizing for the different displays should take care of itself. That is, your sizes will be in world units and therefore will change with the display. Your game will only be pixel perfect (i.e. one pixel on the texture represents one pixel on the screen), for the default screen size you authored for.
As for positioning (and assuming you want placement based on pixels as you indicated), this is how it can be done. You need to set both the the Anchor in the UI Button script and the Relative To in the EZScreenPlacement script. So for example, if you wanted a graphics placed 50 pixels from the top and the right, you would first set the anchor in the UI Button script to UPPER_RIGHT. Then you would set the Relative To in EZScreenPlacement, to SCREEN_LEFT/SCREEN_TOP and finally you would set Screen Pos to (-50,-50,0). The 'Z' parameter only matters if you are stacking elements.
Compound object like your arrow keys will require a bit more work. They will need to be authored in their positions and rotation in Photoshop. The easiest things is to create them as a single file with one arrow per layer. Then save them as four different images (eight if you included the selected state). You will need to set 'Do Not Trim Images' to true in the UIButton script, or you can place an almost completely transparent pixel in the four corners of the images. You will need a separate EZScreenPlacement script for each of these buttons.
A lot of fiddling, but no scripting. The only issue I see is if you have wildly different screen resolutions, a size of 50 pixels will represent substantially different offsets form objects. This could be fixed with a bit of scripting. A simple script that detects the difference between the default screen vertical and Screen.height and resets the placement by the ratio should work.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make UI appear in camera scene view? 0 Answers
Game object position. 1 Answer