Components
An entity is a bag of components — a sprite, a physics body, an audio source. From a script you reach them through GetComponent<T>().
public class Player : GameEntity
{
private Rigidbody2DComponent? m_Body;
private SpriteRendererComponent? m_Sprite;
protected override void OnCreate()
{
m_Body = GetComponent<Rigidbody2DComponent>();
m_Sprite = GetComponent<SpriteRendererComponent>();
}
}How the façades work
Every class on this page is a façade: a thin object with no state of its own that reads and writes straight through to the engine's component the moment you touch a property.
Two consequences worth knowing:
GetComponent<T>()builds a new façade every call. It's cheap, but not free — cache it in a field inOnCreaterather than calling it inOnUpdate.- There are no C# events on components. Nothing native holds a durable reference to a façade, so there is nothing to raise an event on. Notifications are
GameEntityvirtual methods instead —OnClick,OnAnimatorStateEntered,OnTriggerEnter. See GameEntity.
Checking before you reach
GetComponent<T>() returns null when the entity doesn't have that component. A null-checked call that silently does nothing looks exactly like "the feature is broken", so check explicitly when you're not sure:
if (HasComponent<Rigidbody2DComponent>())
GetComponent<Rigidbody2DComponent>()!.LinearVelocity = Vector2.Zero;The component façades
| Class | Editor name | Reference |
|---|---|---|
TransformComponent | Transform | Below |
Rigidbody2DComponent | Rigidbody 2D | Rigidbody2DComponent |
SpriteRendererComponent | Sprite Renderer | SpriteRendererComponent |
TextComponent | Text Component | TextComponent |
CameraComponent | Camera | Camera |
CameraFollowComponent | Camera Follow | Camera |
Animator | Animator | Animator |
AudioSourceComponent | Audio Source | Audio |
ParticleEmitter | Particle Emitter | Particles |
TilemapComponent | Tilemap | TilemapComponent |
Canvas | Canvas | UI |
RectTransform | Rect Transform | UI |
UIImage | UI Image | UI |
UIButtonComponent | UI Button | UI |
UIProgressBar | UI Progress Bar | UI |
Components with no C# façade yet
Several components exist in the editor but have no scripting surface in 0.1.0: Audio Listener, Circle Renderer, Box Collider 2D and Circle Collider 2D (configure them in the Properties panel; query them with Physics2D), and the UI components UI Selectable, UI Slider, UI Toggle, UI Layout Group, UI Mask and UI Scroll Rect. See UI scripting for the workaround.
TransformComponent
The entity's position. There is only one member, and it forwards to GameEntity.Translation — use whichever reads better where you are.
Translation
GetComponent<TransformComponent>().Translation = new Vector3(0, 5, 0);
// identical to:
Translation = new Vector3(0, 5, 0);Rotation and scale
There is no C# access to rotation or scale in 0.1.0 — set them in the Properties panel. Position is the only transform channel a script can drive.
See also
- GameEntity —
GetComponent,HasComponent, and the lifecycle hooks components notify through - Attributes — exposing your own script fields to the Properties panel