Add in Editor Icon to your Custom Actor

Code Snippets that show you how to add in editor icon to your custom Actor.

Updated over 2 years ago Edit Page Revisions

Overview

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 SpriteComponent;

    // Icon sprite
    UTexture2D* SpriteTexture;;
};

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

  
//Old
TSubobjectPtr 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 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 SceneComponent = PCIP.CreateDefaultSubobject(this, TEXT("SceneComp"));
    RootComponent = SceneComponent;
    RootComponent->Mobility = EComponentMobility::Static;

#if WITH_EDITORONLY_DATA
    SpriteComponent = PCIP.CreateEditorOnlyDefaultSubobject(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

}

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 SceneComponent = PCIP.CreateDefaultSubobject(this, TEXT("SceneComp"));

//New
USceneComponent* SceneComponent = ObjectInitializer.CreateDefaultSubobject(this, TEXT("SceneComp"));

//Old
SpriteComponent = PCIP.CreateEditorOnlyDefaultSubobject(this, TEXT("Sprite"));

//New
SpriteComponent = ObjectInitializer.CreateEditorOnlyDefaultSubobject(this, TEXT("Sprite"));

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.