简体   繁体   中英

How to save a color

Okay so I've tried using floats and strings but they don't seem to work. The console just throws up cannot convert type bleh to type bleh. I've looked this up and people suggest using PlayerPrefs.SetColor but it seems as if unity have got rid of that. I'm not really sure how I'm suppose to save a color now. I did find this on the unity website, however I'm unsure on how to implement it.

Renderer rend = GetComponent<Renderer>();
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.red); Any help here? 

any help?

okay so this is what i tried:

PlayerPrefs.SetString("myColor1", Title.myColor1);
PlayerPrefs.SetString("myColor2", Title.myColor2);

and on another script:

myColor1 = PlayerPrefs.GetString("myColor1");
myColor2 = PlayerPrefs.GetString("myColor2");

I would suggest you to use ArrayPrefs2 so you can just use PlayerPrefsX.SetColor() and PlayerPrefsX.GetColor() or take it as reference to do some specific to your needs.

edited: To be more specific, you need to copy the content of this PlayerPrefsX class provided into a PlayerPrefsX class somewhere in your scripts (there are c# and unity script versions). Or just use that code as you want to create another helper as you wish.

You can serialize the color to string using Unity ColorUtility

const string Key = "my_stored_color";

public void SetColor(Color color)
{
    PlayerPrefs.SetString(Key, ColorUtility.ToHtmlStringRGBA(color));
}

public Color GetColor()
{
    var storedColorAsString = "#" + PlayerPrefs.GetString(key);
    Color result;
    ColorUtility.TryParseHtmlString(storedColorAsString, out result);
    return result;
}

Make sure to add the # before parsing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM