Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by hubert322 · Sep 13, 2014 at 07:27 AM · colorplayerprefs

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.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vexe · Sep 13, 2014 at 07:29 AM

http://wiki.unity3d.com/index.php/ArrayPrefs2

Comment
Add comment · Show 3 · 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 hubert322 · Sep 13, 2014 at 10:53 AM 0
Share

Can u explain about ArrayPrefs2?

avatar image vexe · Sep 13, 2014 at 06:38 PM 1
Share

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.

avatar image vexe · Sep 15, 2014 at 06:22 AM -1
Share

@hubert322 anything else dear? if your problem is solved, tick an answer

avatar image
1
Wiki

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

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 hubert322 · Sep 13, 2014 at 10:30 AM 0
Share

Can you offer the c# version?

avatar image code_warrior · Sep 16, 2014 at 05:50 PM 2
Share

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;
     }
 }
 
avatar image
1

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)

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
0

Answer by pareshkh · Sep 06, 2018 at 07:55 AM

visite this answer.

https://answers.unity.com/questions/1447929/how-to-save-color.html?childToView=1550296#answer-1550296

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
0

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

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

8 People are following this question.

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

Related Questions

Use A button to set a color using playerprefs in a different scene? 2 Answers

panel and button 1 Answer

Help with Playerprefs to saving colors of buttons. 0 Answers

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer


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