第二章角色创建
1.角色创建UI界面搭建
新建Sprite组件
创建取名为Title标题的Sprite组件
创建取名为Next和Prev的Sprite组件
分别为Next和添加Box Collider和Button Script与Play Sound组件
并调整他们的属性
新建取名为EnterName的Sprite组件并添加输入框预设物
添加Ok按钮并添加组件
调整它们的布局大小
2.设计lder状态的两个角色
设置两个人物角色模型
添加lder动画状态并存为预制体
3.控制所有角色的创建显示与切换
创建名为CharacterCreation的GameObject,放置于人物模型初始的位置
添加CharacterCreation代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterCreationl : MonoBehaviour {
public GameObject[] characterPrefabs;
public UIInput nameInput;//用来得到输入的文本
private GameObject[] characterGameObjects;
private int selectedIndex = 0;
private int length;//所有可供选择的角色的个数
// Use this for initialization
void Start () {
length = characterPrefabs.Length;
characterGameObjects = new GameObject[length];
for(int i = 0; i < length; i++)
{
characterGameObjects[i] = GameObject.Instantiate(characterPrefabs[i], transform.position, transform.rotation) as GameObject;
}
UpdateCharacterShow();
}
void UpdateCharacterShow()
{//更新所有角色的显示
characterGameObjects[selectedIndex].SetActive(true);
for(int i = 0; i < length; i++)
{
if (i != selectedIndex)
{
characterGameObjects[i].SetActive(false);//把未选择的角色设置为隐藏
}
}
}
public void OnNextButtonClick()
{//当我们点击了下一个按钮
selectedIndex++;
selectedIndex %= length;
UpdateCharacterShow();
}
public void OnPrevButtonClick()
{//当我们点击了上一个按钮
selectedIndex--;
if (selectedIndex == -1)
{
selectedIndex = length - 1;
}
UpdateCharacterShow();
}
public void OnOkButtonClick()
{//加载下一个场景
PlayerPrefs.SetInt("SelectedCharacterIndex", selectedIndex);//存储选择的角色
PlayerPrefs.SetString("name", nameInput.value);//存储输入的名字
//加载下一个场景
}
}
绑定按钮On Click点击
4.名称的输入和场景的切换
写入CharacterCreation赋值并绑定Ok按钮点击事件