Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Screen Resolutions, Popuplists In Ngui

Discussion in 'Scripting' started by Ab21392, Jul 18, 2014.

  1. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    I am new to C# I am currently using NGUI Free 2.7 but I don't think there will be much difference in the result

    I have the following code that I have been working on from using the NGUI, Unity scripting references but I am stuck.

    The code below works perfectly fine but what I am trying to do is make the resolutions that are put into the list omit certain resolutions for example below 800x600, but I am unable to find any code examples on how to omit certain resolutions be it a single or anything below a certain value.

    I am aware that I need to build and run it before I can see all the resolutions but before I get into making options buttons for a menu I am working on for all the graphics settings as a base learning experience this one has me confused.

    Any help would be appreciated.

    Code (CSharp):
    1. public class ResolutionDetection : MonoBehaviour
    2. {
    3.     public GameObject Resolutionlist;
    4.     UIPopupList PopupList;
    5.  
    6.     void Start()
    7.     {
    8.         PopupList = gameObject.GetComponent<UIPopupList>();
    9.         Resolution[] resolutions = Screen.resolutions;
    10.         foreach (Resolution res in resolutions)
    11.         {
    12.             PopupList.items.Add(res.width + "x" + res.height);
    13.         }
    14.     }
    15. }
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    You can specify aspect ratios in the build settings but if you really want to allow only some specific resolution you'll have to think of how you want to identify them.

    If you have a list without any mathematical consistency you could have a hard coded collection with thoose against which you would compare the available resolution for the current display device.

    If you have a mathematical consistency in the resolutions you want to make available then you just have to check the calculation for each available resolution for the current display device.

    For example if you only want to allow resolutions with a minimum 1024px width:
    Code (csharp):
    1.      foreach (Resolution res in resolutions)
    2.        {
    3.            if (res.width >= 1024)
    4.                PopupList.items.Add(res.width + "x" + res.height);
    5.        }
    6.  
     
  3. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    Thank you kindly that one line of code difference is what I required i was mostly trying to omit resolutions below 800x600 for sake of compatability with monitors as some do not go below 800x600 but the code below works just fine, cannot believe one simple line did what I required again thank you for the help.

    Code (CSharp):
    1. if (res.width + res.height >= 800 + 600)
     
  4. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    You're welcome ! :)
     
  5. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    I have now bumped into another problem, after many hours of searching I have worked out in Ngui 2.7 how to send my pop uplist to the OnSelectionChange handler, however the Screen.set Resolution is failing to work.

    The code below is what I have so far but searching forums and the various references for both NGUI 2.7 and Unity.

    I have tried to use within OnSelectionChange

    Code (CSharp):
    1.     void OnSelectionChange(string selectedItem)
    2.     {
    3.         Screen.SetResolution (selectedItem);
    4.     }
    5. }
    Which then throws up an error of No overload for method `SetResolution' takes `1' arguments, have searched but I am stuck and in need of some pointers or help. I have a feeling I need to specify something some where but after 8 hours searching for examples as I am learning is proving to be a real pain.

    Any further help would be appreciated.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ResolutionDetection : MonoBehaviour
    5. {
    6.     public GameObject Resolutionlist;
    7.     UIPopupList PopupList;
    8.  
    9.  
    10.     void Start()
    11.     {
    12.             PopupList = gameObject.GetComponent<UIPopupList> ();
    13.  
    14.             Resolution[] resolutions = Screen.resolutions;
    15.             foreach (Resolution res in resolutions)
    16.         {
    17.             if (res.width + res.height >= 800 + 600)
    18.             PopupList.items.Add (res.width + "x" + res.height);
    19.         }
    20.     }
    21. }
    I know the selections are reaching here this is here so I could see that the drop down menu was being picked up the other end.

    Code (CSharp):
    1.     void OnSelectionChange(string selectedItem)
    2.     {
    3.         Debug.LogWarning (selectedItem);
    4.     }
    5. }
     
  6. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    hi, method call take specific argument type as input. When you look at the doc is says "SetResolution(int width, int height, bool fullscreen, int preferredRefreshRate = 0);" I underlined the arguments and their required type. You need 2 integers and a boolean, the last argument having a default value meaning it can be omitted.

    One of the flaws of NGUI is the way it transports data, here for the selection widget you only have a string given to the event. You can either use that or the selectedIndex variable of the object to find what values you must feed into setResolution(). (maybe you could keep a list/array of the values in the widget this way you could find the values by calling something like this._availableResolutions[selectedIndex]).
     
  7. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    I found a possible solution but I need a little help debugging it, I will try to be as clear as possible.

    I am using this below to define if the boolean true or false for setting the fullscreen mode of the resolution as defined below by the case, which means if a user finds a resolution on their monitor that won;t switch I will have to manually add it in case of obscure resolution on older monitors.

    However using the FScreen boolean is causing on resolution change to swap in and out of fullscreen and ignore the check box if any one can provide assistance in debugging or where I am going wrong in trying to define FScreen as the boolean to check the status of Screen.fullScreen and then allow the resolution switch to keep the setting of the full screen.

    relevant code snippets below of what I am using on my check boxes.

    Code (CSharp):
    1.     public bool FScreen;
    2.  
    3.     void Start ()
    4.     {
    5.         if (Screen.fullScreen)
    6.         {
    7.             FScreen = true;
    8.         }
    9.  
    10.         else
    11.         {
    12.             FScreen = false;
    13.         }
    14.     }
    15.  
    Code (CSharp):
    1. void OnResolutionChange (string ResolutionSwitch)
    2.     {
    3.         switch (ResolutionSwitch)
    4.         {
    5.             case "800x600":    Screen.SetResolution(800, 600, FScreen);
    6.             break;
    7.             case "1024x768": Screen.SetResolution(1024, 768, FScreen);
    8.             break;
    9.             case "1280x720": Screen.SetResolution(1280, 720, FScreen);
    10.             break;
    11.             case "1280x768": Screen.SetResolution(1280, 768, FScreen);
    12.             break;
    13.             case "1280x800": Screen.SetResolution(1280, 800, FScreen);
    14.             break;
    15.             case "1280x960": Screen.SetResolution(1280, 960, FScreen);
    16.             break;
    17.             case "1280x1024": Screen.SetResolution(1280, 1024, FScreen);
    18.             break;
    19.             case "1360x768": Screen.SetResolution(1360, 768, FScreen);
    20.             break;
    21.             case "1366x768": Screen.SetResolution(1366, 768, FScreen);
    22.             break;
    23.             case "1440x900": Screen.SetResolution(1440, 900, FScreen);
    24.             break;
    25.             case "1600x900": Screen.SetResolution(1600, 900, FScreen);
    26.             break;
    27.             case "1920x1080": Screen.SetResolution(1920, 1080, FScreen);
    28.             break;
    29.         }
    30.     }
    Code (CSharp):
    1.     void OnActivateFullScreen (bool isFullScreen)
    2.     {
    3.         if (isFullScreen)
    4.         {
    5.             Screen.fullScreen =  true;
    6.         }
    7.  
    8.         else
    9.         {
    10.             Screen.fullScreen = false;
    11.         }
    12.     }
    13.  
     
  8. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    You change the full screen mode directly in the Screen object but leave your FScreen variable unchanged, what do you think happens the next time you change resolution using that variable ? ^^

    I let you meditate on that ;)
     
  9. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    I figured it out now thanks needed to add the FScreen = true to the switch for Full screen mode toggle, thanks for the help ;)

    Now onto adding the rest of the resoliutions now I have debugged this.
     
  10. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    You welcome, also another tip, the code below is simpler:

    Code (CSharp):
    1.     public bool FScreen;
    2.  
    3.     void Start ()
    4.     {
    5.         FScreen = Screen.fullScreen;
    6.     }
    7.  
    Good luck with your project :)
     
  11. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    If any one can provide some help into Lists and for loops and how to properly set these up since this is related to my previous requests for a little pointer in the right direction I added it to this.

    What I am mostly trying to do is make the list loop to create the index, then when I press the ListNext or ListBack make it choose and display on the label what is stored in the strings, the problems I have is I am not sure if the for loops are in the spot I have tried tying them to the buttons directly but I do not seem able to make them scroll one item at a time through the list only output the entire list and the last item in the list is what shows up on the label.

    If any one can provide some help into lists I have spent alot of time examining C# lists and resources on the web and tried foreach loops, but I do not seem to be able to put my finger onto the last I need to make the buttons work properly, I have added a few comments to the actual code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ResolutionDetection : MonoBehaviour
    6. {
    7.     public int index = 0;
    8.     public GameObject SelectOption;
    9.     public UILabel ResolutionLabel;
    10.     public List <string> ResolutionList;
    11.  
    12.     void Start()
    13.     {
    14.         UIEventListener.Get(SelectOption).onClick += ListNext;
    15.         UIEventListener.Get(SelectOption).onClick += ListBack;
    16.  
    17.         ResolutionList = new List<string> ();
    18.  
    19.         //Resolution[] resolutions = Screen.resolutions; //Disabled As Using Some Hard Coded Strings
    20.         //foreach (Resolution res in resolutions)
    21.         {
    22.             //if (res.width + res.height >= 800 + 600)
    23.             {
    24.                 //ResolutionList.Add(res.width + "x" + res.height);
    25.                 ResolutionList.Add ("Baked Beans");
    26.                 ResolutionList.Add ("Sausages");
    27.                 ResolutionList.Add ("Eggs");
    28.                 ResolutionList.Add ("Bacon");
    29.             }
    30.  
    31.             for (int index = 0; index < ResolutionList.Count; index++)
    32.             {
    33.                 // Not sure if this loop is in the right place
    34.             }
    35.  
    36.             for (int index = ResolutionList.Count -1; index >= 0; index--)
    37.             {
    38.                 // Not sure if this loop is in the right place either
    39.             }
    40.         }
    41.     }
    42.      
    43.     void ListNext (GameObject SelectOption) //Button For Going Forward Through Each Item In List
    44.     {
    45.         //ResolutionLabel.text = (ResolutionList[index]); // Commented Out As This Will Be For Final Use
    46.     }
    47.  
    48.     void ListBack(GameObject SelectOption) //Button For Going Back Through Each Item In List
    49.     {
    50.         //ResolutionLabel.text = (ResolutionList[index]); // Commented Out As This Will Be For Final Use
    51.     }
    52. }
     
  12. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hi Ab, I'm not sure I understand what you mean by creating the index, if you only meant to initialize it then try this it should work.

     
    Ab21392 likes this.
  13. Ab21392

    Ab21392

    Joined:
    Jul 18, 2014
    Posts:
    13
    Yes I meant to say I was trying to intialise the index int I had set, this works perfectly for what I was trying to do, have a string list and buttons scroll through them, I was just unsure if I was using the right loops or doing it correct I got most of the way just stumped by one bit.

    The script you posted does throw up an error though in unity but I already know the cause of it but incase other people do see this thread the resolution list is empty until a full build is made and a capitalization of Count is needed also, manually adding a string will allow you to test it if you cannot do a full build ;)

    Thumbs up for the help met44.
     
  14. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    You're welcome, glad I could help a little ^^

    About the error in the editor, you can use the preprocessor tags to have a mockup in the editor and the real thing in your builds:

    Code (csharp):
    1.  
    2. ResolutionList = new List<string>();
    3. #if UNITY_EDITOR
    4. ResolutionList.Add("1024x768");
    5. ResolutionList.Add("1280x1024");
    6. #else
    7. //real thing
    8. Resolution[] resolutions = Screen.resolutions;
    9. foreach (Resolution res in resolutions)
    10. {
    11. ...
    12. }
    13. #endif
    14.