使用 PlayerPrefs 浮动使数组的某个元素可交互,同时所有 +1 元素保持锁定

Making a certain Element of an array interactable using a PlayerPrefs float while all of the +1 elements remain locked

所以基本上我有一个 Score/Highscore 系统,我还有一个数组,其中包含一些改变玩家角色颜色的按钮。所以我的问题是,如果我有 highscoreCount >= 20,我希望某个按钮可以交互。

ScoreManager 脚本

using UnityEngine;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour
{
    public Transform player;
    public Text scoreText;
    public Text highScoreText;

    public float scoreCount;
    public float highscoreCount;

    public float pointsPerSecond;

    public bool scoreIncreasing;

    public Button[] CustomizeButtons;


    void Start()
    {
        if (PlayerPrefs.HasKey("Highscore"))
        {
            highscoreCount = PlayerPrefs.GetFloat("Highscore");
        }

        int CustomizationButtonReached = PlayerPrefs.GetInt("CustomizationButtonReached", 1);
        {
            for (int i = 0; i < CustomizeButtons.Length; i++)
            {
                if (i + 1 > CustomizationButtonReached)
                    CustomizeButtons[i].interactable = false;
            }
        }
    }

    void Update()
    {

        if (scoreIncreasing)
        {
            scoreCount += pointsPerSecond * Time.deltaTime;
        }

        if(scoreCount > highscoreCount)
        {
            highscoreCount = scoreCount;
            PlayerPrefs.SetFloat("Highscore", highscoreCount);
        }

        scoreText.text = "Score: " + Mathf.Round(scoreCount);
        highScoreText.text = "Highscore: " + Mathf.Round(highscoreCount);
    }
}

自定义颜色脚本

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

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

本质上,这是我(在 ScoreManager 中)创建数组并禁用所有按钮的代码。我只想在 highscoreCount >= "number"

时让特定元素可交互
    public Button[] CustomizeButtons;


    void Start()
    {
        int CustomizationButtonReached = PlayerPrefs.GetInt("CustomizationButtonReached", 1);
        {
            for (int i = 0; i < CustomizeButtons.Length; i++)
            {
                if (i + 1 > CustomizationButtonReached)
                    CustomizeButtons[i].interactable = false;
            }
        }
    }

您可以采用多种方式解决此问题,例如存储该特定按钮的引用,当您提到的条件为真时,只需将该按钮设置为可交互

[SerializeField] Button button; //Set this in the inspector

public void AddToScore(int score)
{
   //Do your score adding stuff

   if (highscoreCount >= number)
   {
      button.interactable = true
   }
}