The Highlight feature provides visual feedback when entities are hovered, selected, or in other notable states. It includes a default implementation using outlines but can be extended for different visual styles.
Core Components
Highlightable
The base component that controls highlight state:
public struct Highlightable : IComponentData
{
public bool IsEnabled;
public float Intensity;
}
We provide a built-in system that uses this to highlight interactables that are currently being hovered.
Outlines
The default implementation uses the Outline feature to show highlights:
- Outlines animate smoothly in/out when highlight state changes
- Works with both single meshes and hierarchies
- Customizable thickness and color via outline material
Extending the System
Custom Visual Effects
You can implement alternative highlight visuals by creating systems that react to Highlightable state:
public partial struct ColorHighlightSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
foreach (var (highlight, material) in
SystemAPI.Query<RefRO<Highlightable>, RefRW<MaterialPropertyColor>>())
{
var targetColor = highlight.ValueRO.IsEnabled ?
new float4(1, 0.5f, 0, 1) :
new float4(1, 1, 1, 1);
material.ValueRW.Value = math.lerp(
material.ValueRO.Value,
targetColor,
SystemAPI.Time.DeltaTime * 5f);
}
}
}
Custom Highlight Triggers
You can also create systems that enable highlights based on custom conditions:
public partial struct ProximityHighlightSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
var playerPosition = SystemAPI.GetSingleton<PlayerPosition>();
foreach (var (highlightable, transform) in
SystemAPI.Query<RefRW<Highlightable>, RefRO<LocalToWorld>>())
{
var distance = math.distance(
transform.ValueRO.Position,
playerPosition.Value);
highlightable.ValueRW.IsEnabled = distance < 2f;
}
}
}
Best Practices
- Consider performance when choosing highlight effects for large numbers of entities
- Implement highlight effects on the client side only
- Use the built-in outline system as a reference for implementing new highlight effects
Example: Complete Highlight Setup
public class CustomHighlightableAuthoring : MonoBehaviour
{
public Color HighlightColor = Color.yellow;
public bool EnableOnStart = false;
private class Baker : Baker<CustomHighlightableAuthoring>
{
public override void Bake(CustomHighlightableAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new Highlightable
{
IsEnabled = authoring.EnableOnStart,
Intensity = authoring.EnableOnStart ? 1f : 0f
});
AddComponent(entity, new CustomHighlightEffect
{
Color = authoring.HighlightColor
});
}
}
}