- Home /
change color with playerprefs using buttons, between scenes.,How to use playerprefs to save colors from a button?
I would like to change colors between scenes, using Buttons. I have Red, Green, Blue, Purple. Blue is my default color. i would like that if i change the color to green in the menu scene, then its also changed to the same color, in the expert scene or any of my other scenes.
I imagine you need to use Playerprefs, however i cant really figure it out. Thank you.,i basically got 4 colors. Red, Green, Blue, Purple. I got multiple scenes and i want the colors to be changed and saved, between each scene. so if i make it green in the menu scene, then in the expert scene, its still red and not its default color.
Colors get changed with the press of a button.
Could i please get some help, with how thats gonna work?
Answer by Patrickmol · Jun 10, 2021 at 11:14 AM
//YOU HAVE TO PUT THE USING IMPORTS
public class ColorButtonHandler : MonoBehaviour{
public static Color32 red;
public static Color32 green;
public static Color32 blue;
public static Color32 purple;
public Button button;
private void Awake(){
if (SceneManager.GetActiveScene().name == "Menu")//or whatever it is
button.GetComponent<Image>().color = green;
else if (SceneManager.GetActiveScene().name == "x")//or whatever it is
button.GetComponent<Image>().color = red;
//AND SO ON
}
}
THERE COULD BE ERRORS CAUSE I DID NOT COMPILE THIS, JUST WROTE IT
Answer by DenisIsDenis · Jun 10, 2021 at 11:16 AM
You can use an array of colors and store the color index in PlayerPrefs:
using UnityEngine;
public class ColorSaver: MonoBehaviour
{
public Color[] colors; // set it in inspector!
static public Color[] colorsStatic; // static array with colors for static function
private void Start()
{
colorsStatic = colors;
/* Set PlayerPrefs "Color" if it doesn't exist */
if (PlayerPrefs.GetInt("Color", -1) != -1)
{
PlayerPrefs.SetInt("Color", 0);
}
}
//----------- Use it for setting color in each scene! ------------//
static void SetColorInPlayerPrefsByIndex(int newColorIndex)
{
if (newColorIndex >= 0 && newColorIndex < colorsStatic.Length)
PlayerPrefs.SetInt("Color", newColorIndex);
else
print("[ColorSaverError]: invalid index of colors' array!");
}
//----------------------------------------------------------------//
//---- Use it for getting color in each scene! ----//
static public Color GetColorInPlayerPrefs()
{
return colorsStatic[PlayerPrefs.GetInt("Color")];
}
//-------------------------------------------------//
}
Answer by Harry_Drew · Jun 10, 2021 at 11:24 AM
If you want to use player prefs to save colour values you could do it like this:
Colours are made up of 4 values R,G,B,A these are all float values between 0 and 1. You could get these values and put them into a string separating each value with a comma. You then save the string to playerprefs using Playerprefs.SetString("SavedColour", ColourCombinedString); You could then convert this string back into 4 float values using the separating commas. These 4 float can then be converted into a colour.
Here's a better way for your situation:
However these a much easier way of doing it if you know the colours you want (which you do). Have an array of colours in a script for the colour that you want. Set a playerpref value for the index of the colour you want to use from the array. Here's an example below.
public class ColourPicker : MonoBehaviour
{
//-Inspector editable variables
[SerializeField] private Color[] c_DifferentColours;
//ARRAY COLOUR INDEX SHOULD GO:
//BLUE
//RED
//GREEN
//PURPLE
//-Private variables
private int m_ColourIndex;
//-Public variables
public Color m_ChoosenColour;
void Start()
{
//Get the colour stored in the playerprefs
GetPlayerPrefsValue();
}
private void GetPlayerPrefsValue()
{
m_ColourIndex = PlayerPrefs.GetInt("StoredColour", 0);
m_ChoosenColour = c_DifferentColours[m_ColourIndex];
}
//RED
public void SetRed()
{
PlayerPrefs.SetInt("StoredColour", 1);
GetPlayerPrefsValue();
}
//GREEN
public void SetGreen()
{
PlayerPrefs.SetInt("StoredColour", 2);
GetPlayerPrefsValue();
}
//BLUE
public void SetBlue()
{
PlayerPrefs.SetInt("StoredColour", 0);
GetPlayerPrefsValue();
}
//PURPLE
public void SetPurple()
{
PlayerPrefs.SetInt("StoredColour", 3);
GetPlayerPrefsValue();
}
}
Use the public methods like SetRed() to change the stored colour. The colour will be loaded each time a method is called and when the scene is first loaded. Hope this helps :)