关于Unity中UGUI做菜单栏弹出

这里我用的的英文位移用了Translate和Vector3.Lerp
使用鼠标位置的判断结合菜单栏的位置状态来进行开关控制

这里面有一个坑就是锚点坐标,UI界面显示的都是锚点坐标,跟UI的世界坐标和当地坐标没有半毛钱的关系(开始的时候我纠结到坐标位移问题上了)

其实这里用动画的话还是比较方便的,但是我这里直接用代码写了位移,做个笔记以后再遇见UI位移问题就不会出错了

下面直接上代码(这段代码封装到一个函数里面直接放到更新函数中就可以啦):

 if (PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition.x >= 104.5f)//这个数值是UI的x位置
            UIIsMovetr = true;
  if (PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition.x <= -100f)
            UIIsMovefa = true;

// UI按钮界面的显示时

if (Input.mousePosition.x > 220 && UIIsMovetr)
   {
            Debug.Log("UI按钮界面的显示时");
            PanelScrollViewUI.transform.GetComponent<RectTransform>().Translate(Vector3.Lerp(PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition, PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition=new Vector2(-100f,0), UIMoveSpeed*Time.deltaTime));
            if (PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition.x<=-100f)
            {
                UIIsMovetr = false;
                PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(-100f, 0);
            }
   }

// UI按钮界面的隐藏时

     if (Input.mousePosition.x > 0 && Input.mousePosition.x < 30 && UIIsMovefa)
        {
            Debug.Log("UI按钮界面的隐藏时");
            PanelScrollViewUI.transform.GetComponent<RectTransform>().Translate(Vector3.Lerp(PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition, PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition=new Vector2(104.5f,0), UIMoveSpeed * Time.deltaTime));
            if (PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition.x >= 104.5f)
            {
                UIIsMovefa = false;
                PanelScrollViewUI.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(104.5f, 0);
            }
        }
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值