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.
3 Answers
3Hi @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);
}
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 _= System.Single.Parse (strings *);*_
* }*
* 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 = GetSaveColor (key + i);
* }
return tem;
}
}*_
No, the ray itself doesn't do anything. You have to use it in another method like Physics.Raycast. Even then it won't help, because the arrow doesn't have a collider. You could just use a Button component which surely works... By the way, is there an EventSystem in the scene?
– JedBeryllInstead 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*;*
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*);*
}
Edit: Adding “#” in front of colorHtmlString is required (see: Unity - Scripting API: ColorUtility.TryParseHtmlString)
You know I restarted my system and it now does not freeze, it just doesn't do anything (as if the Event Trigger is broken). I never had to kill the process through TM, btw. just click out and hit the play button. I'll snoop through my scripts to see if there is anything sitting around that may cause problems.
– wolftek3