简体   繁体   English

如何保存颜色

[英]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.控制台只是抛出无法将类型 bleh 转换为类型 bleh。 I've looked this up and people suggest using PlayerPrefs.SetColor but it seems as if unity have got rid of that.PlayerPrefs.SetColor了一下,人们建议使用PlayerPrefs.SetColor但似乎统一已经摆脱了这一点。 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.我建议您使用ArrayPrefs2,这样您就可以只使用PlayerPrefsX.SetColor()PlayerPrefsX.GetColor()或将其作为参考来执行一些特定于您的需求。

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).编辑:更具体地说,您需要将提供的此 PlayerPrefsX 类的内容复制到脚本中某处的 PlayerPrefsX 类中(有 c# 和 unity 脚本版本)。 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您可以使用 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解析前一定要加上#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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