Unreal Engine 4 C++ 爲編輯器中Actor創建自定義圖標

有時候我們創建場景的時候,特定的Actor我們想給它一個特定的圖標,便於觀察。比如這樣:


實現起來也很簡單,需要編寫C++代碼:

我們創建一個Actor,叫AMyActor,它包含一個Sprite(精靈),這個精靈負責顯示自定義圖標:代碼如下

#pragma once

#include "GameFramework/Actor.h"
#include "Components/BillboardComponent.h"
#include "MyActor.generated.h"

/**
 * 
 */
UCLASS()
class NANTOPDOWN_API AMyActor : public AActor
{
	GENERATED_UCLASS_BODY()

	//virtual void BeginPlay() OVERRIDE;

	UPROPERTY()
	TSubobjectPtr<UBillboardComponent> SpriteComponent;

	UTexture2D* SpriteTexture;
	
};


#include "NanTopDown.h"
#include "MyActor.h"


AMyActor::AMyActor(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UTexture2D> NoteTextureObject;

		FName ID_Notes;
		FText Name_Notes;

		FConstructorStatics()
			: NoteTextureObject(TEXT("Texture2D'/Game/Textures/MyIcon.MyIcon'"))
			, ID_Notes(TEXT("Notes"))
			, Name_Notes(NSLOCTEXT("SpriteCategory", "Notes", "Notes"))
		{
		}
	};
	
		static FConstructorStatics Cs;
		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 = Cs.NoteTextureObject.Get();
			SpriteComponent->SpriteInfo.Category = Cs.ID_Notes;
			SpriteComponent->SpriteInfo.DisplayName = Cs.Name_Notes;
			SpriteComponent->AttachParent = RootComponent;
			SpriteComponent->Mobility = EComponentMobility::Static;
		}
#endif
}


AMyActor只有一個UBillboardComponent組件,加入場景就可以看到自定義的圖片了。


參考文章:https://wiki.unrealengine.com/Add_in_editor_Icon_to_your_Custom_Actor

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章