Changing programmatically the text of a 3D TextNode in SceneKit using Swift

Viewed 3371

So I have placed a 3D Text Node in my SCN scene and I would like to change the value of the Text property later in my application

SceneKit Inspector

enter image description here

Above is how I change the value of the text from the inspector but is there a way to do it programmatically? thank you

3 Answers

This is what worked for me. Assume you've added a 3D Text Node in interface builder and it's called "CountDownText" in the node hierarchy.

In the relevant view controller, I have two properties:

var countDownText: SCNNode!
var theCountDownText: SCNText!

Later, bind the node from the interface builder to the code (in a set up node function):

countDownText = scnScene.rootNode.childNode(withName: "CountDownText", recursively: true)
theCountDownText = countDownText.geometry as! SCNText

Any time you want to change the display text of that node, you can just do as follows:

theCountDownText.string = "text"
Related