Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        #region AndroidData
#if UNITY_ANDROID
        if (controllerlink.isConnect)
        {
            //手柄转动四元数
            lark2state = controllerlink.GetHBSensorState();
            JsonData Jstate = JsonMapper.ToObject(lark2state);
            lark2w = Convert.ToSingle(Jstate[0].ToString());
            lark2x = Convert.ToSingle(Jstate[1].ToString());
            lark2y = Convert.ToSingle(Jstate[2].ToString());
            lark2z = Convert.ToSingle(Jstate[3].ToString());
            Controller.ControllerQua = new Quaternion(lark2x, lark2y, lark2z, lark2w);

            //lark2手柄键值TouchpadX,TouchpadY,HomeKeyPress,AppKeyPress,ClickKeyPress,VolumeUpKeyPress,VolumeDownKeyPress,BatteryLevel
            lark2key = controllerlink.javaHummingbirdClass.CallStatic <string>("getHBKeyEvent");
            if (enableKeyEvent)
            {
                JsonData JKey = JsonMapper.ToObject(lark2key);
                if (Convert.ToInt16(JKey[0].ToString()) > 0 || Convert.ToInt16(JKey[1].ToString()) > 0)
                {
                    if (Convert.ToInt16(JKey[0].ToString()) == 0)
                    {
                        TouchPadPosition.x = 1;
                    }
                    if (Convert.ToInt16(JKey[1].ToString()) == 0)
                    {
                        TouchPadPosition.y = 1;
                    }
                    TouchPadPosition.x = Convert.ToInt16(JKey[0].ToString());
                    TouchPadPosition.y = Convert.ToInt16(JKey[1].ToString());
                }
                else
                {
                    touchNum++;
                    if (touchNum >= 1)
                    {
                        TouchPadPosition.x = 0;
                        TouchPadPosition.y = 0;
                        touchNum           = 0;
                    }
                }
                Controller.BatteryLevel = Convert.ToInt16(JKey[7].ToString());

                #region base api
                //键值状态
                //Home Key
                if (Convert.ToInt16(JKey[2].ToString()) == 1)
                {
                    if (!HomeKey.state)
                    {
                        HomeKey.pressedDown = true;
                        longPressclock      = false;
                    }
                    else
                    {
                        HomeKey.pressedDown = false;
                    }
                    HomeKey.state = true;
                }
                else
                {
                    if (HomeKey.state)
                    {
                        HomeKey.pressedUp = true;
                    }
                    else
                    {
                        HomeKey.pressedUp = false;
                    }
                    HomeKey.state       = false;
                    HomeKey.pressedDown = false;
                }
                //APP Key
                if (Convert.ToInt16(JKey[3].ToString()) == 1)
                {
                    if (!APPKey.state)
                    {
                        APPKey.pressedDown = true;
                        longPressclock     = false;
                    }
                    else
                    {
                        APPKey.pressedDown = false;
                    }
                    APPKey.state = true;
                }
                else
                {
                    if (APPKey.state)
                    {
                        APPKey.pressedUp = true;
                    }
                    else
                    {
                        APPKey.pressedUp = false;
                    }
                    APPKey.state       = false;
                    APPKey.pressedDown = false;
                }
                //Touchpad Key
                if (Convert.ToInt16(JKey[4].ToString()) == 1)
                {
                    if (!TouchPadKey.state)
                    {
                        TouchPadKey.pressedDown = true;
                        longPressclock          = false;
                    }
                    else
                    {
                        TouchPadKey.pressedDown = false;
                    }
                    TouchPadKey.state = true;
                }
                else
                {
                    if (TouchPadKey.state)
                    {
                        TouchPadKey.pressedUp = true;
                    }
                    else
                    {
                        TouchPadKey.pressedUp = false;
                    }
                    TouchPadKey.state       = false;
                    TouchPadKey.pressedDown = false;
                }
                //VolumeUP Key
                if (Convert.ToInt16(JKey[5].ToString()) == 1)
                {
                    if (!VolumeUpKey.state)
                    {
                        VolumeUpKey.pressedDown = true;
                        longPressclock          = false;
                    }
                    else
                    {
                        VolumeUpKey.pressedDown = false;
                    }
                    VolumeUpKey.state = true;
                }
                else
                {
                    if (VolumeUpKey.state)
                    {
                        VolumeUpKey.pressedUp = true;
                    }
                    else
                    {
                        VolumeUpKey.pressedUp = false;
                    }
                    VolumeUpKey.state       = false;
                    VolumeUpKey.pressedDown = false;
                }
                //VolumeDown Key
                if (Convert.ToInt16(JKey[6].ToString()) == 1)
                {
                    if (!VolumeDownKey.state)
                    {
                        VolumeDownKey.pressedDown = true;
                        longPressclock            = false;
                    }
                    else
                    {
                        VolumeDownKey.pressedDown = false;
                    }
                    VolumeDownKey.state = true;
                }
                else
                {
                    if (VolumeDownKey.state)
                    {
                        VolumeDownKey.pressedUp = true;
                    }
                    else
                    {
                        VolumeDownKey.pressedUp = false;
                    }
                    VolumeDownKey.state       = false;
                    VolumeDownKey.pressedDown = false;
                }
            }

            #endregion

            #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


            if (controllerlink.notPhone)
            {
                if (!longPressclock && Controller.UPvr_GetKeyUp(Pvr_KeyCode.HOME) && !stopConnect)
                {
                    controllerlink.RebackToLauncher();
                }
                if (!longPressclock && Controller.UPvr_GetKeyUp(Pvr_KeyCode.VOLUMEUP))
                {
                    controllerlink.TurnUpVolume();
                }
                if (!longPressclock && Controller.UPvr_GetKeyUp(Pvr_KeyCode.VOLUMEDOWN))
                {
                    controllerlink.TurnDownVolume();
                }
                if (!Controller.UPvr_GetKey(Pvr_KeyCode.VOLUMEUP) && !Controller.UPvr_GetKey(Pvr_KeyCode.VOLUMEDOWN))
                {
                    cTime = 1.0f;
                }
                if (Controller.UPvr_GetKey(Pvr_KeyCode.VOLUMEUP))
                {
                    cTime -= Time.deltaTime;
                    if (cTime <= 0)
                    {
                        cTime = 0.2f;
                        controllerlink.TurnUpVolume();
                    }
                }
                if (Controller.UPvr_GetKey(Pvr_KeyCode.VOLUMEDOWN))
                {
                    cTime -= Time.deltaTime;
                    if (cTime <= 0)
                    {
                        cTime = 0.2f;
                        controllerlink.TurnDownVolume();
                    }
                }
            }

            if (Controller.UPvr_GetKey(Pvr_KeyCode.HOME) && Controller.UPvr_GetKey(Pvr_KeyCode.VOLUMEDOWN) && !stopConnect)
            {
                disConnectTime += Time.deltaTime;
                if (disConnectTime > 1.0)
                {
                    DisConnectBLE();
                    controllerlink.hummingBirdMac = "";
                    stopConnect = true;
                }
            }
        }
#endif
        #endregion
        #region IOSData
#if IOS_DEVICE
        Controller.PVR_GetLark2SensorMessage(ref lark2x, ref lark2y, ref lark2z, ref lark2w);
        Controller.ControllerQua = new Quaternion(lark2x, lark2y, lark2z, lark2w);

        Controller.PVR_GetLark2KeyValueMessage(ref lark2touchx, ref lark2touchy, ref lark2home, ref lark2app, ref lark2click, ref lark2volup, ref lark2voldown, ref lark2power);
        if (lark2touchx > 0 || lark2touchy > 0)
        {
            if (lark2touchx == 0)
            {
                TouchPadPosition.x = 1;
            }
            if (lark2touchy == 0)
            {
                TouchPadPosition.y = 1;
            }
            TouchPadPosition.x = lark2touchx;
            TouchPadPosition.y = lark2touchy;
        }
        else
        {
            touchNum++;
            if (touchNum >= 1)
            {
                TouchPadPosition.x = 0;
                TouchPadPosition.y = 0;
                touchNum           = 0;
            }
        }
        Controller.BatteryLevel = lark2power;
        #region base api
        //键值状态
        //Home Key
        if (lark2home == 1)
        {
            if (!HomeKey.state)
            {
                HomeKey.pressedDown = true;
                longPressclock      = false;
            }
            else
            {
                HomeKey.pressedDown = false;
            }
            HomeKey.state = true;
        }
        else
        {
            if (HomeKey.state)
            {
                HomeKey.pressedUp = true;
            }
            else
            {
                HomeKey.pressedUp = false;
            }
            HomeKey.state       = false;
            HomeKey.pressedDown = false;
        }
        //APP Key
        if (lark2app == 1)
        {
            if (!APPKey.state)
            {
                APPKey.pressedDown = true;
                longPressclock     = false;
            }
            else
            {
                APPKey.pressedDown = false;
            }
            APPKey.state = true;
        }
        else
        {
            if (APPKey.state)
            {
                APPKey.pressedUp = true;
            }
            else
            {
                APPKey.pressedUp = false;
            }
            APPKey.state       = false;
            APPKey.pressedDown = false;
        }
        //Touchpad Key
        if (lark2click == 1)
        {
            if (!TouchPadKey.state)
            {
                TouchPadKey.pressedDown = true;
                longPressclock          = false;
            }
            else
            {
                TouchPadKey.pressedDown = false;
            }
            TouchPadKey.state = true;
        }
        else
        {
            if (TouchPadKey.state)
            {
                TouchPadKey.pressedUp = true;
            }
            else
            {
                TouchPadKey.pressedUp = false;
            }
            TouchPadKey.state       = false;
            TouchPadKey.pressedDown = false;
        }
        //VolumeUP Key
        if (lark2volup == 1)
        {
            if (!VolumeUpKey.state)
            {
                VolumeUpKey.pressedDown = true;
                longPressclock          = false;
            }
            else
            {
                VolumeUpKey.pressedDown = false;
            }
            VolumeUpKey.state = true;
        }
        else
        {
            if (VolumeUpKey.state)
            {
                VolumeUpKey.pressedUp = true;
            }
            else
            {
                VolumeUpKey.pressedUp = false;
            }
            VolumeUpKey.state       = false;
            VolumeUpKey.pressedDown = false;
        }
        //VolumeDown Key
        if (lark2voldown == 1)
        {
            if (!VolumeDownKey.state)
            {
                VolumeDownKey.pressedDown = true;
                longPressclock            = false;
            }
            else
            {
                VolumeDownKey.pressedDown = false;
            }
            VolumeDownKey.state = true;
        }
        else
        {
            if (VolumeDownKey.state)
            {
                VolumeDownKey.pressedUp = true;
            }
            else
            {
                VolumeDownKey.pressedUp = false;
            }
            VolumeDownKey.state       = false;
            VolumeDownKey.pressedDown = false;
        }


        #endregion

        #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
#endif
        #endregion

        if (Controller.UPvr_GetKeyLongPressed(Pvr_KeyCode.HOME))
        {
            Pvr_UnitySDKManager.pvr_UnitySDKSensor.ResetUnitySDKSensor();
            ResetController();
        }
    }