• Create Account

# UI auto-adjustment based on available space.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Polantaris  Members   -  Reputation: 175

Like
0Likes
Like

Posted 17 February 2014 - 01:37 AM

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.

~Polantaris

### #2stevenmarky  Members   -  Reputation: 369

Like
0Likes
Like

Posted 19 February 2014 - 10:45 AM

You're asking difficult questions...you might get more answers if you are able to break it up into separate problems and focus on one at a time.

I haven't fully analyzed what you are saying above, and I haven't fully analyzed your code but I have the feeling that you're making an incorrect assumption somewhere. I don't have a specific answer but I have some guesses and some more general advice.

the UI is always 1024x768, and I cannot adjust this resolution at run time

This sounds strange to me...if you are using Daikon forge and if it is similar to NGUI then when you use a 'fixed size' it's actually just means that it's using a constant 'virtual' pixel height, but the width of the screen is free to change. If so and your problem boils down to converting screen pixels to those 'virtual' pixels all you need to is something like:

    float pixelPositionToVirtualPosition(float f)
{
return f * (virtualScreenHeight/Screen.Height);
}

This will convert a screen pixel position into a NGUI position, and you can also write a function to do the reverse.

When you say something like this:

The problem result is that when I have more than enough space for the larger UI, it does not appear

or this:

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.

It makes me think that you're developing using trial and error. If you've thought logically about how the object should be positioned (could help to test the equations on paper first), and it doesn't end up where you think it should then you need to find out why before trying another solution. Use Debug.Log or print statements, or the debugger. Use divide and conquer to find the point at which your code is breaking down, then fix it.

Edited by stevenmarky, 19 February 2014 - 10:49 AM.

### #3Polantaris  Members   -  Reputation: 175

Like
1Likes
Like

Posted 21 February 2014 - 01:27 AM

You're asking difficult questions...you might get more answers if you are able to break it up into separate problems and focus on one at a time.

I haven't fully analyzed what you are saying above, and I haven't fully analyzed your code but I have the feeling that you're making an incorrect assumption somewhere. I don't have a specific answer but I have some guesses and some more general advice.

the UI is always 1024x768, and I cannot adjust this resolution at run time

This sounds strange to me...if you are using Daikon forge and if it is similar to NGUI then when you use a 'fixed size' it's actually just means that it's using a constant 'virtual' pixel height, but the width of the screen is free to change. If so and your problem boils down to converting screen pixels to those 'virtual' pixels all you need to is something like:

    float pixelPositionToVirtualPosition(float f)
{
return f * (virtualScreenHeight/Screen.Height);
}

This will convert a screen pixel position into a NGUI position, and you can also write a function to do the reverse.

When you say something like this:

The problem result is that when I have more than enough space for the larger UI, it does not appear

or this:

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.

It makes me think that you're developing using trial and error. If you've thought logically about how the object should be positioned (could help to test the equations on paper first), and it doesn't end up where you think it should then you need to find out why before trying another solution. Use Debug.Log or print statements, or the debugger. Use divide and conquer to find the point at which your code is breaking down, then fix it.

The way DFGUI seems to work is that the UI itself doesn't change at run time.  No matter when I check the UI during run time it is always exactly the same size, regardless of my screen.  I cannot find anything on any of the material for DFGUI that says one way or the other, and any attempt at getting support has failed.  I have to go with my observations.

There does not seem to be any special virtual Screen variables like the one you have mentioned, either.

I did not start off doing this developing through trial and error, however when every thought out plan to solve the issue has failed there's really nothing left when there is absolutely no details about the Library at all.  I've posted on their forums, I've sent emails, I get no response at all.  Pretty much at the point where I'm going to switch to another GUI tool because of the lack of support.  I've spent more than a few hours thinking out how to do this from a logical perspective and absolutely nothing I attempt helps, what exactly do you expect me to do?  I've tried studying the source code but that doesn't really detail too much either, and combined with the lack of support there is nothing else I can do.

I left out the majority of Debug statements that give me values of each variable because I wanted to clean up the code a little for a posting - Those lines don't do much on a forum where people cannot see what they output in any given scenario, and I can easily re-add them when testing again(In fact, they were never removed from the code, just from the post).  I had been working on it based on the values I was getting but was unable to find an appropriate solution.

Overall I fixed the issues, but I redid how I was doing everything. I started off by doing a base Screen.height / Screen.width and checking out the resulting double ratio.  If it is at least 1.6(16:10 Aspect Ratio, minimum for larger UI), I adjust to the larger UI, and anything smaller gets the small UI.  I solved the "pushing the UI closer when too small" issue by using the following code:

            if (screenRatio < 133 && screenRatio >= 100) {
int extraRatio = screenRatio - 100;
double extraPerc = 1.0 - ((double)extraRatio / 32.0);
int extraPixels = (int)(extraPerc * 128.0);
smallLeftControl.Position = new Vector2(smallLeftControl.Position.x + (extraPixels), smallLeftControl.Position.y);
smallRightControl.Position = new Vector2(smallRightControl.Position.x - (extraPixels), smallRightControl.Position.y);
movedAmount = extraPixels;
}


I multiply the resulting height/width ratio by 100 earlier, and make it an int for the sake of simplicity, as a very small decimal value is not really useful and pretty irrelevant for what it is being used for.

Then, I subtract the screenRatio by 100, so that I get only the ratio remaining after a 1:1 aspect ratio.  I then determine the percentage of the UI that is cut off from the minimum of a 4:3 ratio.  For example, in a 5:4 aspect ratio, the resulting screenRatio is 125, so extraRatio is 25.  25 / 32 (33 = perfect fit) results in  0.78125.  That means I have 78.125% of the extra space I need, and I need to move the UI over by 21.875%.  21.875% of 128 is exactly 28, so I need to move the UI over by 28 pixels.

Considering this works, this further increases my belief that the UI does not scale at all and is only visually stretched to match the screen.

Edited by Polantaris, 21 February 2014 - 01:29 AM.

~Polantaris

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS