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));
                    }
                }
            }
        }

    }
}

  

原文地址:https://www.cnblogs.com/nick-jd/p/12420506.html