Unique
Unique components (a.k.a. resources) are useful when you know there will only ever be a single instance of some component.
In that case there is no need to attach the component to an entity. It also works well as global data without most of its drawback.
As opposed to the default storage uniques are declared using the Unique
trait.
// Using a derive macro
#[derive(Unique)]
struct Camera;
// By manually implementing the trait
struct Camera;
impl Unique for Camera {}
They also need to be initialized with add_unique
. We can then access them with UniqueView
and UniqueViewMut
.
let world = World::new();
world.add_unique(Camera::new());
world
.run(|camera: UniqueView<Camera>| {
// -- snip --
});