人物选择

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class CharactorCreation : MonoBehaviour {
 5     public GameObject[] prefabs;   //在Inspector中赋值的数组
 6     private GameObject[] gameobjectsPrefabs;     //实例化出来的人物
 7     private int length;    //数组的长度
 8     private int selectIndex = 0;   //当前选择的人物的Index
 9     public UIInput nameInput;
10 
11     // Use this for initialization
12     void Start () {
13         length =prefabs .Length ;
14         gameobjectsPrefabs = new GameObject[length];
15         for (int i = 0; i < length; i++) {
16             gameobjectsPrefabs[i] = GameObject.Instantiate(prefabs[i],transform.position,transform .rotation ) as GameObject;
17         }
18 
19     }
20 
21 
22     //下一个角色
23     public void OnNextButtonClick() {
24         selectIndex++;
25         selectIndex %= length;
26         UpdateCharacterShow();
27     }
28 
29     //上一个角色
30     public void OnPrevButtonClick() {
31         selectIndex--;
32         if (selectIndex == -1) {
33             selectIndex = length - 1;//最后一个
34         }
35         UpdateCharacterShow();
36     }
37 
38     //更新所有角色显示
39     public void UpdateCharacterShow()
40     {
41         //该selectindex显示   
42         gameobjectsPrefabs[selectIndex].SetActive(true);
43         //除了selectindex之外的不显示
44         for (int i = 0; i < length; i++)
45         {
46             if (i != selectIndex)
47             {
48                 gameobjectsPrefabs[i].SetActive(false);
49             }
50         }
51     }
52 
53     //OK按钮被按下的时候
54     public void OnOkButtonClick() {
55         PlayerPrefs.SetInt("SelectedCharacterIndex", selectIndex); //存储当前选择的角色
56         PlayerPrefs.SetString("name", nameInput.value);   //存储输入的名字
57         Application.LoadLevel(2);
58     }
59 }

 

posted @ 2016-02-02 22:47  半步_天涯  阅读(239)  评论(0)    收藏  举报