根据先前玩家的选择更改对象的颜色?

时间:2019-05-15 16:03:33

标签: c# arrays unity3d

我的Unity项目中有2个场景可供选择。要使用例如武器进入第二个场景,您必须在第一个场景中选择角色。我想根据我在角色选择场景中选择的角色来更改武器的颜色。如果一切都在数组上工作,该如何处理? 这是我在角色选择场景中实现的脚本:

 {
     private GameObject[] characterList;
     private int index;

     private void Start()
     {
         index = PlayerPrefs.GetInt("CharacterSelected");

         characterList = new GameObject[transform.childCount];

         //array w players
         for (int i = 0; i < transform.childCount; i++)

             characterList[i] = transform.GetChild(i).gameObject;

         foreach (GameObject go in characterList)

             go.SetActive(false);
             if (characterList[index])
                 characterList[index].SetActive(true);


     }

     public void ToggleLeft()
     {
         characterList[index].SetActive(false);  //turning off the old character

         index --;
         if (index < 0)
             index = characterList.Length - 1;

         characterList[index].SetActive(true);

     }

      public void ToggleRight()
     {
         characterList[index].SetActive(false);  //turning off the old character

         index++;
         if (index == characterList.Length)
             index = 0;
             characterList[index].SetActive(true);

     }

     public void GoBackToCharacter()
     {

         PlayerPrefs.SetInt("CharacterSelected", index);
         SceneManager.LoadScene("WeaponSelect");


     }

 }

1 个答案:

答案 0 :(得分:0)

您的问题与整个应用程序中的持久状态有关。我建议使用静态类在应用程序的整个生命周期(场景1和场景2)中保留字符属性,并在必要时访问适用的属性。一个基本示例如下:

public static class CharacaterClass
 {
   public static string Weapon {get; set; }
   public static string Color {get; set; }
 }

在scene1(Form1)中设置角色武器

 Character.Weapon = "gun";

然后抓住场景2(Form2)中的角色武器

 var charWeapon = Character.Weapon;