Untitled

 avatar
unknown
plain_text
10 months ago
865 B
22
Indexable
using UnityEngine;

public class UIButtonToggle : MonoBehaviour
{
    public GameObject cube1;
    public GameObject sphere1;
    public GameObject cube2;
    public GameObject sphere2;

    private GameObject[] objects;
    private int currentIndex = 0;

    void Start()
    {
        // 建立陣列,方便循環切換
        objects = new GameObject[] { cube1, sphere1, cube2, sphere2 };

        // 初始化:只顯示第一個
        ShowOnly(currentIndex);
    }

    public void ToggleNext()
    {
        currentIndex = (currentIndex + 1) % objects.Length;
        ShowOnly(currentIndex);
    }

    private void ShowOnly(int indexToShow)
    {
        for (int i = 0; i < objects.Length; i++)
        {
            if (objects[i] != null)
                objects[i].SetActive(i == indexToShow);
        }
    }
}
Editor is loading...
Leave a Comment