美文网首页Unity技术VR/AR分享
unity SteamVR2.0 手指与圆盘UI互交(二)

unity SteamVR2.0 手指与圆盘UI互交(二)

作者: WOTTOW | 来源:发表于2019-06-24 17:21 被阅读0次
效果图: UGUI交互A.gif UGUI交互B.gif UGUI交互C.png

思想:
1.获取手指在圆盘的位置,换算成角度。把圆盘分为两个区。左按钮和右按钮。
2.在SteamVR Input,打开网页在圆盘添加触摸板,并添加相应的动作(Action)
3.通过圆盘触摸和圆盘点击实现事件的执行、变色

 public UnityEvent Menter = new UnityEvent();

    [Header("左边按钮")]
    public UnityEvent LeftOnClick = new UnityEvent();

    [Header("右边按钮")]
    public UnityEvent RightOnClick = new UnityEvent();

    public UnityEvent Mexit = new UnityEvent();

    public Image LeftImage;
    public Image RightImage;

    //颜色设置
    private Color LeftImageStartColor;
    private Color RightImageStartColor;
    private Color ClickImageColor = Color.gray;

    private bool Limit = true;

    private void Start()
    {
        LeftImageStartColor = LeftImage.color;
        RightImageStartColor = RightImage.color;

        Debug.Log(RightImageStartColor);
    }

    private void Update()
    {
        TrackPadTwoPartition();
    }

    private void TrackPadTwoPartition()
    {
        Vector3 currentPos = SteamVR_Actions._default.TrackPadPos.lastAxis;

        float Angle = Mathf.Acos(Vector2.Dot(currentPos, Vector2.up)) * Mathf.Rad2Deg;
        Angle = currentPos.x < 0 ? -Angle : Angle;

        if (SteamVR_Actions._default.TrackPadTouch.GetState(SteamVR_Input_Sources.LeftHand) && Limit)
        {
            if (Angle > 0)
            {
                TouchColor(RightImage, Color.yellow);
                TouchColor(LeftImage, LeftImageStartColor);
            }
            else if (Angle < -1)
            {
                TouchColor(LeftImage, Color.green);
                TouchColor(RightImage, RightImageStartColor);
            }
        }
        if (!SteamVR_Actions._default.TrackPadTouch.GetState(SteamVR_Input_Sources.LeftHand))
        {
            NoClick();
        }

        if (SteamVR_Actions._default.TrackPadClick.GetStateDown(SteamVR_Input_Sources.LeftHand))
        {
            if (Angle > 0)
            {
                RightOnClick.Invoke();
            }
            else if (Angle < -1)
            {
                LeftOnClick.Invoke();
            }
            Limit = false;
        }

        if (SteamVR_Actions._default.TrackPadClick.GetStateUp(SteamVR_Input_Sources.LeftHand))
        {
            NoClick();
            Limit = true;
        }
    }

    public void NoClick()
    {
        ChangeColor(LeftImage, LeftImageStartColor);
        ChangeColor(RightImage, RightImageStartColor);
    }

    public void LeftClick()
    {
        Debug.Log("左边点击");
        ChangeColor(LeftImage, ClickImageColor);
    }

    public void RighttClick()
    {
        Debug.Log("右边点击");
        ChangeColor(RightImage, ClickImageColor);
    }

    public void ChangeColor(Image image, Color color)
    {
        image.color = color;
    }

    public void TouchColor(Image image, Color color)
    {
        image.color = color;
    }

相关文章

网友评论

    本文标题:unity SteamVR2.0 手指与圆盘UI互交(二)

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