SpatialFeature
Updated: Oct 16, 2024
A SpatialFeature
is a collection of components, systems, and lifecycle events that you can add to your code. Spatial SDK will execute these at the appropriate time to implement features into any Spatial SDK app.
A SpatialFeature
allows you to integrate ready-made features into any of your Spatial SDK apps with only a few lines of code. For example, the Physics
class, which adds realistic interactions to your scene, is a SpatialFeature that you can add to your apps.
Use registerFeatures to add features to your app
The registerFeatures
function defines the features you want to include in your app. It is only called and used at startup. Any modifications to the list after the initial startup will not change the registered features.
Here is an example of registering multiple features in an app:
import com.meta.spatial.core.SpatialFeature
import com.meta.spatial.physics.PhysicsFeature
import com.meta.spatial.vr.VRFeature
class MyActivity : AppSystemActivity() {
override fun registerFeatures(): List<SpatialFeature> {
return listOf(
PhysicsFeature(spatial),
VRFeature(this))
}
...
}
Using this code, the example Spatial SDK app will have Physics
capabilities and default VR capabilities.
When should I make a SpatialFeature? You should create a SpatialFeature
if you plan to use a specific feature in multiple apps or want to use it as a code organization method.
What can I do in a SpatialFeature? Using a SpatialFeature
allows you to execute code at different lifecycle points of an Spatial SDK app.
How do I register components? Overrid the componentsToRegister
function to register components required for your feature. componentsToRegister
returns a list of ComponentRegistration
objects.
override fun componentsToRegister(): List<ComponentRegistration> {
return listOf(
ComponentRegistration.createConfig(MyComponent1.Companion, MyComponent1.id, { MyComponent1() }),
ComponentRegistration.createConfig(MyComponent2.Companion, MyComponent2.id, { MyComponent2() }, sendRate = SendRate.LOW))
}
ComponentRegistration
objects enable you to define your component’s SendRate
. If your component is essential and needs to be updated every frame, you can leave it as SendRate.DEFAULT
. If it does not need to be updated every frame, you may benefit from setting the sendRate
to SendRate.LOW
.
Use different functions to register the systems required for your feature depending on when you want your system to run.
You have three options:
earlySystemsToRegister
: All systems in the list returned by this function will run first.systemsToRegister
: All systems in the list returned by this function will run next.lateSystemsToRegister
: All systems in the list returned by this function will run last.
If you do not know which bucket to put your system in, or if it doesn’t matter, use systemsToRegister
.
override fun systemsToRegister(): List<SystemBase> {
return listOf(MySystem())
}
override fun earlySystemsToRegister(): List<SystemBase> {
return listOf(MyEarlySystem())
}
override fun lateSystemsToRegister(): List<SystemBase> {
return listOf(MyLateSystem())
}
Android and Spatial SDK lifecycle events
Using a SpatialFeature
, you can override most traditional Android lifecycle event functions, and some Spatial SDK specific lifecycle events. These functions will execute when the corresponding Android/Spatial SDK lifecycle event occurs.
You can override these functions by using a SpatialFeature
:
onCreate
onSceneReady
onStart
onResume
onPauseActivity
- This is only called when extending
VrActivity
, not VrService
, as services do not utilize OnPause()
.
onStopActivity
- This is only called when extending
VrActivity
, not VrService
, as services do not utilize OnStop()
.
onDestroy
onVRReady
onVRPause
Here is an example:
// Example init code
override fun onSceneReady() {
Entity.create(listOf(MyComponent()))
// Other initialization code here
...
}