ARKit: Name objects in Reality Composer, get names in Swift

Name with Reality Composer

Double-click the object to be displayed to display the setting screen and give it a name. In the case of the figure below, it is named "hako". スクリーンショット 2020-10-18 20.37.29.png

Get a name in Swift

When you tap the object, "hako" is displayed on the debug screen.


import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Load the "Box" scene from the "Experience" Reality File
        let boxAnchor = try! Experience.loadBox()
        boxAnchor.actions.tapped.onAction=clickEvent(_:)
        // Add the box anchor to the scene
        arView.scene.anchors.append(boxAnchor)
    }
    
    func clickEvent(_ entity: Entity?) {
        guard let entity = entity else { return }
        
        print(entity.name)
    }
}

"Tapped" is a behavior added by Reality Composer.

Recommended Posts

ARKit: Name objects in Reality Composer, get names in Swift
Compare objects in Swift
When you get lost in the class name
Map GET requests to complex objects in Spring.