【问题标题】:Difficulty settings carryover to game scenes via playerprefs通过 playerprefs 将难度设置转移到游戏场景
【发布时间】:2021-07-04 20:56:47
【问题描述】:

感谢一些优秀的建议,我有难度按钮来改变我的游戏的刷新率。但是,这些变化并没有转化为后续场景。谁能发现我的 Playerprefs 设置的问题?

难度按钮脚本

私有Button按钮;

public RandomSpawn randomSpawn;

public int difficulty;


void Start()
{
    button = GetComponent<Button>(); 
    randomSpawn = GameObject.Find("Random Spawn").GetComponent<RandomSpawn>();
    button.onClick.AddListener(delegate{randomSpawn.UpdateSpawnRate(difficulty);});
}

void SetDifficulty()
{
    Debug.Log(gameObject.name + "was clicked");
}

 void Update()
{
    PlayerPrefs.SetInt("Player Difficulty", difficulty);
}

}

生成脚本

public GameObject prefab1, prefab2, prefab3, prefab4;

public float maxSpawnRate = 2f;

private float nextSpawn = 0f;

private int whatToSpawn;

private float spawnRate = 2f;

public static int difficulty = 0;

private void Start()
{
    difficulty = PlayerPrefs.GetInt("Player Difficulty");
}

void Update()
{
    
    if (Time.time > nextSpawn) { //if time has come
        whatToSpawn = Random.Range(1, 6); // define random value between 1 and 4 (5 is exclusive)
        Debug.Log(whatToSpawn); //display its value in console
   
   
        switch (whatToSpawn) {
            case 1:
                Instantiate(prefab1, transform.position, Quaternion.identity);
                break;
            case 2:
                Instantiate(prefab2, transform.position, Quaternion.identity);
                break;
            case 3:
                Instantiate(prefab3, transform.position, Quaternion.identity);
                break;
            case 4:
                Instantiate(prefab4, transform.position, Quaternion.identity);
                break;
       
        }
 
        nextSpawn = Time.time + spawnRate;
    }
}

public void UpdateSpawnRate(int difficulty)
{
    spawnRate = maxSpawnRate / difficulty;
}

}

SetDifficulty 没有显示为 Inspector 的 OnClick enter image description here 的选项

【问题讨论】:

  • 在运行时添加的回调永远不会出现在 onClick 的检查器中

标签: unity3d scripting


【解决方案1】:

我认为问题在于在Update() 方法中使用SetInt,因为您有多个使用RandomSpawn 脚本的对象。它会随着每个对象的难度不断覆盖这个值。

private Button button;

public int difficulty;


void Start()
{
    button = GetComponent<Button>();

    // can also add the onclick to your button here
    button.onClick.AddListener(delegate{SetDifficulty();});
}

// make this your buttons onClick in the hierarchy
public void SetDifficulty()
{
    PlayerPrefs.SetInt("Player Difficulty", difficulty);
    Debug.Log(gameObject.name + "was clicked");
}
}

SetDifficulty 是按钮的 onClick 吗?如果是这样,如果您在检查器中分配 onClick,则不再需要 button.onClick.AddListener。我会将 PlayerPrefs.SetInt 移动到您的按钮发生 onClick 的任何位置,这样您就可以在该实例中设置难度,而不是在 Update() 中的每一帧。

我现在正在编辑 spawn 脚本以根据难度和 maxSpawn 更改 spawnRate;

public GameObject prefab1, prefab2, prefab3, prefab4;

public float maxSpawnRate = 2f;

private float nextSpawn = 0f;

private int whatToSpawn;

private float spawnRate = 2f;

public static int difficulty = 0;

private void Start()
{
    difficulty = PlayerPrefs.GetInt("Player Difficulty");
    spawnRate = maxSpawnRate / difficulty;
}

void Update()
{
    
    if (Time.time > nextSpawn) { //if time has come
        whatToSpawn = Random.Range(1, 6); // define random value between 1 and 4 (5 is exclusive)
        Debug.Log(whatToSpawn); //display its value in console
   
   
        switch (whatToSpawn) {
            case 1:
                Instantiate(prefab1, transform.position, Quaternion.identity);
                break;
            case 2:
                Instantiate(prefab2, transform.position, Quaternion.identity);
                break;
            case 3:
                Instantiate(prefab3, transform.position, Quaternion.identity);
                break;
            case 4:
                Instantiate(prefab4, transform.position, Quaternion.identity);
                break;
       
        }
 
        nextSpawn = Time.time + spawnRate;
    }
}

【讨论】:

  • 感谢您的快速响应。我删除了 button.onclick 行并粘贴到上面的 mods 中,但仍然没有将设置转移到后续场景。我注意到在后续场景的 Inspector 中更改由 randomspawn 生成的框中的 maxspawnrate 也对生成器没有影响。好像我们已经很接近了,但还是有什么不对劲。调试日志很干净,但奇怪的是没有显示“被点击”的任何内容——在点击 Easy 等时它会确认
  • @xaltx 您是否发布了两者的完整脚本?我在 Spawner 脚本中注意到不再有影响 spawnRate 的代码。我更新了代码,所以它应该可以工作。
  • 现在似乎可以工作了!!!检查器中对 maxspawn 的调整有所帮助。现在逐级进行更彻底的检查 - 完成后会报告。
  • @xaltx 很高兴听到!希望现在一切正常。
  • 确认一切正常! @TEEBQNE 非常感谢您的善意、时间和耐心。
猜你喜欢
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
相关资源
最近更新 更多