Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by federicolentini · Mar 02, 2020 at 12:01 PM · resolutionsettingsdropdownoptions

how to save the resolutions for the options menu

Hi, thanks a lot in advice i'm doing the options menu for the game(the resolutions) and i now have all the code that i need but i don't know how to save it with the player prefs

basically I would like to save the resolution of the dropdown and the fullscreen to make that when I select a resolution and then I close the game, when I reopen the game everything is saved

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ResolutionMenu : MonoBehaviour {

 public Dropdown resolutionDropdown;
 Resolution[] resolutions;
 int currentResolutionIndex = 0;

 void Start()
 {


     resolutions = Screen.resolutions;
     resolutionDropdown.ClearOptions();

     List<string> options = new List<string>();

     for (int i = 0; i < resolutions.Length; i++)
     {

         string option = resolutions[i].width + " x " + resolutions[i].height;
         options.Add(option);

         if(resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
         {
             currentResolutionIndex = i;
         }

     }

     resolutionDropdown.AddOptions(options);
     resolutionDropdown.value = currentResolutionIndex;
     resolutionDropdown.RefreshShownValue();

 }



 public void SetFullscreen(bool isFullscreen)
 {
     Screen.fullScreen = isFullscreen;
 }

 public void SetResolution(int resolutionIndex)
 {

     Resolution resolution = resolutions[resolutionIndex];
     Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);

 }

} this is the code(brackeys) and basically i want to save the fullscreen toggle and the current resolution of the dropdown thank a lot in advice for your time!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Mar 02, 2020 at 12:01 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ResolutionMenu : MonoBehaviour
 {
     private const string resolutionWidthPlayerPrefKey = "ResolutionWidth";
     private const string resolutionHeightPlayerPrefKey = "ResolutionHeight";
     private const string resolutionRefreshRatePlayerPrefKey = "RefreshRate";
     private const string fullScreenPlayerPrefKey = "FullScreen";
     public Toggle fullScreenToggle;
     public Dropdown resolutionDropdown;
     Resolution[] resolutions;
     Resolution selectedResolution;
     void Start()
     {
         resolutions = Screen.resolutions;
         LoadSettings();
         CreateResolutionDropdown();
 
         fullScreenToggle.onValueChanged.AddListener(SetFullscreen);
         resolutionDropdown.onValueChanged.AddListener(SetResolution);
     }
 
     private void LoadSettings()
     {
         selectedResolution = new Resolution();
         selectedResolution.width = PlayerPrefs.GetInt(resolutionWidthPlayerPrefKey, Screen.currentResolution.width);
         selectedResolution.height = PlayerPrefs.GetInt(resolutionHeightPlayerPrefKey, Screen.currentResolution.height);
         selectedResolution.refreshRate = PlayerPrefs.GetInt(resolutionRefreshRatePlayerPrefKey, Screen.currentResolution.refreshRate);
         
         fullScreenToggle.isOn = PlayerPrefs.GetInt(fullScreenPlayerPrefKey, Screen.fullScreen ? 1 : 0) > 0;
 
         Screen.SetResolution(
             selectedResolution.width,
             selectedResolution.height,
             fullScreenToggle.isOn
         );
     }
 
     private void CreateResolutionDropdown()
     {
         resolutionDropdown.ClearOptions();
         List<string> options = new List<string>();
         int currentResolutionIndex = 0;
         for (int i = 0; i < resolutions.Length; i++)
         {
             string option = resolutions[i].width + " x " + resolutions[i].height;
             options.Add(option);
             if (Mathf.Approximately(resolutions[i].width, selectedResolution.width) && Mathf.Approximately(resolutions[i].height, selectedResolution.height))
             {
                 currentResolutionIndex = i;
             }
         }
         resolutionDropdown.AddOptions(options);
         resolutionDropdown.value = currentResolutionIndex;
         resolutionDropdown.RefreshShownValue();
     }
 
     public void SetFullscreen(bool isFullscreen)
     {
         Screen.fullScreen = isFullscreen;
         PlayerPrefs.SetInt(fullScreenPlayerPrefKey, isFullscreen ? 1 : 0);
     }
     public void SetResolution(int resolutionIndex)
     {
         selectedResolution = resolutions[resolutionIndex];
         Screen.SetResolution(selectedResolution.width, selectedResolution.height, Screen.fullScreen);
         PlayerPrefs.SetInt(resolutionWidthPlayerPrefKey, selectedResolution.width);
         PlayerPrefs.SetInt(resolutionHeightPlayerPrefKey, selectedResolution.height);
         PlayerPrefs.SetInt(resolutionRefreshRatePlayerPrefKey, selectedResolution.refreshRate);
     }
 }


You will also have to remove the callbacks you have specified through the inspector of the dropdown and the toggle. All is set up by code.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Mar 02, 2020 at 02:43 PM 0
Share

@federicolentini Answer updated and tested

avatar image cindycharvet · Jun 16, 2021 at 03:41 PM 0
Share

Hi! Thanks for the help with this code, I wanted to asked you how can I define a serie of specific resolutions, I mean I don't want all the screen resolutions to be avaliable... I just want Full HD (1920x1080), 2K and 4K. And also to be that the text of the option dropdown, not the pixels just the name like 2K and 4K...

Thanks for the help! @Hellium

avatar image Hellium cindycharvet · Jun 16, 2021 at 07:40 PM 0
Share

Code not tested, written on a simple notepad, soo I don't even know if it compiles

 public struct Resolution
 {
     public readonly string name;
     public readonly int width;
     public readonly int height;
     public readonly int refreshRate;
 
     public Resolution(string n, int w, int h, int rr)
     {
         name = n;
         width = w;
         height = h;
         refreshRate = rr;
     }
 }
 
 public class ResolutionMenu : MonoBehaviour
 {
     private const string resolutionNamePlayerPrefKey = "ResolutionWidth";
     private const string fullScreenPlayerPrefKey = "FullScreen";
     public Toggle fullScreenToggle;
     public Dropdown resolutionDropdown;
     Resolution[] resolutions;
     Resolution selectedResolution;
     void Start()
     {
         resolutions = new Resolution[]
         {
             new Resolution("Custom", Screen.currentResolution.width, Screen.currentResolution.height, Screen.currentResolution.refreshRate),
             new Resolution("Full HD", 1920, 1080, 60),
             new Resolution("4K", 3840, 2160, 60),
             // ....
         };
         LoadSettings();
         CreateResolutionDropdown();
 
         fullScreenToggle.onValueChanged.AddListener(SetFullscreen);
         resolutionDropdown.onValueChanged.AddListener(SetResolution);
     }
 
     private void LoadSettings()
     {
         int index = Mathf.Max(
             System.Array.FindIndex(resolutions, resolution => resolution.name == PlayerPrefs.GetString(resolutionNamePlayerPrefKey)),
             0 // 0 = Index of custom resolution
         );
         selectedResolution = resolutions[index];
         
         fullScreenToggle.isOn = PlayerPrefs.GetInt(fullScreenPlayerPrefKey, Screen.fullScreen ? 1 : 0) > 0;
 
         Screen.SetResolution(
             selectedResolution.width,
             selectedResolution.height,
             fullScreenToggle.isOn,
             selectedResolution.refreshRate
         );
     }
 
     private void CreateResolutionDropdown()
     {
         resolutionDropdown.ClearOptions();
         List<string> options = new List<string>();
         int currentResolutionIndex = 0;
         for (int i = 0; i < resolutions.Length; i++)
         {
             options.Add(resolutions[i].name);
             if (resolutions[i].width == selectedResolution.width && resolutions[i].height == selectedResolution.height)
             {
                 currentResolutionIndex = i;
             }
         }
         resolutionDropdown.AddOptions(options);
         resolutionDropdown.value = currentResolutionIndex;
         resolutionDropdown.RefreshShownValue();
     }
 
      // SetFullscreen(bool isFullscreen) ...
      // SetResolution(int resolutionIndex) ...
 }
avatar image cindycharvet Hellium · Jun 17, 2021 at 04:47 PM 0
Share

Thanks for the help! the code is working well but still if I selected the 4K on the main menu and then go to the game and open de setting screen on that scene I get that the selected resolution is other like the Custom :( and on the dropdown the custom is showing as an option and won't like that to happen I tried to erase that line of code but the I won't work.

Thanks again for your advice and time, I really appreciate your help! @Hellium

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save the resolution of screen between scenes 2 Answers

In Game Graphics/Resolution Options 2 Answers

Making a launcher for my game with Visual Basic 1 Answer

Unity resolution dropdown duplicating 0 Answers

What are the most common screen resolutions to have in my options menu 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges