• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Polantaris

Unity
UI auto-adjustment based on available space.

2 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By OPNeonGames
      SumiKen : Ink Blade Samurai is released! Download and leave a review to help support the game!
      Download here for free : https://play.google.com/store/apps/details?id=com.OPNeon.SumiKen&hl=en

      ★★★★★ - "Just get it it's the best runner!" - Icatsasha
      ★★★★★ - "Great game, wonderful art! Super addictive!" - EZk1ll 
      A casual samurai runner game with seven samurai and beautiful mountain paintings. Join Sumi and his samurai companions on this epic path to slash & dash through endless hordes of enemy lines. Beware of the enemy blades as a single blow from the sword blade will end it all. Will you be turned into faded ink? Or will you train and become a samurai legend?
       
    • By OPNeonGames
      Hey guys, posting my work in progress for Macho Cat here. 

      A very early prototype for Macho Cat. Everything is just placeholder for now
      What is Macho Cat?
      A silly little game where you scrub the macho cat with random objects found in trash and junkyard to please him
      Gameplay feature?
      -Cat scrubbin, lots of scrubbin
      -Unlock moar objects in junkyard 
      -Funny, silly and easy
      When will the game release?
      December 2017 (estimate)
      Interested to Beta test?
      opneongame@gmail.com
    • By OPNeonGames
      Hey guys, posting my work in progress for Macho Cat here. 

      A very early prototype for Macho Cat. Everything is just placeholder for now
      What is Macho Cat?
      A silly little game where you scrub the macho cat with random objects found in trash and junkyard to please him
      Gameplay feature?
      -Cat scrubbin, lots of scrubbin
      -Unlock moar objects in junkyard 
      -Funny, silly and easy
      When will the game release?
      December 2017 (estimate)
      Interested to Beta test?
      opneongame@gmail.com
    • By Tset_Tsyung
      Hey all,

      As the heading says I'm trying to get my head around Goal Objective Action Planning AI.  However I'm having some issues reverse engineering Brent Owens code line-by-line (mainly around the recursive graph building of the GOAPPlanner).  I'm assuming that reverse engineering this is the best way to get a comprehensive understanding... thoughts?

      Does anyone know if an indepth explanation on this article (found here: https://gamedevelopment.tutsplus.com/tutorials/goal-oriented-action-planning-for-a-smarter-ai--cms-20793), or another article on this subject?

      I'd gladly post my specific questions here (on this post, even), but not too sure how much I'm allowed to reference other sites...

      Any pointers, help or comments would be greatly appreciated.

      Sincerely,

      Mike
    • By E-gore
      Hi all,
      This is our (xDBAGames) first game
      Called Iron Dome Legacy.
      It is a Missile Command clone. In this game you control the Israeli "Iron Dome" anti missile defence system.
      Features: 
      The player has limited amount of missiles. The Iron dome system is upgradable. The money is determined by the outcome of a level. The game is free. There are only rewarded ads. We tried to create a game that has some context to the daily life, but we are sure not trying to be political here.
      I hope you could try this game, and we will appreciate any comments.
      xDBAGames is a company of two programmers. That have no experience in the video game industry, but have a lot of passion for games.

  • Popular Now