Unity GameObject SetActive保存到playerprefs

时间:2017-12-02 08:58:48

标签: unity5 gameobject

我想保存每个游戏对象的活动状态,这样当你重新加载时,你收集的游戏就不再存在了。 我的代码:

public class PlayerController : MonoBehaviour {

     public LevelManager levelManager;          
     public GameObject[] stardust; //array of stardust in level
     public int isActive; //variable for amount of stardust collected

     void Start()
     {            
         isActive = PlayerPrefs.GetInt("stardust");
         active();
     }

     void OnTriggerEnter(Collider other)
     {    
         //Stardust
         if (other.gameObject.CompareTag("stardust"))
         {
             isActive = 1;
             PlayerPrefs.SetInt("stardust", isActive);           
             active();
             //other.gameObject.SetActive(false);
             levelManager.score++;
             levelManager.setScoreText();
             audioSource.Play();
             PlayerPrefs.SetInt("score", levelManager.score);
         }
     }

     void active()
     {
         if (isActive == 0) //if none are collected
         {
             for (int i = 0; i < stardust.Length; i++)
             {
                 stardust[i].SetActive(true); //make all visible
             }
         }
         if (isActive == 1) //first one collected
         {
             stardust[0].SetActive(false);
             stardust[1].SetActive(false);
             stardust[2].SetActive(false);
         }    
     }
 }

正如您所看到的,它将所有对象设置为非活动状态,如何单独检查每个对象?

我在所有星尘中使用了playerprefs中的单个键。我应该为每个星尘设置一个关键吗?

我尝试在EventTrigger下按标签比较每个标签,每个方法检查每个标签,并单独设置Active,不成功

1 个答案:

答案 0 :(得分:0)

嗨Curstin Visagie,                     它不会使用单个键,将覆盖playerprefs值,你必须为每个星尘创建每个键。