Tab鍵切換選擇物件

菠蘿拿鐵發表於2020-11-15

Tab切換選擇


/* ***********************************************
* Discribe:Tab鍵迴圈選擇列表內物件
* Author:Demo
* CreateTime:2020-11-15 11:18:22
* Edition:1.0
* ************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class SelectGameObject : MonoBehaviour
{

    public List<GameObject> SelectPanel;
    private EventSystem system;

    int Index;
    public int index
    {
        get
        {
            return Index;
        }

        set
        {
            Index = value;
            if (Index > SelectPanel.Count -1)
            {
                Index = 0;
            }

            if (Index < 0)
            {
                Index = SelectPanel.Count - 1;
            }
        }
    }




    private void Start()
    {
        system = EventSystem.current;
    }

    private void Update()
    {
        if (SelectPanel.Count > 0)
        {
            if (Input.GetKeyDown(KeyCode.Tab))
            {
                index++;
                Debug.LogError($"Current: {system.currentSelectedGameObject} Next: {SelectPanel[index].name}");
                system.SetSelectedGameObject(SelectPanel[index], new BaseEventData(system));
            }
        }
    }
}

 

相關文章