Let’s say I have several textures (count does not matter). I want to order them on the screen so they will be completely symmetrical, starting from the center of the screen. I can do this manually, right - one by one, but what if I want to make an algorithm that will automatically order and center the images no matter how many they are? Let’s say we have 5 images, there will be one in the middle and two images on the left and the right. If they are four, there will only be two images on the left and the right. Of course, all those images will have the same sizes.
I’m sure a for loop can be used for such thing, but I can’t imagine the algorithm itself. I’ve done such thing with a for loop, but it was starting from the left and that’s an easy one.
It’s pretty straight forward: sum the widths of the images, divide by two and subtract that from the horizontal screen-center position. Now you know the left-most position and can apply the same logic you’ve used before. Or, just use GUILayout: wrap the images in a BeginHorizontal/EndHorizontal block with a flexible space before and after the images. The flexible spaces will auto-expand to take up half of the ‘left over’ horizontal space, forcing the images to the center.
Wow, that was easy, but gotta question. Since my Untiy’s not working today, wouldn’t this code make the images overlap ?
for(i = 1; i <= numberWeapons; i++){
allSlotWidths = 140 * i;
GUI.BeginGroup(Rect(Screen.width/2 - allSlotWidths/2 , Screen.height-90, i * 140, 90));
GUI.DrawTexture(Rect(i * 140/2 - allSlotWidths/2 , weaponSlots[i], 130, 75), weaponSlot);
GUI.EndGroup();
}
They wouldn’t overlap, but they wouldn’t be centered either. As written, that code would skip (fail to draw) the first image (since you start i at 1), would place the second image at screen center, and would place each subsequent image to the left of the last. Not what you were going for…
My first image in the array starts from 1, not from 0, because of some system I made in the game. Now what happens is they overlap and the first one gets cut off a little.