I am trying to toggle hiding/showing entities in a scene depending on a “layer” they belong to (string that each object has). These layers are custom, and are not to be confused with Unity layers.
In GameObject version of my application, the method looks like this:
private void BindLayerItem(VisualElement element, int index)
{
var info = sceneLayers[index];
element.userData = info;
var selected = element.Q<Toggle>("visible-toggle");
selected.SetValueWithoutNotify(info.State == SceneLayerInfo.LayerStates.Visible);
selected.RegisterCallback<ChangeEvent<bool>>(evt =>
{
var i = element.userData as SceneLayerInfo;
i.State = evt.newValue ? SceneLayerInfo.LayerStates.Visible : SceneLayerInfo.LayerStates.Hidden;
gameManager.AppSettings.Save();
//update visualization
var sceneObject = GetVisualGameObject(i);
if (sceneObject != null)
{
sceneObject.SetActive(evt.newValue);
}
});
}
This is what I have in the ECS version of my application. I use Unity 2021.1.27f1 with Entities 0.1.1-preview (but I plan to upgrade to 2022.3.x and Entities 1.x):
private void BindLayerItem(VisualElement element, int index)
{
var info = sceneLayers[index];
element.userData = info;
var selected = element.Q<Toggle>("visible-toggle");
selected.SetValueWithoutNotify(info.State == SceneLayerInfo.LayerStates.Visible);
selected.RegisterCallback<ChangeEvent<bool>>(evt =>
{
var i = element.userData as SceneLayerInfo;
i.State = evt.newValue ? SceneLayerInfo.LayerStates.Visible : SceneLayerInfo.LayerStates.Hidden;
gameManager.AppSettings.Save();
//update visualization
var sceneObject = GetVisualGameObject(i);
if (sceneObject != null)
{
World.Active.EntityManager.DestroyEntity(sceneObject);
}
});
}
It destroys the entity, but I don’t know how to respawn it. Is there an equivalent to gameObject.SetActive(bool) in Entities (either preview, or the release version of the package)?
The Disabled component. Adding it to an Entity excludes it from queries, which means systems won’t find the entities (unless you query for disabled entities specifically, e.g. when you want to remove the component). This also excludes the entity from rendering. Since this involves structural changes, it’s not suitable for high-frequency changes.
There’s also the DisableRendering component for rendering specifically.
Enableable components. When a component implements the IEnableableComponent interface, ECS lets you enable or disable this component specifically. Queries that ask for this component type will omit disabled components by default. This adds a tiny bit of processing here and there (a bit mask is used to efficiently check which components are enabled), but enabling/disabling components doesn’t involve structural changes, so it’s very fast.
The MaterialMeshInfo struct now implements IEnableableComponent. I think you can use that to toggle rendering of specific entities.
As far as I remember ECS didn’t have enableable components before 1.0 but I think the Disabled component might fit your use case.
I just managed to upgrade the project to Unity 2022.3, so I will be able to rewrite it with Entities 1.x, and thus I will have an option to use IEnableableComponent