본문 바로가기
Unity3D/Unity & C#

Unity & C# ~ Tab 키를 눌러서 입력 필드 위치 변경 (public list 활용)

by 캬캬백곰 2022. 11. 17.
728x90

Tab 키를 눌렀을 때 입력 필드 위치를 변경하는 방법 중에서 특정 오브젝트만을 list에 담아 적용하는 방법입니다.

using System.Collections.Generic;

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Tabkey : MonoBehaviour
{
    [SerializeField] private List<Selectable> list;
    private int currentSelectedNum = 0;

    private void Update()
    {
    	if (Input.GetKeyDown(KeyCode.Tab))
        {
        	//event system에서 선택한 오브젝트가 있는지를 검사
            if(EventSystem.current.currentSelectedGameObject != null)
            {
            	//선택한 오브젝트가 있을 경우, 해당 오브젝트가 리스트에 있는 오브젝트인지 검사
                if (list.Contains(EventSystem.current.currentSelectedGameObject.GetComponent<Selectable>()))
                {
                    var index = list.FindIndex(x => x.Equals(EventSystem.current.currentSelectedGameObject.GetComponent<Selectable>()));

                    currentSelectedNum = index;
                }
            }

            if (currentSelectedNum <= list.Count - 2)
                currentSelectedNum++;

            list[currentSelectedNum].Select();
        }
    }
}
728x90
반응형