[Unity][优化]Esc按键快速隐藏背包UI

版权声明:本博客一本正经胡说八道,文章内容不能作参考。本文为博主原创文章,未经博主允许不得转载。该博客所用图片资源均作学习分享用途,仅供参考,请勿用于商业行为。传播者自负。 如果本博客所写文章侵犯到您的权益,请主动联系留言,我们将及时删除相关内容。 https://blog.csdn.net/BuladeMian/article/details/89170701

有的时候,打开的UI过多,需要快捷的隐藏 背包UI的操作。



    /// <summary>
    /// esc按键,默认esc。
    /// </summary>
    private KeyCode esc = KeyCode.Escape;

    /// <summary>
    /// 用于存放 ESC 按键,列表
    /// </summary>
    private List<Transform> esc_list = new List<Transform>();


    /// <summary>
    /// 背包按键 的背包UI
    /// </summary>
    [SerializeField]
    private InventoryUI inventoryUI;



    // Update is called once per frame
    void Update () {


        if (Input.GetButtonDown("Inventory")//KeyCode.I
            && inventoryUI!=null)
        {
            inventoryUI.UpdateUI();
            esc_list.Add(inventoryUI.inventoryUI.transform);
        }



        if (Input.GetKeyDown(esc)//当按下 ESCAPE 按键,快速隐藏 UI
            && esc_list.Count >0)
        {
            #region esc按键函数
            esc_list[esc_list.Count - 1].gameObject.SetActive(false);
            esc_list.Remove(esc_list[esc_list.Count - 1]);
            #endregion
        }


    }

隐藏 UI的几种方式

UIPlane.gameObject.SetActive(!UIPlane.activeSelf);

UIPlane.transform.localscale = new Vectore(0,0,0);

猜你喜欢

转载自blog.csdn.net/BuladeMian/article/details/89170701