查看: 821|回复: 0
打印 上一主题 下一主题

[其他] 游戏主角的选择(两个或多个)

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53488
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-8-30 00:57:03 |只看该作者 |倒序浏览
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-13 02:22 , Processed in 0.058213 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部