保存换色器

Saving Color Changer

我有这个代码设置,可以让我改变我的播放器的颜色。在 Unity 测试器中,当我点击播放时,我可以更改我的播放器的颜色并在保存该颜色的场景之间切换,但是当我构建我的项目时,我只能在播放器自定义菜单中更改颜色,当我更改场景时,我会配备默认颜色.

using UnityEngine;

public class CustomizeColors : MonoBehaviour
{
    public Color[] headColors;
    public Material headMat;

    public void ChangeHeadColor(int colorIndex)
    {
        headMat.color = headColors[colorIndex];
        PlayerPrefs.SetInt("HeadColor", colorIndex);
        PlayerPrefs.Save();
    }
}

您在此处显示的代码不完整。如果你想获得保存的颜色,你可以在 Start() 函数中使用 PlayerPrefs.GetInt()。正如我所说,如果您更新您的问题并向我们展示您在开始时用于 select 颜色的 class 的完整代码,我可以为您提供帮助。

开始之前请删除您的 PlayerPrefs.cs 脚本,因为它会干扰 UnityEngine。这应该有效:

using UnityEngine;

public class CustomizeColors : MonoBehaviour
{
    public Color[] Colors;
    public Material Mat;


    public void Start()
    {
        if (PlayerPrefs.HasKey("HeadColor"))
        {
            Mat.color = Colors[PlayerPrefs.GetInt("HeadColor")];
            Debug.Log("works");
        }
    }

    public void ChangeColor(int colorIndex)
    {
        Mat.color = Colors[colorIndex];
        PlayerPrefs.SetInt("HeadColor", colorIndex);
        PlayerPrefs.Save();
    }
}