纳金网

标题: 游戏主角的选择(两个或多个) [打印本页]

作者: 烟雨    时间: 2015-8-30 00:57
标题: 游戏主角的选择(两个或多个)
  1. public class Script : MonoBehaviour {
  2.     public GameObject[] characterPrefabs;  //人物的预设体
  3.     private GameObject[] characterGameObjects; //把人物装进一个数组里
  4.     private int selectedIndex = 0;  //当前选择的人物是第几个
  5.     private int length;    //所有可供选择的角色个数
  6. // Use this for initialization
  7. void Start () {
  8.         length = characterPrefabs.Length;
  9.         characterGameObjects = new GameObject[length];
  10.         for (int i = 0; i < length; i++)
  11.         {
  12.             characterGameObjects[i] = GameObject.Instantiate(characterPrefabs[i], transform.position, Quaternion.identity) as GameObject;
  13.         }
  14.         UpdateCharacterShow();
  15. }

  16. // Update is called once per frame
  17. void Update () {

  18. }
  19.     void UpdateCharacterShow()  //更新所有角色的显示
  20.     {
  21.         characterGameObjects[selectedIndex].SetActive(true);
  22.         for (int i = 0; i < length; i++)
  23.         {
  24.             if (i!=selectedIndex)
  25.             {
  26.                 characterGameObjects[i].SetActive(false);   //把未选择的角色设置为隐藏
  27.             }
  28.         }
  29.     }
  30.     public void OnNextButtonClick() //当我们点击了下一个按钮
  31.     {
  32.         selectedIndex++;
  33.         selectedIndex %= length;
  34.         UpdateCharacterShow();
  35.     }
  36.     public void OnPrevButtonClick()//当我们点击上一个按钮
  37.     {
  38.         selectedIndex--;
  39.         if (selectedIndex==-1)
  40.         {
  41.             selectedIndex = length - 1;
  42.         }
  43.         UpdateCharacterShow();
  44.     }
  45. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5