您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 2浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3d用键盘操作自定义菜单

程序员正茂 发布时间:2021-01-05 18:31:30 ,浏览量:2

最近有个项目需要纯键盘操作菜单,本来写个通用类,菜单等级可以无限扩展下去,试了一下没成功,只好以二级菜单先搞出来。

按数字键1点击按钮 按数字键2切换同级按钮 按数字键3返回上级按钮  

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Menu : MonoBehaviour {
    //public getReal3DPlayerInputs getReal3dInputs;

    //主菜单
    public Button[] mainButtons;

    //二级菜单面板
    public GameObject[] childButtonsPanel;

    //二级菜单项,按组分
    [HideInInspector]
    public List childButtonGroup = new List();

    //当前选中主菜单索引
    private int mainButtonIndex = 0;

    //当前选中子菜单索引
    private int childButtonIndex = 0;

    //子菜单是否打开
    private bool childPanelExpanded = false;

    void Start () {
        //主菜单事件
        for(int i = 0; i < mainButtons.Length; i++)
        {
            mainButtons[i].onClick.AddListener(OnMainButtonClick);
        }

        if (mainButtons.Length > 0)
        {
            mainButtons[mainButtonIndex].Select();
        }

        //子菜单按钮分组
        for (int i = 0; i < childButtonsPanel.Length; i++)
        {
            List btns = new List();
            for (int j = 0; j < childButtonsPanel[i].transform.childCount; j++)
            {
                Transform tr = childButtonsPanel[i].transform.GetChild(j);
                Button btn = tr.GetComponent();
                btn.onClick.AddListener(OnChildButtonClick);
                btns.Add(btn);
            }

            childButtonGroup.Add(btns);
        }
    }	

	void Update () {

        //点击,按下数字'1'
        if (UnityEngine.Input.GetKeyDown(KeyCode.Alpha1))//getReal3dInputs.WandButtonDown
        {
            if (!childPanelExpanded)
            {
                mainButtons[mainButtonIndex].onClick.Invoke();
            }
            else
            {
                childButtonGroup[mainButtonIndex][childButtonIndex].onClick.Invoke();
            }
                
        }

        //切换,按下数字'2'
        if (UnityEngine.Input.GetKeyDown(KeyCode.Alpha2))//getReal3dInputs.ChangeWandButtonDown
        {
            if(!childPanelExpanded)
            {
                mainButtonIndex++;
                if (mainButtonIndex > mainButtons.Length - 1)
                {
                    mainButtonIndex = 0;
                }
                mainButtons[mainButtonIndex].Select();
            }
            else
            {
                childButtonIndex++;
                childButtonIndex = childButtonIndex > childButtonGroup[mainButtonIndex].Count - 1 ? 0 : childButtonIndex;
                childButtonGroup[mainButtonIndex][childButtonIndex].Select();        
            }            
        }

        //返回,按下数字'3'
        if (UnityEngine.Input.GetKeyDown(KeyCode.Alpha3))//getReal3dInputs.ResetButtonDown
        {
            CloseChildPanel();
            mainButtons[mainButtonIndex].Select();
        }

        //刷新当前选中项
        if (childPanelExpanded)
        {
            childButtonGroup[mainButtonIndex][childButtonIndex].Select();
        }
    }

    //主菜单点击事件函数
    void OnMainButtonClick()
    {
        Debug.Log(string.Format("点击了主按钮 {0}", mainButtonIndex));
        childButtonIndex = 0;
        DisplayChildPanel(mainButtonIndex);

        childButtonGroup[mainButtonIndex][childButtonIndex].Select();
    }

    //子菜单点击事件函数
    void OnChildButtonClick()
    {
        Debug.Log(string.Format("点击了子按钮 {0}", childButtonIndex));
    }

    //显示某个子菜单面板
    void DisplayChildPanel(int index)
    {
        childPanelExpanded = true;
        for (int i = 0; i  < childButtonsPanel.Length; i++)
        {
            if(i == index)
            {
                if (!childButtonsPanel[i].activeSelf)
                {
                    childButtonsPanel[i].SetActive(true);
                    childButtonGroup[index][childButtonIndex].Select();
                }
                    
            }
            else
            {
                if (childButtonsPanel[i].activeSelf)
                    childButtonsPanel[i].SetActive(false);
            }            
        }

    }

    //显示关闭所有菜单面板
    void CloseChildPanel()
    {
        childPanelExpanded = false;
        for (int i = 0; i < childButtonsPanel.Length; i++)
        {
            if (childButtonsPanel[i].activeSelf)
                childButtonsPanel[i].SetActive(false);
        }
    }
}

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0398s