Unity3d 如何在unity中使用角色选择菜单中的playerprefs
我有一个字符选择菜单,你应该有足够的硬币购买(解锁)字符。一旦选择了其中一个角色,您的硬币必须根据其价格减少。当您重新启动游戏并再次选择该角色时,该角色不得被锁定,您的硬币也不得减少。我试过不同的方法,但都不管用。我无意中看了录像。这是我的剧本Unity3d 如何在unity中使用角色选择菜单中的playerprefs,unity3d,Unity3d,我有一个字符选择菜单,你应该有足够的硬币购买(解锁)字符。一旦选择了其中一个角色,您的硬币必须根据其价格减少。当您重新启动游戏并再次选择该角色时,该角色不得被锁定,您的硬币也不得减少。我试过不同的方法,但都不管用。我无意中看了录像。这是我的剧本 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using Uni
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEditor;
public class MainMenu3 : MonoBehaviour {
int SceneIndex;
public Button BluePlane;
public Button WhitePlane;
public GameObject wood;
public static int character_number;
public int isPlanesold;
// Use this for initialization
void Start () {
isPlanesold = PlayerPrefs.GetInt ("isPlanesold");
if (!PlayerPrefs.HasKey ("Number")) {
PlayerPrefs.SetInt ("Number", 1);
}
character_number = PlayerPrefs.GetInt ("Number");
UIManager2.coin_score = PlayerPrefs.GetInt ("Score");
SceneIndex = SceneManager.GetActiveScene ().buildIndex;
}
public void Blueplane () {
character_number = 1;
SceneManager.LoadScene ("Menu2");
}
public void BuyWhitePlane () {
if (UIManager2.coin_score >= 1 && isPlanesold ==0) {
WhitePlane.interactable = true;
PlayerPrefs.SetInt ("isPlanesold", 1);
character_number = 2;
UIManager2.coin_score--;
wood.SetActive (false);
PlayerPrefs.SetInt ("Score", UIManager2.coin_score);
PlayerPrefs.SetInt ("Number", character_number);
SceneManager.LoadScene ("Menu2");
}
else
WhitePlane.interactable = false;
}
void Update () {
if (Input.GetKeyDown (KeyCode.Escape))
SceneManager.LoadScene (SceneIndex - 1);
}
例如,当我从BluePlane获得1枚默认已解锁的硬币并单击WhitePlane按钮时,它工作正常,但当我重新启动游戏时,按钮返回WhitePlane.Interactiable=false;我知道我该怎么做,但我真的不知道怎么做 好的,从我看到你有两个问题 首先,您没有给
isPlanesold
一个默认值。当您第一次获得一个PlayerPref
时,如果当前不存在该值,则为其指定一个默认值
其次,我认为这是因为对于BuyWhitePlane,您没有“已解锁”选项,只有“未解锁”和“无法解锁”。如果isPlaneSold
设置为1(就像您购买飞机时那样),则无法再次选择白飞机(即重新加载游戏时)
此外,我会在用户购买游戏后调用PlayerPrefs.Save()
,以防游戏过早退出或崩溃,这样用户将保持进度
因此,我认为您的脚本应该如下所示:
public class MainMenu3 : MonoBehaviour
{
[SerializeField] Button BluePlane;
[SerializeField] Button WhitePlane;
[SerializeField] GameObject wood;
int character_number;
int isPlanesold;
int score;
void Start()
{
isPlanesold = PlayerPrefs.GetInt("isPlanesold", 0); // I guess you're using this like a true/false? So the plane won't to be sold at first, so it would be zero. Could also convert to a bool with Convert.ToBoolean()
character_number = PlayerPrefs.GetInt("Number", 1);
score = PlayerPrefs.GetInt("Score", 0);
}
public void Blueplane()
{
PlayerPrefs.SetInt("Number", 1);
PlayerPrefs.Save();
SceneManager.LoadScene("Menu2");
}
public void BuyWhitePlane()
{
if (isPlaneSold == 1)
{
PlayerPrefs.SetInt("Number", 2);
PlayerPrefs.Save();
SceneManager.LoadScene("Menu2");
}
else if (score >= 1 && isPlanesold == 0)
{
PlayerPrefs.SetInt("isPlanesold", 1);
PlayerPrefs.SetInt("Number", 2);
PlayerPrefs.SetInt("Score", score--);
PlayerPrefs.Save();
// wood.SetActive(false); If you're immediately changing scenes, whats the point in doing this?
SceneManager.LoadScene("Menu2");
}
else
{
Debug.Log("You don't have the score!");
// Do stuff, maybe an UI error message?
// If you wanted to make the button not intractable if the user didn't have the score you could check the score value in start and if it was 0 set WhileButton.intractable = false;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
如果您计划添加更多平面,并且isPlaneSold
的值将增加超过1,那么我建议您添加一个方法,不要重复相同的操作
PlayerPrefs.SetInt("isPlanesold", 1);
PlayerPrefs.SetInt("Number", 2);
PlayerPrefs.SetInt("Score", score--);
PlayerPrefs.Save();
代码
另一方面,使用PlayerPref
存储这样的信息(即用户解锁和硬币)是不好的,因为PlayerPref
的数据在用户设备的某个地方处于不安全的火灾中。它应该只用于存储用户已经可以自由使用的内容(如设置)。更好的解决方案是使用二进制格式化程序
,我建议通过视频学习如何使用它们
此外,我看不到您的其余代码,因此我不能确定,但当您在PlayerPrefs
中保存UIManager2.coinscore
和character\u number
时,它们不需要是静态的?如果您不知道自己在做什么,静态变量可能会导致一些奇怪的行为,作为一个粗略的指南
希望这有帮助,祝你好运
PlayerPrefs.GetInt(“Score”)
中的分数在哪里初始化?我的游戏就像一只扑翼鸟,每n个分数,你的硬币号码(coin_分数)必须增加1个单位。coin_score是一个静态int变量,可以放在文本用户界面上,并显示在游戏场景中(左上角)。非常感谢!你救了我的命!上帝保佑你!我使用character_number作为静态变量,因为它必须在另一个场景中引用。对于游戏对象(木头),因为它是一把锁,一旦玩家购买了这个角色,它就不能再显示了。为了保存wood.SetActive(false),我定义了一个布尔变量,将其命名为(iswoodunlocked),并在start()中将其设置为wood.SetActive(iswoodunlocked),但它不起作用。谢谢anyway@amin007您可以为wood
变量创建另一个PlayerPref
(类似于playerPrefs.GetString(“isWoodUnlocked”,“false”)
,在用户购买飞机时将其设置为true
,然后检查如果(playerPrefs.GetString(“isWoodUnlocked”)=“true”)
在Start()
中。至于静态变量,我只是有点困惑,因为你可以在下一个场景中检查PlayerPref.GetInt(“Number”)
而不是使用静态变量。无论如何,很高兴我能帮上忙:)我真蠢!谢谢兄弟!我是新来的团结!但还有一件事!当我设置另一个平面时,如果硬币数量为2,我首先购买了第一个平面(硬币数量为1),那么我的硬币数量将按计划减少。但是当我收集到足够的硬币来买下一架飞机并买了那架飞机时,硬币的号码没有改变。我猜1号飞机的coin_分数一定有定义。正如你所说的,也许下一步我应该用另一种方法planes@amin007没问题,伙计,每个人都从头开始。我不太明白你的意思。如果你指的是你在游戏中看到的硬币数量,你可能会忘记在购买新飞机时实际更新价值(即,如果你有一些文本告诉用户他们有多少硬币,你可能会忘记更改)。否则,请记住更改PlayerPref
中硬币的价值,类似于PlayerPrefs.SetInt(“硬币”,currentCoins-planeCost)
。祝你好运!