游戏主角的选择(两个或多个)
热度 1174 2015-6-3 15:04 |个人分类:Unitty
public class Script : MonoBehaviour {
public GameObject[] characterPrefabs; //人物的预设体
private GameObject[] characterGameObjects; //把人物装进一个数组里
private int selectedIndex = 0; //当前选择的人物是第几个
private int length; //所有可供选择的角色个数
// Use this for initialization
void Start () {
length = characterPrefabs.Length;
characterGameObjects = new GameObject[length];
for (int i = 0; i < length; i++)
{
characterGameObjects[i] = GameObject.Instantiate(characterPrefabs[i], transform.position, Quaternion.identity) as GameObject;
}
UpdateCharacterShow();
}
// Update is called once per frame
void Update () {
}
void UpdateCharacterShow() //更新所有角色的显示
{
characterGameObjects[selectedIndex].SetActive(true);
for (int i = 0; i < length; i++)
{
if (i!=selectedIndex)
{
characterGameObjects[i].SetActive(false); //把未选择的角色设置为隐藏
}
}
}
public void OnNextButtonClick() //当我们点击了下一个按钮
{
selectedIndex++;
selectedIndex %= length;
UpdateCharacterShow();
}
public void OnPrevButtonClick()//当我们点击上一个按钮
{
selectedIndex--;
if (selectedIndex==-1)
{
selectedIndex = length - 1;
}
UpdateCharacterShow();
}
}