【发布时间】: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的检查器中