- Home /
Custom settings not responding in game as should
Hey Guys,
I am trying to get these ingame Setting working,
The issue comes Revolving around the Fullscreen, The rest are working fine, Fullscreen doesnt save, everytime i start game up it goes back to Screen.fullScreen = false, Even when ini is set true;
using UnityEngine;
using System;
using System.Collections;
using System.Text.RegularExpressions;
public class Settings : MonoBehaviour {
public static bool showMenu = false;
private string settings;
public static bool fullscreen;
public string screenres = "1";
public string curRes = "1";
public string defaultx = "1024";
public string defaulty = "768";
int xx;
int yy;
public string[] ScreenRes;
public Rect Box;
public string selectedItem = "Select Sreen Resolution";
bool editing = false;
// Use this for initialization
#if UNITY_STANDALONE_WIN
void Start () {
settings = System.IO.File.ReadAllText(@"settings.ini");
string[] settingsLine = Regex.Split(settings, "\r\n");
foreach (string line in settingsLine)
{
string[] thisLine = Regex.Split(line, "=");
if(thisLine[0] == "fullscreen")
{
if (thisLine[1] == "True" || thisLine[1] == "true")
{
Screen.fullScreen = true;
}
else
{
Screen.fullScreen = false;
}
}else if(thisLine[0] == "ScreenRes")
{
switch (thisLine[1])
{
case "1":
Screen.SetResolution(1024,768,fullscreen);
break;
case "2":
Screen.SetResolution(1920, 1080, fullscreen);
break;
}
screenres = thisLine[1];
}
}
}
// Update is called once per frame
void Update () {
if (Screen.fullScreen != Settings.fullscreen)
{
Screen.fullScreen = Settings.fullscreen;
}
}
void OnGUI()
{
if (showMenu == true)
{
GUI.Box(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 300), "");
fullscreen = GUI.Toggle(new Rect(Screen.width / 2 - 150, Screen.height / 2 -150, 200, 30),fullscreen, "Fullscreen");
/*if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 150, 150, 30), "Toggle FullScreen"))
{
if (fullscreen == false)
{
fullscreen = true;
}else{
fullscreen = false;
}
}*/
GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 190, 150), "");
if (GUI.Button(new Rect(Screen.width/2 - 95,Screen.height/2-95,180,30),selectedItem))
{
editing = true;
}
if(editing)
{
Box.y = Screen.height / 2 - 85;
Box.width = 180;
for (int x = 0; x < ScreenRes.Length; x++)
{
if (GUI.Button(new Rect(Screen.width/2 -95 ,((Box.height * x) + Box.y + Box.height), Box.width, Box.height),ScreenRes[x]))
{
selectedItem = ScreenRes[x];
editing = false;
}
}
}
if (selectedItem == "1990x1080")
{
xx = 1920 ;
yy = 1080 ;
}else if(selectedItem == "1024x768")
{
xx = 1024;
yy = 1080;
}
if (GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 + 70, 120, 30), "Apply Screen"))
{
Screen.SetResolution(xx, yy, fullscreen);
}
if(GUI.Button(new Rect(Screen.width/2-60,Screen.height/2+110,120,30),"Close"))
{
showMenu = false;
saveSettings();
}
}
}
void saveSettings()
{
string output = "fullscreen=" + fullscreen;
System.IO.StreamWriter file = new System.IO.StreamWriter(@"settings.ini");
file.Write(output);
file.Close();
}
#endif
}
Answer by JohnnySunshine · Nov 13, 2013 at 12:13 PM
You never set the "fullscreen" variable, so it will always stay false. Set the variable when reading the settings file so it will be written correctly.
i Have, Here is the variable - public static bool fullscreen;
I mean you never assigned a value to it :) Write
Settings.fullscreen = Screen.fullScreen;
after line 40
I got this
void Update () {
if (Screen.fullScreen != Settings.fullscreen)
{
Screen.fullScreen = Settings.fullscreen;
}
}
I know, but you're writing SETTINGS.fullscreen into the file, and you never assign a value to it, so you're always writing 'false' into the file. In the update you set SCREEN.fullscreen, I meant the other way around! :)