【问题标题】:Receiving Xcode notification on Reality Composer animation end在 Reality Composer 动画结束时接收 Xcode 通知
【发布时间】:2023-02-02 18:51:06
【问题描述】:

我有以下正确加载的 Reality Composer 项目。如您所见,当动画完成时,它应该使用关键字“attackComplete”进行通知。

我如何收到此通知?

import RealityKit
import ARKit

class ViewController: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let boxAnchor = try! Experience.loadOrcAttack()
        arView.session.delegate = self
        arView.scene.anchors.append(boxAnchor)
        print("done")
    }
    
    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        print(anchors)
    }
}

【问题讨论】:

    标签: swift xcode realitykit reality-composer


    【解决方案1】:

    使用 Reality Composer 的通知,您可以实现两种场景:

    动作侦听器

    这是你的情况,使用很容易实现

    public var onAction: ((RealityKit.Entity?) -> Swift.Void)?

    import UIKit
    import RealityKit
    
    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        let scene = try! Experience.loadScene()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            arView.scene.anchors.append(scene)
            
            scene.actions.attackCompleted.onAction = notificationID   // listener
        }
     
        fileprivate func notificationID(_ entity: Entity?) {        
             print(scene.actions.attackCompleted.identifier)
        }
    }
    

    这是如何使用 .onAction 完成处理程序的示例:


    触发行动

    当您需要通知 Reality Composer 的场景播放动作时,请使用以下场景:

    import UIKit
    import RealityKit
    
    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        let scene = try! Experience.loadScene()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            arView.scene.anchors.append(scene)
        }
        
        @IBAction func press(_ sender: UIButton) {
            scene.notifications.spinner.post()            // trigger for action
        }
    }
    

    或为[NAME.NotificationTrigger]使用下标:

    @IBAction func press(_ sender: NSButton) {
        scene.notifications.allNotifications[0].post()
    }
    


    如果您需要更多信息,请阅读this post

    【讨论】:

    • 您的情况是您将其设置为在一段时间间隔后专门打印该语句。我需要诸如“didReceiveNotification”之类的东西或诸如侦听器功能之类的东西,还是不可用?我的意思是,我可以发送通知,那么我该如何接收呢?
    • 我认为答案是以下内容的实现:let newUpdate = arView.scene.subscribe(to: SceneEvents.Update.self
    • IE,我如何获得“attackComplete”的xcode输入
    • 它似乎确实在初步测试中起作用。谢谢你。
    猜你喜欢
    • 2020-08-31
    • 1970-01-01
    • 2019-08-11
    • 2022-01-22
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    相关资源
    最近更新 更多