Add in editor Icon to your Custom Actor

Rate this Article:
5.00
(one vote)

Approved for Versions:(please verify)


Overview

Greetings!

This is a code snippets show you how to add in editor icon to your custom Actor.

Just like this:

I add some comment for reference.

MyActor.h

 
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
 
#pragma once
 
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
 
/**
 * 
 */
UCLASS()
class AMyActor : public AActor
{
	GENERATED_UCLASS_BODY()
	virtual void BeginPlay() OVERRIDE;
 
 
	UPROPERTY()
	// A UBillboardComponent to hold Icon sprite
	TSubobjectPtr<UBillboardComponent> SpriteComponent;
 
	// Icon sprite
	UTexture2D* SpriteTexture;;
};

Note TSubobjectPtr has been depreciated as of v4.6. Use standard C++ pointers instead.

 
//Old
TSubobjectPtr<UBillboardComponent> SpriteComponent;
 
//New
UBillboardComponent* SpriteComponent;

MyActor.cpp

 
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
 
#include "MyActor.h"
 
AMyActor::AMyActor(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	// Structure to hold one-time initialization
	struct FConstructorStatics
	{
		// A helper class object we use to find target UTexture2D object in resource package
		ConstructorHelpers::FObjectFinderOptional<UTexture2D> NoteTextureObject;
 
		// Icon sprite category name
		FName ID_Notes;
 
		// Icon sprite display name
		FText NAME_Notes;
 
		FConstructorStatics()
			// Use helper class object to find the texture
			// "/Engine/EditorResources/S_Note" is resource path
			: NoteTextureObject(TEXT("/Engine/EditorResources/S_Note"))
			, ID_Notes(TEXT("Notes"))
			, NAME_Notes(NSLOCTEXT("SpriteCategory", "Notes", "Notes"))
		{
		}
	};
	static FConstructorStatics ConstructorStatics;
 
	// We need a scene component to attach Icon sprite
	TSubobjectPtr<USceneComponent> SceneComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComp"));
	RootComponent = SceneComponent;
	RootComponent->Mobility = EComponentMobility::Static;
 
#if WITH_EDITORONLY_DATA
	SpriteComponent = PCIP.CreateEditorOnlyDefaultSubobject<UBillboardComponent>(this, TEXT("Sprite"));
	if (SpriteComponent)
	{
 
		SpriteComponent->Sprite = ConstructorStatics.NoteTextureObject.Get();		// Get the sprite texture from helper class object
		SpriteComponent->SpriteInfo.Category = ConstructorStatics.ID_Notes;		// Assign sprite category name
		SpriteComponent->SpriteInfo.DisplayName = ConstructorStatics.NAME_Notes;	// Assign sprite display name
		SpriteComponent->AttachParent = RootComponent;				        // Attach sprite to scene component
		SpriteComponent->Mobility = EComponentMobility::Static;
	}
#endif // WITH_EDITORONLY_DATA
 
}

Note The syntax for constructors has been changed as of v4.6.

 
//Old
AMyActor::AMyActor(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
 
//New
AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
//Old
TSubobjectPtr<USceneComponent> SceneComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComp"));
 
//New
USceneComponent* SceneComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComp"));
//Old
SpriteComponent = PCIP.CreateEditorOnlyDefaultSubobject<UBillboardComponent>(this, TEXT("Sprite"));
 
//New
SpriteComponent = ObjectInitializer.CreateEditorOnlyDefaultSubobject<UBillboardComponent>(this, TEXT("Sprite"));

Note AttachParent has been made private as of v4.13. Use AttachToComponent instead

//Old
SpriteComponent->AttachParent = RootComponent;
 
//New
SpriteComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);

Conclusion

Please refer to Engine\Source\Runtime\Engine\Private\Note.cpp for more information.

Enjoy!

Eros (talk)