我是团结的新手,我的编程能力仍然有点弱,我正在研究2D平台游戏,以此来学习如何编写代码来练习我的激情,即游戏开发
我现在的主要问题是我有两个场景(Level1,Level2)然后我有一个SceneSelctor 在选择器中,我有两个门代表两个级别,但我面临的问题是
我创建了一个小代码来锁定Level2,直到我达到一个点然后解锁它,就是当我开始游戏时,level2总是被解锁,这对我没有好处
控制这些内容的代码保存在两个脚本中 我使用Hastebin,因为我不知道如何在这里复制/粘贴代码
https://hastebin.com/oluzujukid.cs
===> LevelDoor.cs
public class LevelDoor : MonoBehaviour {
public string levelToLoad;
public bool unlocked;
// Use this for initialization
void Start () {
//first level must be always unlocked
PlayerPrefs.SetInt("Level1", 1);
//when the leveltoLoad = 1 that mean that the level should be unlocked
if(PlayerPrefs.GetInt(levelToLoad) == 1)
{
unlocked = true;
} else
{
unlocked = false;
}
//some animation stuff toshow doorOpen or closed
if(unlocked)
{
doorTop.sprite = doorTopOpen;
doorBottom.sprite = doorBottomOpen;
} else
{
doorTop.sprite = doorTopClosed;
doorBottom.sprite = doorBottomClosed;
}
}
===> LevelExit.cs
public class LevelExit : MonoBehaviour {
public string levelToUnlock;
}
public IEnumerator levelExitCo()
{
// if the player reach the checkpoint thisline unlock the second level on SceneSelect
PlayerPrefs.SetInt(levelToUnlock, 1);
}
答案 0 :(得分:0)
这就是我如何解决这个问题:
一旦解锁门,我会设置一个触发器(就在打开的门后面),这将加载一个新的场景。然后从一个场景移动到另一个场景我会使用
SceneManager.LoadScene()
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
在第二级别中,您可以随时放置另一扇门,已经打开,并触发,以便玩家可以回到第一级
使用其他方法进行编辑:
如果你想拥有一个可以按顺序解锁的不同级别的游戏,你可以做的一件事是创建一个单独的变量,这是一个独特的变量,将为所有游戏场景共享,类型为布尔值。数组中的每个元素都代表一个级别。最初你可以定义所有级别,但第一个级别为false(所以全部锁定),当你考虑一个级别可以解锁时你改变那个值
所以在你的游戏开始时你可以调用一个类似的脚本(假设你有10个级别):
public static bool[] levels = new bool[10];
levels[0]=true
for (int i = 1; i < levels.Length; i++)
{ levels[i] = false; }