Unreal Engine 4 C 为编辑器中Actor创建自定义图标

发表于2017-09-27
评论0 1.1k浏览

有时候我们创建场景的时候,特定的Actor我们想给它一个特定的图标,便于观察。比如这样:


实现起来也很简单,需要编写C 代码:

我们创建一个Actor,叫AMyActor,它包含一个Sprite(精灵),这个精灵负责显示自定义图标:代码如下:

  1. #pragma once  
  2.   
  3. #include "GameFramework/Actor.h"  
  4. #include "Components/BillboardComponent.h"  
  5. #include "MyActor.generated.h"  
  6.   
  7. /** 
  8.  *  
  9.  */  
  10. UCLASS()  
  11. class NANTOPDOWN_API AMyActor : public AActor  
  12. {  
  13.     GENERATED_UCLASS_BODY()  
  14.   
  15.     //virtual void BeginPlay() OVERRIDE;  
  16.   
  17.     UPROPERTY()  
  18.     TSubobjectPtr<UBillboardComponent> SpriteComponent;  
  19.   
  20.     UTexture2D* SpriteTexture;  
  21.       
  22. };  

  1. #include "NanTopDown.h"  
  2. #include "MyActor.h"  
  3.   
  4.   
  5. AMyActor::AMyActor(const class FPostConstructInitializeProperties& PCIP)  
  6.     : Super(PCIP)  
  7. {  
  8.     struct FConstructorStatics  
  9.     {  
  10.         ConstructorHelpers::FObjectFinderOptional<UTexture2D> NoteTextureObject;  
  11.   
  12.         FName ID_Notes;  
  13.         FText Name_Notes;  
  14.   
  15.         FConstructorStatics()  
  16.             : NoteTextureObject(TEXT("Texture2D'/Game/Textures/MyIcon.MyIcon'"))  
  17.             , ID_Notes(TEXT("Notes"))  
  18.             , Name_Notes(NSLOCTEXT("SpriteCategory""Notes""Notes"))  
  19.         {  
  20.         }  
  21.     };  
  22.       
  23.         static FConstructorStatics Cs;  
  24.         TSubobjectPtr<USceneComponent> SceneComponent = PCIP.CreateDefaultSubobject<USceneComponent>(  
  25.             this, TEXT("SceneComp"));  
  26.         RootComponent = SceneComponent;  
  27.         RootComponent->Mobility = EComponentMobility::Static;  
  28.   
  29. #if WITH_EDITORONLY_DATA  
  30.         SpriteComponent = PCIP.CreateEditorOnlyDefaultSubobject<UBillboardComponent>(this, TEXT("Sprite"));  
  31.         if (SpriteComponent)  
  32.         {  
  33.             SpriteComponent->Sprite = Cs.NoteTextureObject.Get();  
  34.             SpriteComponent->SpriteInfo.Category = Cs.ID_Notes;  
  35.             SpriteComponent->SpriteInfo.DisplayName = Cs.Name_Notes;  
  36.             SpriteComponent->AttachParent = RootComponent;  
  37.             SpriteComponent->Mobility = EComponentMobility::Static;  
  38.         }  
  39. #endif  
  40. }  

AMyActor只有一个UBillboardComponent组件,加入场景就可以看到自定义的图片了。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: