Unity 黑暗之光 笔记 第二章

第二章 角色创建

1.角色创建UI界面搭建

新建Sprite组件

创建取名为Title标题的Sprite组件

创建取名为Next和Prev的Sprite组件

分别为Next和添加Box Collider和Button Script与Play Sound组件

 
 

并调整他们的属性

 
 

新建取名为EnterName的Sprite组件并添加输入框预设物

 

添加Ok按钮并添加组件

 

调整它们的布局大小

2.设计Ider状态的两个角色

设置两个人物角色模型

添加Ider动画状态并存为预制体

3.控制所有角色的创建显示与切换

创建名为CharacterCreation的GameObject,放置于人物模型初始的位置

添加CharacterCreation代码

 1        public GameObject[] characterPrefabs;//存储所有角色的数组
 2        private GameObject[] characterGameObjects;//创建出来之后的游戏物体
 3        private int selectedIndex = 0;//当前选择的索引
 4        private int length;//所有角色的个数
 5        /// <summary>
 6        /// 初始化
 7        /// </summary>
 8        void Start()
 9        {
10               length = characterPrefabs.Length;
11               characterGameObjects = new GameObject[length];
12               for (int i = 0; i < length; i++)
13               {
14                      characterGameObjects[i] =  GameObject.Instantiate(characterPrefabs[i], transform.position,  transform.rotation) as GameObject;
15               }
16               UpdateCharacterShow();
17        }
18        /// <summary>
19        /// 更新所有角色的显示
20        /// </summary>
21        void UpdateCharacterShow()
22        {
23               characterGameObjects[selectedIndex].SetActive(true);
24         for (int i = 0; i < length; i++)
25         {
26             if (i != selectedIndex)
27             {
28                            characterGameObjects[i].SetActive(false);//把未选择的角色设置为隐藏
29             }
30         }
31     }
32     /// <summary>
33     /// 点击了上一个按钮
34     /// </summary>
35     public void OnPrevButtonClick()
36     {
37         selectedIndex--;
38         if (selectedIndex == -1)
39         {
40             selectedIndex = length - 1;
41         }
42         UpdateCharacterShow();
43     }
44     /// <summary>
45     /// 点击了下一个按钮
46     /// </summary>
47     public void OnNextButtonClick()
48        {
49               selectedIndex++;
50               selectedIndex %= length;
51               UpdateCharacterShow();
52        }

绑定按钮On Click点击

 

4.名称的输入和场景的切换

 写入CharacterCreation赋值并绑定Ok按钮点击事件

1        public UIInput nameInput;//用来得到输入的文本
2  
3        public void OnOkButtonClick()
4        {
5               PlayerPrefs.SetInt("SelectedCharacterIndex", selectedIndex);//存储选择的角色
6               PlayerPrefs.SetString("name", nameInput.value);//存储输入的名字
7               //加载下一个场景
8        }

 
 

关于Unity黑暗之光的其他笔记

posted @ 2020-12-07 08:53  白兔兔秃~  阅读(122)  评论(0)    收藏  举报
/* */