Unity Custom UI Toggle ○●

Unity Custom UI Toggle ○●

Hamza Herbou

3 года назад

32,159 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Talisman Skulls
Talisman Skulls - 21.09.2023 04:15

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?

Ответить
San
San - 12.07.2023 16:01

If Toggle is Is On clicked, I cant see my toggle

Ответить
GD_Helper
GD_Helper - 12.05.2023 11:01

Amazing..man u save my tonnes of hours to d that.. i literally just drag and dropped ur asset and it works so perfectly... :)

Ответить
Aliagha Huseynli
Aliagha Huseynli - 27.02.2023 19:46

thanks Hamza Herbou

Ответить
Hello World
Hello World - 23.01.2023 13:09

Thx!

Ответить
김도현
김도현 - 06.01.2023 03:48

NICE~!

Ответить
Алексей Буров
Алексей Буров - 22.11.2022 17:25

Clean code and good taste in style. Awesome!

Ответить
Komal Gupta
Komal Gupta - 25.07.2022 06:49

Amazing tutorial :)

Ответить
Kadarella ENC DJ
Kadarella ENC DJ - 06.06.2022 17:10

how does this toggle work during game pause?

Ответить
Амар Тыгыдынов
Амар Тыгыдынов - 17.05.2022 05:16

thank you soo much for the Lesson !!

Ответить
Silly Dog
Silly Dog - 18.03.2022 09:46

老哥真厉害,我看了看感觉非常棒!但是我不会C#,只会用playmaker,请问用playmaker能做吗?

Ответить
Hashan Madusanka
Hashan Madusanka - 10.03.2022 13:53

thank you so much

Ответить
ADSL PIU' LENTO D'ITALIA
ADSL PIU' LENTO D'ITALIA - 27.02.2022 04:15

But the big question Is how to save a toggle

Ответить
Thanh Tùng Vũ
Thanh Tùng Vũ - 24.01.2022 09:42

really nice tutorial and beautifully illustrated, thank you and subcribed :)

Ответить
Pixel Artist Spirhack
Pixel Artist Spirhack - 18.01.2022 23:20

Nice tutorial. Thank you.

Ответить
NeoLoveCookie
NeoLoveCookie - 01.01.2022 11:37

So beautiful tutorial, but so loud music. :)

Ответить
Armand Sahetchian
Armand Sahetchian - 08.12.2021 01:35

Thanks

Ответить
M. Nenad
M. Nenad - 14.11.2021 17:14

Good tutorial , thx!

Ответить
Joseph Chica
Joseph Chica - 14.11.2021 03:16

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?

Ответить
Saiful Islam
Saiful Islam - 27.10.2021 08:06

what's the font u are using on your IDE?

Ответить
T Jinyu
T Jinyu - 08.10.2021 16:09

brilliant ~!!

Ответить
Asad ali
Asad ali - 23.09.2021 21:31

Great video bro..Keep it up and also thanks

Ответить
Leonardo Olivieri
Leonardo Olivieri - 03.09.2021 23:47

Thanks from Brazil!!!

Ответить
Rivera Ramírez Fernando Nohel
Rivera Ramírez Fernando Nohel - 05.08.2021 00:59

Thanks!!! The tutorial was very useful!

Ответить
Binarium
Binarium - 14.05.2021 11:03

instead of that can we change the transition to animation right ?

Ответить
Markiyan Lelet
Markiyan Lelet - 07.05.2021 19:05

Just greate!!!

Ответить
Documentaires Banales
Documentaires Banales - 25.04.2021 11:38

thx

Ответить
Nikita Nechaev
Nikita Nechaev - 24.03.2021 10:18

useful video, tnx

Ответить
László Domján
László Domján - 19.03.2021 15:18

100th like and thank you!

Ответить
Madison Farr
Madison Farr - 01.03.2021 08:06

I get "No toggle namespace"?

Ответить
yahya shafqat
yahya shafqat - 24.11.2020 20:09

It's great but man would love to hear some voice overs you know

Ответить
Wai Han Lim
Wai Han Lim - 23.11.2020 17:00

Brilliant tutorial! Very knowledgeable and useful!

Ответить
Elmehdi DAIR
Elmehdi DAIR - 23.11.2020 11:39

very nice <3

Ответить
youssef abba
youssef abba - 23.11.2020 09:56

❤️❤️❤️

Ответить
Android User
Android User - 22.11.2020 20:30

Good work

Ответить
文揚
文揚 - 22.11.2020 17:32

Amazing detailed tutorial also very useful.

Ответить
Musa Kaya
Musa Kaya - 22.11.2020 00:57

Awesome

Ответить
Chrifi Simo
Chrifi Simo - 21.11.2020 21:32

Very nice you are very talented i like when I see arabic ( moroccan ) people make this great things all the best bro

Ответить
HONDA YADAV
HONDA YADAV - 21.11.2020 21:29

Nice

Ответить