- Home /
How to save color
Hello, how would I save color on an object? Tried using Playerprefs but didn't get it to work correctly. I have done some research, I found that you need to use playerprefsX but I have no idea how it works. Help would be appreciated.
Answer by doztep · Dec 30, 2019 at 10:46 AM
Instead of writing your own methods, you could use unity's ColorUtility class:
// Store single color
Color color;
PlayerPrefs.SetString("ColorKey", ColorUtility.ToHtmlStringRGB(color)); // without alpha
PlayerPrefs.SetString("ColorKey", ColorUtility.ToHtmlStringRGBA(color)); // with alpha
// Load single color
Color color;
ColorUtility.TryParseHtmlString("#" + PlayerPrefs.GetString("ColorKey"), out color);
// store color array
Color[] colors;
//// fill array with your colors ////
int numberOfColors = colors.Length;
for (int i = 0; i < numberOfColors; i++){
Color color = colors[i];
PlayerPrefs.SetString("ColorKey" + i, ColorUtility.ToHtmlStringRGBA(color)); // with alpha
}
// load color array
Color[] colors = new Color[numberOfColors];
for (int i = 0; i < numberOfColors; i++){
ColorUtility.TryParseHtmlString("#" + PlayerPrefs.GetString("ColorKey" + i), out colors[i]);
}
Edit: Adding "#" in front of colorHtmlString is required (see: https://docs.unity3d.com/ScriptReference/ColorUtility.TryParseHtmlString.html)
Answer by Grish_tad · Dec 29, 2017 at 01:18 PM
Hi @unity_G2yrNlWYdpJKuw . Try this.
void SaveColor(Color color)
{
PlayerPrefs.SetString("SavedColor", ColorToHex(color));
}
void GetSavedColor()
{
Color GetedColor= HexToColor(PlayerPrefs.GetString("SavedColor"));
}
// Note that Color32 and Color implictly convert to each other. You may pass a Color object to this method without first casting it.
string ColorToHex(Color32 color)
{
string hex = color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2");
return hex;
}
Color HexToColor(string hex)
{
byte r = byte.Parse(hex.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4,2), System.Globalization.NumberStyles.HexNumber);
return new Color32(r,g,b, 255);
}
Why is it so complicated? There isn't any easier way?
Answer by pareshkh · Sep 06, 2018 at 07:50 AM
hey @unity_G2yrNlWYdpJKuw . use this code. for save and get save color just you need to call. SaveColor() and GetSaveColor(). u can also set and get Collor Array at a time.
public class MyPlayerPrefs : MonoBehaviour {
public static Color GetSaveColor (string key)
{
string col = PlayerPrefs.GetString (key);
Debug.Log (col);
if (col == "")
{
return Color.black;
}
string[] strings = col.Split (',');
Color output = new Color ();
for (int i = 0; i < 4; i++)
{
output [i] = System.Single.Parse (strings [i]);
}
return output;
}
public static bool CheckColor (string key)
{
if (PlayerPrefs.HasKey (key))
return true;
else
return false;
}
public static void SaveColor (Color color, string key)
{
string col = color.ToString ();
col = col.Replace ("RGBA(", "");
col = col.Replace (")", "");
PlayerPrefs.SetString (key, col);
}
public static void SaveColorArray (Color[] col, string key)
{
int i = 0;
PlayerPrefs.SetInt (key, col.Length);
foreach (Color c in col)
{
SaveColor (c, key + i++);
}
}
public static Color[] GetSaveColorArray (string key)
{
if (!PlayerPrefs.HasKey (key))
return null;
int len = PlayerPrefs.GetInt (key);
Color[] tem = new Color[len];
for (int i = 0; i < len; i++)
{
tem [i] = GetSaveColor (key + i);
}
return tem;
}
}
Your answer
Follow this Question
Related Questions
Android Build not deleting PlayerPrefs 1 Answer
Save score in Unity 1 Answer
options menu UI doesnt save when switching scenes 1 Answer
How can i save a variable for only one scene? 1 Answer
Playerprefs to save unlocked button 2 Answers