自定义数据表:
#pragma once #include "CoreMinimal.h" #include "Engine/UserDefinedStruct.h" #include "Components/CanvasPanel.h" #include "Blueprint/UserWidget.h" #include "Runtime/Engine/Classes/Engine/UserDefinedStruct.h" #include "Runtime/Engine/Classes/Engine/DataTable.h" #include "Runtime/CoreUObject/Public/UObject/ObjectMacros.h" #include "MiniMapFlagData.generated.h" /*BlueprintType:暴露成为蓝图数据类型,蓝图变量可以使用该结构体作为数据类型*/ USTRUCT(BlueprintType) struct FUMiniMapFlagDataC : public FTableRowBase { GENERATED_USTRUCT_BODY() //小图标 UPROPERTY(EditAnywhere, BlueprintReadWrite) UUserWidget* flag; //小图标对应的Actor UPROPERTY(EditAnywhere, BlueprintReadWrite) AActor* Actor; //小图标对应的Canvas panel Slot UPROPERTY(EditAnywhere, BlueprintReadWrite) UCanvasPanelSlot* Slot; UPROPERTY(EditAnywhere, BlueprintReadWrite) int MyAge; };
成员变量添加UPROPERTY标签,可以享受到UE4垃圾自动管理机制,不用手动释放变量内存。
自定义类
#pragma once #include "CoreMinimal.h" #include "Engine/UserDefinedStruct.h" #include "Components/CanvasPanel.h" #include "Blueprint/UserWidget.h" #include "Runtime/Engine/Classes/Engine/UserDefinedStruct.h" #include "Runtime/Engine/Classes/Engine/DataTable.h" #include "Runtime/CoreUObject/Public/UObject/ObjectMacros.h" #include "MiniMapFlagData.generated.h" /** * */ UCLASS(BlueprintType) class PROJ10_0121_API UMiniMapFlagData : public UUserDefinedStruct { GENERATED_BODY() public: UMiniMapFlagData() {} UMiniMapFlagData(UUserWidget* flag, AActor* Actor, UCanvasPanelSlot* Slot) :flag(flag), Actor(Actor), Slot(Slot) {} /* flag(flag)表示将参数flag赋值给成员参数flag 使用方法:UMiniMapFlagData data(flag,Actor,Slot); */ public: //小图标 UPROPERTY() UUserWidget* flag; //小图标对应的Actor UPROPERTY() AActor* Actor; //小图标对应的Canvas panel Slot UPROPERTY() UCanvasPanelSlot* Slot; UPROPERTY() int MyAge; };
自定义函数添加说明文字,可在蓝图中显示,如果要支持中文,则只要把h和cpp文件以utf-8编码保存就可以了。
注意:EditAnywhere,BlueprintReadWrite等等修饰符不能拼写错误,拼写错误是不会有错误提示的,并且编译出错信息也不会告诉我们是修饰符写错了,只会说编译出错,没有详细的出错信息。
#include "CoreMinimal.h" #include "Blueprint/UserWidget.h" #include "Components/CanvasPanel.h" #include "MiniMap/MiniMapFlagData.h" #include "StaticMiniMapBase.generated.h" /** * */ UCLASS() class PROJ10_0121_API UStaticMiniMapBase : public UUserWidget { GENERATED_BODY() protected: UPROPERTY(EditAnywhere,BlueprintReadWrite, Category=myMethods,meta=(ToolTip="小图标容器面板")) UCanvasPanel * PanelFlag; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = myMethods,meta=(ToolTip="存放小图标数据机构数组")) TArray<UMiniMapFlagData*> ArrayFlag; //TArray<TSubclassOf<class UMiniMapFlagData>> FlagArray; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = myVariables,meta=(ToolTip="小地图比例尺")) float RatioMap; protected: UFUNCTION(BlueprintCallable, Category = myMethods,meta=(ToolTip="初始化UI控件对象")) void ini(UCanvasPanel* sFlagPanel); public: UFUNCTION(BlueprintCallable, Category = myMethods,meta=(ToolTip="添加图标到小地图")) void Add_Flag(UUserWidget* flag, AActor* actor); };
ArrayFlag是在C++定义的函数,可以在蓝图中显示中文说明文字。