話不多啥,博主我先放個影片給你們康康,😁😋😊😛😄

創建一個空物體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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/235578.html
標籤:其他
上一篇:JAVA小游戲 混亂大槍戰
