通过玩家偏好将难度设置转移到游戏场景
Difficulty settings carryover to game scenes via playerprefs
感谢一些很好的建议,我有难度按钮改变我的游戏的生成率。但是,这些更改并未转化为后续场景。谁能发现我的 Playerprefs 设置的问题?
难度按钮脚本
私有按钮;
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
我认为问题出在 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() 中设置每一帧。
我现在正在编辑生成脚本以根据难度和 maxSpawn 更改生成速率;
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;
}
}
感谢一些很好的建议,我有难度按钮改变我的游戏的生成率。但是,这些更改并未转化为后续场景。谁能发现我的 Playerprefs 设置的问题?
难度按钮脚本
私有按钮;
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
我认为问题出在 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() 中设置每一帧。
我现在正在编辑生成脚本以根据难度和 maxSpawn 更改生成速率;
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;
}
}