Unity Pico手柄触摸圆盘滑动功能

Pico 开发SDK 中实现了对手柄触摸圆盘中上滑,下滑,左滑,右滑的判定,只是将这些方法写进了拓展的API中,并且默认是不被启用的。所以我们通过查看源码便可轻松调用里面的方法属性。
查看SDK中Pvr_ControllerManager.cs中第304行开始。

我们发现在拓展的API中实现了长按和滑动的功能,通过ExtendedAPI控制是否启用。我们可以将ExtendedAPI设置为True,调用里面的布尔属性:

  TouchPadKey.slideright   //向右滑动
    TouchPadKey.slideleft //向左滑动
    TouchPadKey.slidedown //向下滑动
    TouchPadKey.slideup //向上滑动
 #region extended api
            //打开扩展API后,提供长按和滑动功能
            if (ExtendedAPI)
            {
                //slip
                if (TouchPadPosition.x > 0 || TouchPadPosition.y > 0)
                {
                    if (!touchClock)
                    {
                        touchXBegin = TouchPadPosition.x;
                        touchYBegin = TouchPadPosition.y;
                        touchClock = true;
                    }
                    touchXEnd = TouchPadPosition.x;
                    touchYEnd = TouchPadPosition.y;
                }
                else
                {
                    if (touchXEnd > touchXBegin)
                    {
                        if (touchYEnd > touchYBegin)
                        {
                            if (touchXEnd - touchXBegin > slipNum && ((touchXEnd - touchXBegin) > (touchYEnd - touchYBegin)))
                            {
                                //slide up
                                TouchPadKey.slideup = true;
                            }
                            if (touchYEnd - touchYBegin > slipNum && ((touchYEnd - touchYBegin) > (touchXEnd - touchXBegin)))
                            {
                                //slide right
                                TouchPadKey.slideright = true;
                            }
                        }
                        else if (touchYEnd < touchYBegin)
                        {
                            if (touchXEnd - touchXBegin > slipNum && ((touchXEnd - touchXBegin) > (touchYBegin - touchYEnd)))
                            {
                                //slide up
                                TouchPadKey.slideup = true;
                            }
                            if (touchYBegin - touchYEnd > slipNum && ((touchYBegin - touchYEnd) > (touchXEnd - touchXBegin)))
                            {
                                //slide left
                                TouchPadKey.slideleft = true;
                            }
                        }
                    }
                    else if (touchXEnd < touchXBegin)
                    {
                        if (touchYEnd > touchYBegin)
                        {
                            if (touchXBegin - touchXEnd > slipNum && ((touchXBegin - touchXEnd) > (touchYEnd - touchYBegin)))
                            {
                                //slide down
                                TouchPadKey.slidedown = true;
                            }
                            if (touchYEnd - touchYBegin > slipNum && ((touchYEnd - touchYBegin) > (touchXBegin - touchXEnd)))
                            {
                                //slide right
                                TouchPadKey.slideright = true;
                            }
                        }
                        else if (touchYEnd < touchYBegin)
                        {
                            if (touchXBegin - touchXEnd > slipNum && ((touchXBegin - touchXEnd) > (touchYBegin - touchYEnd)))
                            {
                                //slide down 
                                TouchPadKey.slidedown = true;
                            }
                            if (touchYBegin - touchYEnd > slipNum && ((touchYBegin - touchYEnd) > (touchXBegin - touchXEnd)))
                            {
                                //slide left
                                TouchPadKey.slideleft = true;
                            }
                        }
                    }
                    else
                    {
                        TouchPadKey.slideright = false;
                        TouchPadKey.slideleft = false;
                        TouchPadKey.slidedown = false;
                        TouchPadKey.slideup = false;
                    }
                    touchXBegin = 0;
                    touchXEnd = 0;
                    touchYBegin = 0;
                    touchYEnd = 0;
                    touchClock = false;
                }

                //longpress
                if (HomeKey.state)
                {
                    HomeKey.timecount += Time.deltaTime;
                    if (HomeKey.timecount >= longPressTime && !HomeKey.longPressedClock)
                    {
                        HomeKey.longPressed = true;
                        HomeKey.longPressedClock = true;
                        longPressclock = true;
                    }
                    else
                    {
                        HomeKey.longPressed = false;
                    }
                }
                else
                {
                    HomeKey.longPressedClock = false;
                    HomeKey.timecount = 0;
                    HomeKey.longPressed = false;
                }
                if (APPKey.state)
                {
                    APPKey.timecount += Time.deltaTime;
                    if (APPKey.timecount >= longPressTime && !APPKey.longPressedClock)
                    {
                        APPKey.longPressed = true;
                        APPKey.longPressedClock = true;
                        longPressclock = true;
                    }
                    else
                    {
                        APPKey.longPressed = false;
                    }
                }
                else
                {
                    APPKey.longPressedClock = false;
                    APPKey.timecount = 0;
                    APPKey.longPressed = false;
                }
                if (TouchPadKey.state)
                {
                    TouchPadKey.timecount += Time.deltaTime;
                    if (TouchPadKey.timecount >= longPressTime && !TouchPadKey.longPressedClock)
                    {
                        TouchPadKey.longPressed = true;
                        TouchPadKey.longPressedClock = true;
                        longPressclock = true;
                    }
                    else
                    {
                        TouchPadKey.longPressed = false;
                    }
                }
                else
                {
                    TouchPadKey.longPressedClock = false;
                    TouchPadKey.timecount = 0;
                    TouchPadKey.longPressed = false;
                }
                if (VolumeUpKey.state)
                {
                    VolumeUpKey.timecount += Time.deltaTime;
                    if (VolumeUpKey.timecount >= longPressTime && !VolumeUpKey.longPressedClock)
                    {
                        VolumeUpKey.longPressed = true;
                        VolumeUpKey.longPressedClock = true;
                        longPressclock = true;
                    }
                    else
                    {
                        VolumeUpKey.longPressed = false;
                    }
                }
                else
                {
                    VolumeUpKey.longPressedClock = false;
                    VolumeUpKey.timecount = 0;
                    VolumeUpKey.longPressed = false;
                }
                if (VolumeDownKey.state)
                {
                    VolumeDownKey.timecount += Time.deltaTime;
                    if (VolumeDownKey.timecount >= longPressTime && !VolumeDownKey.longPressedClock)
                    {
                        VolumeDownKey.longPressed = true;
                        VolumeDownKey.longPressedClock = true;
                        longPressclock = true;
                    }
                    else
                    {
                        VolumeDownKey.longPressed = false;
                    }
                }
                else
                {
                    VolumeDownKey.longPressedClock = false;
                    VolumeDownKey.timecount = 0;
                    VolumeDownKey.longPressed = false;
                }

            }
            #endregion

方法使用示例

 void Update()
    {
        Debug.Log("x:"+TouchPadPosition.x);
        Debug.Log("y:"+TouchPadPosition.y);
        ShowText_x.text = "x:" + TouchPadPosition.x.ToString();
        ShowText_y.text = "y:" + TouchPadPosition.y.ToString();     
        
        
         
        if ( TouchPadKey.slideup )
        {           
            Debug.Log("向上滑");        
        }    
        if ( TouchPadKey.slidedown )
        {          
            Debug.Log("向下滑");         
        }
        if ( TouchPadKey.slideleft )
        {           
            Debug.Log("向左滑");        
        }
        if ( TouchPadKey.slideright )
        {         
            Debug.Log("向右滑");   
        }
        if ( TouchPadPosition.x != 0 || TouchPadPosition.y != 0 )
        {
           //to do something 
        }
    }

更多的功能可以摸索实现。

你可能感兴趣的:(Unity开发)