美文网首页Unity技术VR/AR分享
unity SteamVR2.0 激光与UGUI交互(一)

unity SteamVR2.0 激光与UGUI交互(一)

作者: WOTTOW | 来源:发表于2019-06-23 14:51 被阅读73次
    效果图: UGUI交互E.gif

    发射激光:
    在StemVR中自带有SteamVR_LaserPointer的脚本,是用来控制激光。


    SteamVR_LaserPointer.png
    开启激光:
    SteamVrLaserPointer.GetComponent<SteamVR_LaserPointer>().enabled = true; 
    在开启激光时,会创建一个cube,用来模拟激光
    关闭激光:
       SteamVrLaserPointer.GetComponent<SteamVR_LaserPointer>().enabled = false; //射线关闭
            SteamVrLaserPointer.GetComponent<SteamVR_LaserPointer>().pointer.transform.localScale = new Vector3(0, 0, 0);
    

    UI互交:
    使用UnityEvent的目的是,绑定事件可视化


    image.png
      private PointerEventArgs pointerEventArgs;
        public SteamVR_LaserPointer SteamVrLaserPointer;
        public UnityEvent mOnEnter = null;
        public UnityEvent mOnClick = null;
        public UnityEvent mOnUp = null;
    
        void OnEnable()
        {
            SteamVrLaserPointer.PointerClick += SteamVrLaserPointer_PointerClick;
            SteamVrLaserPointer.PointerIn += SteamVrLaserPointer_PointerIn;
            SteamVrLaserPointer.PointerOut += SteamVrLaserPointer_PointerOut;
        }
    
        void OnDestroy()
        {
            SteamVrLaserPointer.PointerClick -= SteamVrLaserPointer_PointerClick;
            SteamVrLaserPointer.PointerIn -= SteamVrLaserPointer_PointerIn;
            SteamVrLaserPointer.PointerOut -= SteamVrLaserPointer_PointerOut;
        }
    
        private void SteamVrLaserPointer_PointerOut(object sender, PointerEventArgs e)
        {
            if (e.target.gameObject == this.gameObject)
            {
                if (mOnUp != null) mOnUp.Invoke();
            }
        }
    
        private void SteamVrLaserPointer_PointerIn(object sender, PointerEventArgs e)
        {
            if (e.target.gameObject == this.gameObject)
            {
                if (mOnEnter != null) mOnEnter.Invoke();
            }
        }
         
        private void SteamVrLaserPointer_PointerClick(object sender, PointerEventArgs e)
        {
            if (e.target.gameObject == this.gameObject)
            {
                Debug.Log(e);
                if (mOnClick != null)
                {
                    mOnClick.Invoke();
                }
            }
        }
    

    当获取手柄的GrabGrip键,按下就执行互交

    相关文章

      网友评论

        本文标题:unity SteamVR2.0 激光与UGUI交互(一)

        本文链接:https://www.haomeiwen.com/subject/nabiqctx.html