I've been working on a project in Unity for a few months now, and I've gotten to the point where I am attempting to make a UI.
In my project, I have the game view forced to a 1:1 aspect ratio regardless of resolution, and then the remaining open space on the left + right sides of the screen are left to be given to the UI. I have it set up so that I have two different UIs, based on the amount of space left over after the 1:1 ratio forcing. Essentially, 16:9 and 16:10 (and similar) aspect ratio resolutions will get a larger UI with extra (but not necessarily needed on demand) data, while 4:3, 5:4, 3:2, etc. will get a smaller UI with only the necessities.
Also, in the case of a 5:4 aspect ratio, the extra space on the sides of the screen will be too small for even the small UI, and in this scenario I have the UI pushed inward to compensate, so that the UI is always completely visible.
I am currently having issues properly programming the above effects, and am wondering if anyone is able to help me in determining the issue (as it's driving me nuts, to be frank). The biggest issue is that the UI library that I am using uses a UI that is set in size. In this case, the UI is always 1024x768, and I cannot adjust this resolution at run time, however it does automatically scale properly to the screen regardless of its size. Overall, the issue is properly determining just how much space I have left in comparison to how big the UI will be on the screen.
Currently, I'm using the below code:
using UnityEngine;
using System.Collections;
public class CenterUI : MonoBehaviour {
public dfPanel gamePanel;
public dfControl smallLeftControl;
public dfControl largeLeftControl;
public dfControl smallRightControl;
public dfControl largeRightControl;
public double totalSize = -1;
public int movedAmount = 0;
void Update() {
//If the controls are missing, don't do anytihng.
if (!smallLeftControl || !smallRightControl || !largeLeftControl || !largeRightControl) { return; }
//If the screen width has not changed, don't do anything.
int curSize = Constants.mainCameraWidth + Constants.extraCameraWidth;
if (curSize == totalSize || Constants.extraCameraWidth == -1) { return; }
totalSize = curSize;
//Enable all 4 controls, to be disabled selectively later.
smallLeftControl.gameObject.SetActive(true);
smallRightControl.gameObject.SetActive(true);
largeLeftControl.gameObject.SetActive(true);
largeRightControl.gameObject.SetActive(true);
//Determine the ratio of the current screen size to the UI size (which always is 1024x768).
int extraSpace = Constants.extraCameraWidth;
double screenRatio = (double)extraSpace / 128.0;
extraSpace = (int)((double)extraSpace * screenRatio);
int smallNeededSpace = (int)((double)smallLeftControl.Size.x);
int largeNeededSpace = (int)((double)largeLeftControl.Size.x);
//If there is enough extra space on the sides of the screen, deactivate the smaller UI. If not, deactivate the larger UI.
if (extraSpace >= largeNeededSpace) {
smallLeftControl.gameObject.SetActive(false);
smallRightControl.gameObject.SetActive(false);
}
else {
largeLeftControl.gameObject.SetActive(false);
largeRightControl.gameObject.SetActive(false);
//If there is not enough extra space to hold even the small UI (5:4-like), move the UI in to ensure it is entirely visible.
if (movedAmount != 0) {
Debug.Log("Moving back.");
smallLeftControl.Position = new Vector2(smallLeftControl.Position.x - (movedAmount), smallLeftControl.Position.y);
smallRightControl.Position = new Vector2(smallRightControl.Position.x + (movedAmount), smallRightControl.Position.y);
movedAmount = 0;
}
if (extraSpace < smallNeededSpace) {
Debug.Log("Moving.");
smallLeftControl.Position = new Vector2(smallLeftControl.Position.x + (smallNeededSpace - extraSpace), smallLeftControl.Position.y);
smallRightControl.Position = new Vector2(smallRightControl.Position.x - (smallNeededSpace - extraSpace), smallRightControl.Position.y);
movedAmount = smallNeededSpace - extraSpace;
}
}
}
}
Something to note:
The variable screenRatio uses the constant dividend of 128.0, because the extra space in the 1024x768 UI resolution is 128 pixels on each side. A 1:1 ratio on 1024x768 = 768x768, with a remainder of 256 on the width. 256 / 2 = 128 (128 on the left and 128 on the right).
Also, any variable that is used that deals with Constants is properly defined, I've checked these numbers on more than one occasion and am certain that they are properly set and calculated before they are used in the above code.
Overall the issue is the equations to determine the sizes, starting with the int extraSpace line. Somewhere in there is the answer but honestly I'm not sure what it is. I've tried 10-20 different ways of calculating it, including using the whole screen's width and height to determine the aspect ratio and a bunch of other stuff....I'm really out of things to try right now.
The problem result is that when I have more than enough space for the larger UI, it does not appear, and when I'm at a resolution like 5:4, it does not properly get pushed in and part of it remains cut off. The equations are simply wrong, but I can't seem to figure out what is correct. If any more information is needed I will provide it to the best of my ability.
Any suggestions on how to solve this problem would be greatly appreciated.