【发布时间】:2019-05-15 16:03:33
【问题描述】:
我的 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");
}
}
【问题讨论】: