ARKit Session Paused and Not Resuming

前端 未结 4 1362
小蘑菇
小蘑菇 2020-12-06 12:04

In my ARKit app I am presenting a modal window. When I close the modal and go back to the ARSCNView then I find out that the session is paused due to this code:

<         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 12:48

    I get that you have chosen an answer, and that answer is what is recommended by apple, you can restart the AR Session. You can't unpause/resume the Session though, because the device stops it's tracking once you're out of your controller presenting the ARSceneView and will stop keeping track of the position of your device relative to the objects you've placed in the scene.

    Anyway, I've managed to restart the session essentially by destroying all aspects of my session and rebuilding them them when my view reappears, or through a button press.

    I'll post some sample code here. It's in Objective-C cause my project was written in that, but it might help future people with the same question.

    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated]
        [self setupScene];
        [self setupSession];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [self destroySession];
        [self destroyScene];
    }
    
    - (void)setupScene {
    
    // Setup the ARSCNViewDelegate - this gives us callbacks to handle new
    // geometry creation
        self.sceneView.delegate = self;
    
    // A dictionary of all the current planes being rendered in the scene
        self.planes = [NSMutableDictionary new];
    
    // Contains a list of all the boxes rendered in the scene
        self.boxes = [NSMutableArray new];
    
    // Show statistics such as fps and timing information
        self.sceneView.showsStatistics = YES;
        self.sceneView.autoenablesDefaultLighting = YES;
    
        SCNScene *scene = [SCNScene new];
    
        [self.sceneView setScene:scene];
    
        self.sceneView.scene.physicsWorld.contactDelegate = self;   
    }
    
    - (void)setupSession {
    
        // Create a session configuration
        ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];
        //ARWorldTrackingSessionConfiguration *configuration = [ARWorldTrackingSessionConfiguration new]; This has been deprecated in favor of the previous line in XCode 9 beta 5. 
    
        // Specify that we do want to track horizontal planes. Setting this will cause the ARSCNViewDelegate
        // methods to be called when scenes are detected
        //configuration.planeDetection = ARPlaneDetectionHorizontal;
    
        // Run the view's session
        [self.sceneView.session runWithConfiguration:configuration options:ARSessionRunOptionResetTracking];
    }
    
    
    -(void)destroyScene {
        bottomPlane = nil;
        [self.sceneView setScene:nil];
        [self.sceneView setDebugOptions:nil];
        self.boxes = nil;
        self.planes = nil;
        self.sceneView.delegate = nil;  
    }
    
    -(void)destroySession {
        [self.sceneView.session pause];
        [self.sceneView setSession:nil];
    }
    

    These destroy methods are used when the view disappears. I am also restarting the AR Session on a button press, but it is not through these methods. It is as follows:

    -(void)resetPressed{
        NSLog(@"Reset Pressed");
        [_sceneView.session pause];
    
    
        SCNScene *scene = [[SCNScene alloc] init];
        [_sceneView setScene:scene];
        [_sceneView.scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode * _Nonnull child, BOOL * _Nonnull stop) {
            [child removeFromParentNode];
        }];
    
        ARWorldTrackingConfiguration *configuration = [[ARWorldTrackingSessionConfiguration ARWorldTrackingConfiguration] init];
        [_sceneView.session runWithConfiguration:configuration options:ARSessionRunOptionResetTracking | ARSessionRunOptionRemoveExistingAnchors];
    }
    

    Hope it helps.

提交回复
热议问题