【问题标题】:iOS, RealityKit. How would you check if the Entity is appended to EntityAnchoriOS,RealityKit。您将如何检查实体是否附加到 EntityAnchor
【发布时间】:2021-11-23 04:34:11
【问题描述】:

我想在 Entity 不仅已加载回合也已成功附加到 AnchorEntity

时运行一些函数

现在我的实体正在异步加载。

 func addModel(to planeAnchor: AnchorEntity) {
        Entity.loadAsync(named: "SomeUSDZModel")
            .sink { completion in
                switch completion {
                case .finished:
                    print("Ok")
                case .failure(let error):
                    print(error.localizedDescription)
                }
            } receiveValue: { model in
                // Entity should be added before the animation is started.
                planeAnchor.addChild(model)
                if let walkingAnimation = model.availableAnimations.first {
                    model.playAnimation(walkingAnimation.repeat(duration: .infinity),
                                        transitionDuration: 1.25,
                                        blendLayerOffset: 0,
                                        separateAnimatedValue: false,
                                        startsPaused: false)

                }
                self.model = model
                
            }
            .store(in: &subscriptions)
            doSomething()
    }

方法 doSomething() 过早触发。我想通过一些条件检查来触发它。

【问题讨论】:

    标签: swift arkit combine realitykit


    【解决方案1】:

    我已经设法采取了不同的方法来解决这个问题。

    首先我创建了一个 AnchorEntity

    let planeAnchor = AnchorEntity(
            plane: .horizontal,
            classification: .any,
            minimumBounds: [0.5,0.5]
        )
    

    我立即将机器人添加到此锚点:

     robot = MegaRobot(anchorEntity: planeAnchor, arView: self, gameSettings: gameSettings)
    

    此时,我将动画暂停但激活。

    现在我要订阅我的 planeAnchor 上 AnimationEvents 的更改:

    func observeAnchorState() {
            if let robot = robot {
                self.gameSettings.gameStatus = .planeSearching
                // 1. Subscribe to changes
                self.anchorEntitySubscribtion = self.scene.subscribe(
                    to: SceneEvents.AnchoredStateChanged.self,
                    on: planeAnchor) { anchored in
                        // 3. if the change is the desired one, perform extra setup
                        if anchored.isAnchored {
                            robot.robotMode()
                            robot.animationController?.resume()
                            robot.activateRobotDragging()
                            self.gameSettings.gameStatus = .positioning
                            DispatchQueue.main.async {
                                // 4. Remove subscriber if further observations are not needed
                                self.anchorEntitySubscribtion?.cancel()
                                self.anchorEntitySubscribtion = nil
                            }
                        }
                    }
                // 2. add planeAnchor to the scene
                self.scene.anchors.append(planeAnchor)
            } else {
                print("Fail to load")
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 2020-06-09
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多