- Home /
Correct space GUI elements?
How do I go about correctly spacing gui elements, say textures, that are 32x32 pixel blocks. I've got an orthogonal camera set up, size of 540.
How do I ensure that the elements will be correctly spaced? As of now, I can't even find a way to space them so they're side by side. That is, I can't find a way to convert pixel size to viewport coordinates.
for gui elements, set position, rotation and scale to 0. then obtain the following values,
int screenWidth = Screen.width;
int screenHeight = Screen.height;
then
GUITexture someGui;
then
someGui.pixelInset = new Rect(left, top, width, height);
you need to set left, top, width and height accordingly with screen size. like screenWidth * 0.1f etc...
This is a proper and safe way to place gui elements.
In order to fill some area with these elements, is to use loops, for height and for width.
on importing the texture, you set pixels to units. if you set it to 32, then your block width and height would be 1.
I'm not very sure what you are asking, but I hope above would help.
Good luck
What I'm trying to accomplish is make is so say 2 32x32 blocks are directly side by side, no overlap regardless of screen size. But I'm unsure how to get the correct spacing of these 32x32 blocks when I'm dealing with the viewport which is 0...1 on the x and y.
forget the code.
on GUITexture, change transform position, rotation and scale to 0.
change pixel inset values as the following;
for the 1st, 100,100,32,32;
for the second, 132,100,32,32;
try this and you can understand how GUITexture works. also check this;
https://docs.unity3d.com/Documentation/Components/class-GuiTexture.html
That's perfect! That pixel inset is exactly what I needed. Thanks for the heads up and the link!
Answer by Ermarrero · Apr 04, 2014 at 04:02 PM
Hi, not tested but you can try something like this:
public Rect guiRect;
public float spacing;
public Texture[] texture;
void OnGUI()
{
for(int i = 0; i < texture.Length; i++)
{
GUI.DrawTexture(new Rect(Screen.width / guiRect.x * (i * spacing), Screen.width / guiRect.y, texture[i].width + guiRect.width, texture[i].height + guiRect.height), texture[i],ScaleMode.StretchToFill);
}
}
Your answer