Add in editor Icon to your Custom Actor

From Epic Wiki
Jump to: navigation, search

Template:Rating


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

<syntaxhighlight lang="cpp"> // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

  1. pragma once
  1. include "GameFramework/Actor.h"
  2. 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;; }; </syntaxhighlight>

Template:Warning

<syntaxhighlight lang="cpp"> //Old TSubobjectPtr<UBillboardComponent> SpriteComponent;

//New UBillboardComponent* SpriteComponent; </syntaxhighlight>

MyActor.cpp

<syntaxhighlight lang="cpp"> // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

  1. 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;

  1. 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; }

  1. endif // WITH_EDITORONLY_DATA

} </syntaxhighlight>

Template:Warning

<syntaxhighlight lang="cpp"> //Old AMyActor::AMyActor(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP) {

//New AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { </syntaxhighlight> <syntaxhighlight lang="cpp">

//Old TSubobjectPtr<USceneComponent> SceneComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComp"));

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

</syntaxhighlight> <syntaxhighlight lang="cpp">

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

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

</syntaxhighlight>

Template:Warning

<syntaxhighlight lang="cpp"> //Old SpriteComponent->AttachParent = RootComponent;

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

</syntaxhighlight>

Conclusion

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

Enjoy!

Eros (talk)