using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class loadlevels : MonoBehaviour {
public Button b2;
public Button b3;
void OnLevelWasLoaded() {
int MyLevel = PlayerPrefs.GetInt("Level");
switch (MyLevel) {
case 2:
b2.interactable = true;
b2.image.sprite = Resources.Load<Sprite> ("UISprite");
break;
case 3:
b3.interactable = true;
b3.image.sprite = Resources.Load<Sprite>("UISprite");
break;
}
}
}
我使用playerprefs保存按钮状态,但遇到一个问题:解锁第三级(按钮3)时,按钮2进入锁定状态。奇怪的是,解锁第三关时按钮2没有保存。
看起来您想要一组将启用到特定索引的按钮。您使用
switch
进行编码只会启用与级别完全匹配的一个按钮。
public Button[] buttons = new Button[4];
// don't forget to create / set in array
for( int level = 0; level <= myLevel; level++)
{
buttons[level].interactable = true;
buttons[level].image.sprite = Resources.Load<Sprite> ("UISprite");
}