- Home /
Playerprefs for Color
How can you use playerprefs to save colors? I know about int, string but there it seems like it cant save color.
Answer by vexe · Sep 13, 2014 at 07:29 AM
hmm I thought it's self-explanatory - it's just a script that offers more features than the normal PlayerPrefs. It has a Set/Get methods for arrays, Quaternions, Vectors, Colors and much more. Just like GetString/SetString, etc in PlayerPrefs. Just copy-paste the script and use it like you normally use PlayerPrefs.
PlayerPrefsX.SetColor("Color $$anonymous$$ey", colorValue);
var color = PlayerPrefsX.GetColor("Color $$anonymous$$ey");
Just read the description and usage. There's a C# version too.
@hubert322 anything else dear? if your problem is solved, tick an answer
Answer by code_warrior · Sep 13, 2014 at 08:34 AM
Hey hubert,
this is an easy example (JavaScript) which shows how to save a color into a playerprefab and how to parse the playerprefab string afterwards back into a color that can be used as an material color for a gameobject.
function Start () {
//I've choosen one of the default colors (red)
PlayerPrefs.SetString("Color", Color.red.ToString());
ColorGO();
}
function ColorGO () {
var str_color : String;
str_color = PlayerPrefs.GetString("Color");
//Remove the header and brackets
str_color = str_color.Replace("RGBA(", "");
str_color = str_color.Replace(")", "");
//Get the individual values (red green blue and alpha)
var strings = str_color.Split(","[0] );
var outputcolor : Color;
for (var i = 0; i < 4; i++) {
outputcolor[i] = System.Single.Parse(strings[i]);
}
//apply the color to a gameobject
this.gameObject.renderer.material.color = outputcolor;
}
Codewarrior
Hey Hubert,
this is a C# version of this script from above:
using UnityEngine;
using System.Collections;
public class PrefColor : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
//I've choosen one of the default colors (red)
PlayerPrefs.SetString("Color", Color.red.ToString());
ColorGO();
}
void ColorGO () {
string str_color;
str_color = PlayerPrefs.GetString("Color");
//Remove the header and brackets
str_color = str_color.Replace("RGBA(", "");
str_color = str_color.Replace(")", "");
//Get the individual values (red green blue and alpha)
var strings = str_color.Split(","[0] );
Color outputcolor;
outputcolor = Color.black;
for (var i = 0; i < 4; i++) {
outputcolor[i] = System.Single.Parse(strings[i]);
}
//apply the color to a gameobject
this.gameObject.renderer.material.color = outputcolor;
}
}
Answer by doztep · Dec 30, 2019 at 10:47 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 Nk_Khumbhani · Nov 29, 2019 at 07:26 AM
private float r, g, b;
r = color.r; g = color.g; b = color.b;
PlayerPrefs.SetFloat("CarColorRed",color.r);
PlayerPrefs.SetFloat("CarColorGreen",color.g);
PlayerPrefs.SetFloat("CarColorBlue",color.b);
newVehicle.GetComponent().material.color =new Color(PlayerPrefs.GetFloat("CarColorRed"),PlayerPrefs.GetFloat("CarColorGreen"),PlayerPrefs.GetFloat("CarColorBlue"));
Your answer
![](https://koobas.hobune.stream/wayback/20220613162913im_/https://answers.unity.com/themes/thub/images/avi.jpg)