话不多啥,博主我先放个动画给你们康康。😁😋😊😛😄
创建一个空物体GameObject加上脚本CharacterCreation
源码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterCreation : MonoBehaviour
{
public GameObject[] characterPrefabs;
private GameObject[] characterGameObjects;
private int selectedIndex = 0;
private int length;//所有可供选择角色的个数;
// Start is called before the first frame update
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();
}
}
当代码完成后,我们需要在unity中的GameObject下,先将两个任务模型拖入。
接下来操作就是将各个物体拖入到相应的组件里。
拖入完成后就可以运行,实现点击按钮进行任务切换操作。
这个游戏,博主正在学习,写的不好,请多多见谅。(~ ̄▽ ̄)~φ(゜▽゜*)♪o(*^@^*)o