c# - How can I use PlayerPrefs to save the color values of the object?
问题描述
I'm trying to save the object's color based on what the user chooses and the load them back onto the screen on a key press. With some help from the answers, I managed to find a way to save color RGB values with PlayerPrefs, however, I'm not sure how to set "colorObject" to the object's current color. I've seen solutions where new Color() and predefined sets of colors are used, but I want to save what the user chooses. Is there a way to set "colorObject" to the current color of the object?
/* Changing the color via key presses
*
*/
if (Input.GetKeyDown(KeyCode.R))
{
rend.material.SetColor("_Color", Color.red);
}
if (Input.GetKeyDown(KeyCode.G))
{
rend.material.SetColor("_Color", Color.green);
}
if (Input.GetKeyDown(KeyCode.B))
{
rend.material.SetColor("_Color", Color.blue);
}
}
// To add button elements to the visual interface
void OnGUI()
{
// Saving
if (GUI.Button(new Rect(700, 330, 50, 30), "Save"))
{
// Saving the object's color
Color colorOfObject = new Color();
PlayerPrefs.SetFloat("rValue", colorOfObject.r);
PlayerPrefs.SetFloat("gValue", colorOfObject.g);
PlayerPrefs.SetFloat("bValue", colorOfObject.b);
}
// Loading
if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
{
Color colorOfObject = new Color(PlayerPrefs.GetFloat("rValue", 1F), PlayerPrefs.GetFloat("gValue", 1F), PlayerPrefs.GetFloat("bValue", 1F));
}
解决方案
你可以这样做;
public static void SaveColor (Color color, string key) {
PlayerPrefs.SetFloat(key + "R", color.r);
PlayerPrefs.SetFloat(key + "G", color.g);
PlayerPrefs.SetFloat(key + "B", color.b);
}
public static Color GetColor (string key) {
float R = PlayerPrefs.GetFloat(key + "R");
float G = PlayerPrefs.GetFloat(key + "G");
float B = PlayerPrefs.GetFloat(key + "B");
return new Color(R, G, B);
}
或者你可以将它的十六进制代码保存为字符串并加载它
推荐阅读
- c# - 如何在按钮悬停时在面板周围绘制边框?
- pyspark - pyspark 使用另一个字段作为源以逐行方式生成 rdd
- amazon-web-services - 将相同的生命周期规则应用于多个存储桶
- jquery - 如何避免 JQuery 弹出窗口覆盖整个屏幕宽度?
- c# - 删除另一个线程正在使用的文件中的重复行
- c# - c# 没有显示整个结果
- python-2.7 - RequestsDependencyWarning:urllib3 (1.23) 或 chardet (2.3.0) 与支持的版本不匹配
- java - spring data fetch mapped object in order
- r - Error when trying to calculate column average - R
- java - org.hamcrest.Matcher 的方法 equalTo() 返回值像这样 <
>