Unity3D 選擇焦點切換


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Assets.Scripts.Models
{
    /// <summary>
    /// 該類用於登錄頁面中用戶名、密碼框的切換
    /// </summary>
    class MyTabClass : MonoBehaviour
    {
        /// <summary>
        /// 需要切換的元素
        /// </summary>
        public GameObject UserNameObj,PasswordObj;

        private EventSystem system;

        private void Start()
        {
            system = EventSystem.current;//獲取當前場景EventSystem
        }
        private void Update()
        {
            if (UserNameObj != null && PasswordObj != null) {
                if (Input.GetKeyDown(KeyCode.Tab)) {//按下Tab鍵,如果用戶名沒被選中,就選擇用戶名,否則選擇密碼框
                    if (system.currentSelectedGameObject != UserNameObj)
                    {
                        system.SetSelectedGameObject(UserNameObj, new BaseEventData(system));
                    }
                    else {
                        system.SetSelectedGameObject(PasswordObj, new BaseEventData(system));
                    }
                }
            }
        }

    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM