首页 > 其他 > 详细

人物选择

时间:2016-02-02 23:18:29      阅读:213      评论:0      收藏:0      [点我收藏+]
 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 }

 

人物选择

原文:http://www.cnblogs.com/wry524/p/5178680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!