ISelectable Implementation Example
This implementation demonstrates:
Selection priority system based on ownership and object type (unit, structure)
Ordering system for selection groups
Local player objects are prioritized over other objects
Units are prioritized over structures
Objects are ordered within their groups based on type and ID
Multi-selection and grouping restrictions based on ownership
Integration with Unity's component system
This sample file can be found at the following path
Assets\Santutu\RTS\Selectables\SimpleRealWorldSamples\SelectableRealWorldSample.cs
And you can play the demo.
Assets\Santutu\RTS\Selectables\SimpleRealWorldSamples\Scenes\Demo.scene
Here is preview. You can customize it to fit your project.
class SelectableRealWorldSample : MonoBehaviour, ISelectable
{
[field: SerializeField] public ObjectData Data { get; private set; }
[field: SerializeField] public Forceable Forceable { get; private set; }
[field: SerializeField] public bool CanBeSelected { get; private set; } = true;
[field: SerializeField] public UnityEvent<bool> OnSelectChanged { get; private set; } = new();
[field: SerializeField] public UnityEvent<bool> OnHoverChanged { get; private set; } = new();
public bool IsLocalPlayer => Forceable.forceId == Forceable.LocalPlayerId;
public Sprite Icon => Data.icon;
// Primary selection priority
// Returns 0 for local player objects, 1 for others
// Used to prioritize selecting player's own units first
public int SelectionPriority1
{
get
{
if (IsLocalPlayer) return 0;
return 1;
}
}
// Secondary selection priority
// Prioritizes units (0) over structures (1) and other types (2)
public int SelectionPriority2
{
get
{
if (Data.type == ObjectType.Unit) return 0;
else if (Data.type == ObjectType.Structure) return 1;
return 2;
}
}
// Tertiary selection priority
// For units, returns 0
// For other types, uses the Order value
public int SelectionPriority3
{
get
{
if (Data.type == ObjectType.Unit) return 0;
return Order;
}
}
// Determines the ordering within selection groups
// Units get a higher base order (10000 + ID)
// Other types just use their ID
public int Order
{
get
{
int order = 0;
if (Data.type == ObjectType.Unit)
{
order += 10000;
}
order += Data.id;
return order;
}
}
// SubGroupId uses the object's ID for grouping related selectables
public int SubGroupId => Data.id;
// Only local player objects can be multi-selected
public bool CanBeMultiSelected => IsLocalPlayer;
// Only local player objects can be grouped
public bool CanBeGrouped => IsLocalPlayer;
}
Last updated