Joseph Hajjar
Joseph Hajjar

Reputation: 148

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

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

Upvotes: 3

Views: 3865

Answers (3)

Diskprotek
Diskprotek

Reputation: 601

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"

Upvotes: 0

mnuages
mnuages

Reputation: 13462

When you instantiate a "3D Text" object from the Object Library in Xcode, what you get is a SCNNode that has a SCNText as its geometry.

if let textGeometry = textNode.geometry as? SCNText {
    textGeometry.string = "something"
}

Upvotes: 5

Charles-olivier Demers
Charles-olivier Demers

Reputation: 1048

So if you have linked your text node with your game scene, programmatically you can do:

textNode.geometry.string = "your string"

It should work!

Upvotes: 0

Related Questions