Комментарии:
Maybe you can help me sort this out. I created a toggle group fro quality and resolution settings using PlayerPrefs. So the selection at runtime saves but the toggles when restarting the editor do not reflected the last saved settings and default back. The setup I have so far is as follows but I need to toggles to also match the last saved selection and nothing I apply seems to be working.
{
[SerializeField] private Toggle low, mid, high, veryHigh, ultra, res1, res2, res3, res4, res5;
private const string QUALITY_KEY = "QualityIndex";
private const string RESOLUTION_KEY = "ResolutionIndex";
private const int DEFAULT_QUALITY_INDEX = 2;
private const int DEFAULT_RESOLUTION_INDEX = 1;
public void Start()
{
int qualityIndex = PlayerPrefs.HasKey(QUALITY_KEY) ? PlayerPrefs.GetInt(QUALITY_KEY) : DEFAULT_QUALITY_INDEX;
int resolutionIndex = PlayerPrefs.HasKey(RESOLUTION_KEY) ? PlayerPrefs.GetInt(RESOLUTION_KEY) : DEFAULT_RESOLUTION_INDEX;
// Set the resolution and quality level based on the indexes
SetRes(resolutionIndex);
SetQual(qualityIndex);
}
public void SetQual(int qualityIndex)
{
// Check if the index is valid
if (qualityIndex >= 1 && qualityIndex <= 5)
{
// Set the quality level
QualitySettings.SetQualityLevel(qualityIndex, true);
// Save the index
PlayerPrefs.SetInt(QUALITY_KEY, qualityIndex);
}
else
{
Debug.LogError("Invalid quality index");
}
}
public void SetRes(int resolutionIndex)
{
// Check if the index is valid
if (resolutionIndex >= 1 && resolutionIndex <= 5)
{
// Set the resolution based on the index
switch (resolutionIndex)
{
case 1:
Screen.SetResolution(1920, 1080, FullScreenMode.Windowed);
break;
case 2:
Screen.SetResolution(1536, 864, FullScreenMode.Windowed);
break;
case 3:
Screen.SetResolution(1366, 768, FullScreenMode.Windowed);
break;
case 4:
Screen.SetResolution(1280, 720, FullScreenMode.Windowed);
break;
case 5:
Screen.SetResolution(1024, 768, FullScreenMode.Windowed);
break;
default:
Debug.LogError("Invalid resolution index");
break;
}
// Save the index
PlayerPrefs.SetInt(RESOLUTION_KEY, resolutionIndex);
}
else
{
Debug.LogError("Invalid resolution index");
}
}
public void SetToggles(int qualityIndex, int resolutionIndex)
{
// Check if the indexes are valid
if (qualityIndex >= 1 && qualityIndex <= 5 && resolutionIndex >= 1 && resolutionIndex <= 5)
{
// Set the toggles based on the indexes
switch (qualityIndex)
{
case 1:
low.isOn = true;
break;
case 2:
mid.isOn = true;
break;
case 3:
high.isOn = true;
break;
case 4:
veryHigh.isOn = true;
break;
case 5:
ultra.isOn = true;
break;
default:
Debug.LogError("Invalid quality index");
break;
}
switch (resolutionIndex)
{
case 1:
res1.isOn = true;
break;
case 2:
res2.isOn = true;
break;
case 3:
res3.isOn = true;
break;
case 4:
res4.isOn = true;
break;
case 5:
res5.isOn = true;
break;
default:
Debug.LogError("Invalid resolution index");
break;
}
}
else
{
Debug.LogError("Invalid indexes");
}
}
public void Low()
{
SetQual(1);
}
public void Mid()
{
SetQual(2);
}
public void High()
{
SetQual(3);
}
public void VeryHigh()
{
SetQual(4);
}
public void Ultra()
{
SetQual(5);
}
public void Resolution1()
{
SetRes(1);
}
public void Resolution2()
{
SetRes(2);
}
public void Resolution3()
{
SetRes(3);
}
public void Resolution4()
{
SetRes(4);
}
public void Resolution5()
{
SetRes(5);
}
void OnApplicationQuit()
{
// Save the indexes when the application is closed
PlayerPrefs.Save();
}
}
What am I missing?
If Toggle is Is On clicked, I cant see my toggle
ОтветитьAmazing..man u save my tonnes of hours to d that.. i literally just drag and dropped ur asset and it works so perfectly... :)
Ответитьthanks Hamza Herbou
ОтветитьThx!
ОтветитьClean code and good taste in style. Awesome!
ОтветитьAmazing tutorial :)
Ответитьhow does this toggle work during game pause?
Ответитьthank you soo much for the Lesson !!
Ответить老哥真厉害,我看了看感觉非常棒!但是我不会C#,只会用playmaker,请问用playmaker能做吗?
Ответитьthank you so much
ОтветитьBut the big question Is how to save a toggle
Ответитьreally nice tutorial and beautifully illustrated, thank you and subcribed :)
ОтветитьNice tutorial. Thank you.
ОтветитьSo beautiful tutorial, but so loud music. :)
ОтветитьThanks
ОтветитьGood tutorial , thx!
ОтветитьIm having an issue only after adding the color changes. The whole toggle button disappears when I click it. The object is still there because I can click again and it appears, but its not visible when active. Anybody else having this issue?
Ответитьwhat's the font u are using on your IDE?
Ответитьbrilliant ~!!
ОтветитьGreat video bro..Keep it up and also thanks
ОтветитьThanks from Brazil!!!
ОтветитьThanks!!! The tutorial was very useful!
Ответитьinstead of that can we change the transition to animation right ?
ОтветитьJust greate!!!
Ответитьthx
Ответитьuseful video, tnx
Ответить100th like and thank you!
ОтветитьI get "No toggle namespace"?
ОтветитьIt's great but man would love to hear some voice overs you know
ОтветитьBrilliant tutorial! Very knowledgeable and useful!
Ответитьvery nice <3
Ответить❤️❤️❤️
ОтветитьGood work
ОтветитьAmazing detailed tutorial also very useful.
ОтветитьAwesome
ОтветитьVery nice you are very talented i like when I see arabic ( moroccan ) people make this great things all the best bro
ОтветитьNice
Ответить