美文网首页
unity 陀螺仪判断手机方向

unity 陀螺仪判断手机方向

作者: 毛毛_1e13 | 来源:发表于2018-09-21 15:17 被阅读0次

    首先说一下为什么用陀螺仪,很多时候当ios设备系统级别锁定方向后,我们使用ios系统的设备方向方法将不能其作用。 ios系统代码中为一下代码:

    //下面代码只有在设备系统不锁定屏幕旋转是Natifications 方法才起作用。
    - (void) addDeviceRotateObserver {
      [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
      [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(rotateViews:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    }
    
    - (void) rotateViews:(NSObject *)sender {
    
    UIDevice* device = [sender valueForKey:@"object"];
    NSLog(@"%d",device.orientation);
    
    //   switch (device.orientation) {
    //       case UIDeviceOrientationPortrait: {
    //            NSLog(@"P");
    //            break;
    //        }
    //        case UIDeviceOrientationLandscapeLeft: {
    //            NSLog(@"LL");
    //            break;
    //        }
    //        case UIDeviceOrientationLandscapeRight: {
    //            NSLog(@"LR");
    //            break;
    //        }
    //        default:
    //            break;
    //    }
    
    switch (device.orientation) {
        case UIDeviceOrientationUnknown: {
            // do something
            break;
        }
        case UIDeviceOrientationPortrait: {
            // do something
            break;
        }
        case UIDeviceOrientationPortraitUpsideDown: {
            // do something
            break;
        }
        case UIDeviceOrientationLandscapeLeft: {
            // do something
            break;
        }
        case UIDeviceOrientationLandscapeRight: {
            // do something
            break;
        }
        case UIDeviceOrientationFaceUp: {
            // do something
            break;
        }
        case UIDeviceOrientationFaceDown: {
            // do something
            break;
        }
    }
    
    }
    

    以下为陀螺仪代码,直接在unity中使用

    void CheckOrientation()
    {
        if (Mathf.Abs(Input.gyro.gravity.z) <= 0.9f)
        {
            
            if (Mathf.Abs(Input.gyro.gravity.x) > Mathf.Abs(Input.gyro.gravity.y))
            {
                if (Input.gyro.gravity.x > 0f)
                {
                    unknown = DeviceOrientation.LandscapeRight;
                    
    Rotation.z);
            
                    Debug.Log("DeviceOrientation111=="+Input.deviceOrientation);
                    
                }
                else
                {
                    unknown = DeviceOrientation.LandscapeLeft;
                    Rotation.z);
            
                    Debug.Log("DeviceOrientation222=="+Input.deviceOrientation);
                    
                }
            }
            else if (Input.gyro.gravity.y > 0f)
            {
                unknown =  DeviceOrientation.PortraitUpsideDown;
                
                Debug.Log("DeviceOrientation4444=="+Input.deviceOrientation);
                
                
            }
            else
            {
                unknown = DeviceOrientation.Portrait;
                
                Debug.Log("DeviceOrientation3333=="+Input.deviceOrientation);
                
            }
            currentDeviceOrientation = unknown;
        }
    }
    

    }

    相关文章

      网友评论

          本文标题:unity 陀螺仪判断手机方向

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