In the Actor’s constructor, bind the SelectObjectEvent like this:
AMyActor::AMyActor(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
...other stuff...
#if WITH_EDITORONLY_DATA
USelection::SelectObjectEvent.AddUObject(this, &AMyActor::OnObjectSelected);
#endif
}
Then declare/implement the OnObjectSelected function:
In MyActor.h:
#if WITH_EDITORONLY_DATA
void OnObjectSelected(UObject* Object);
bool SelectedInEditor = false;
#endif
In MyActor.cpp:
#if WITH_EDITORONLY_DATA
void AMyActor::OnObjectSelected(UObject* Object)
{
UE_LOG(LogTemp, Log, TEXT("%s::OnObjectSelected(): %s was %s."), *GetName(), *Object->GetName(), *FString(Object->IsSelected() ? "selected" : "de-selected"));
if (Object == this)
{
SelectedInEditor = true;
...do stuff when your actor is selected...
}
else if (SelectedInEditor && !IsSelected())
{
SelectedInEditor = false;
...do stuff when your actor is un-selected...
}
}
#endif
Beware: OnObjectSelected will be called on every actor you have in the scene. This is why you have to filter out only what is relevant to your object (hence the if (Object == this) ).
It’s a bit harder to detect that an object that is un-selected. You have to keep a flag (bool SelectedInEditor) on your actor that is set when it is selected. Then, when you receive the object selected event for another object and IsSelected() on yourself returns false, you are officially un-selected.