Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_G2yrNlWYdpJKuw · Dec 29, 2017 at 11:50 AM · unity 5colorplayerprefssavesave data

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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);
     }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_G2yrNlWYdpJKuw · Dec 29, 2017 at 01:19 PM 0
Share

Why is it so complicated? There isn't any easier way?

avatar image Grish_tad unity_G2yrNlWYdpJKuw · Dec 29, 2017 at 01:59 PM 0
Share

Sorry but i don't know other way.

avatar image
0

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;
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges