UE蓝图和CPP通信
目录Blueprint Library
- 在内容浏览器 Content中 右键创建Function Library 打开后会让填一个函数名。然后打开关卡蓝图。发现可以作为一个函数被调用到。
- 在C++ Classes中创建C++类,选择Blueprint Function Library 在生成的cpp和h文件中添加一个函数如下。
// .h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "FuncLibTest.generated.h"
/**
*
*/
UCLASS()
class UECPP_API UFuncLibTest : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "MyTest")
static void TestPrint(); // 注意这里不写static在蓝图中使用就要多连一个self
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "FuncLibTest.h"
void UFuncLibTest::TestPrint()
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 9.f, FColor::Red, TEXT("Debug"));
}
}
同理,添加对应的函数参数和返回值。会体现在蓝图参数中
FName FString FText
-
FName不区分大小写,用于快速定位资源是否相同,所以内部维护了哈希值
-
FText比较重,描述文本,可以用于多语言
-
FString最接近 std::string
转换规则
-
FName,FText可以通过 ToString()转FString
-
FString=>FName 通过FName构造函数丢入一个FString实例FName var = FName(*FStringIns)
-
FString=>FText通过 FText::FromString()
-
FName=>FText
-
TEXT宏主要是为了不乱码。ASCII到 TCHAR
Cpp和蓝图通信
创建Actor的cpp类,然后创建对应蓝图脚本。添加注解可被蓝图使用
- 暴露变量给蓝图
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "UI")
float TestFloat;
- 暴露函数给蓝图
UFUNCTION(BlueprintCallable, Category = "UI")
void TestFunc();
- Cpp调用蓝图逻辑通过事件
如下,函数不用实现,在蓝图中实现函数体。Cpp中调用这个函数相当于触发了蓝图的这个Event。实现cpp中调用蓝图逻辑
UFUNCTION(BlueprintImplementableEvent)
void TestEvent();
UFUNCTION(BlueprintNativeEvent)
void TestNativeEvent();
注意其在对应cpp的实例函数比较特殊
void ATestActor::TestNativeEvent_Implementation()
{
}
- 不知道取什么标题
UFUNCTION(BlueprintPure, Category = "UI", meta = (NativeBreakFunc))
void BreakFunc(const FRandomStream &MyRendom, bool isV);
UFUNCTION(BlueprintPure, meta = (DisplayName = "HelloW", CompactNodeTitle = "Hello", Keywords = "Hello", CommutativeAssociativeBinaryOperator = "true"), Category = "UI")
float MyHello(float a, float b);
void ATestActor::BreakFunc(const FRandomStream &MyRendom, bool isV)
{
}
float ATestActor::MyHello(float a, float b)
{
return a > b ? a : b;
}
- Make节点
UFUNCTION(BlueprintPure, meta = (Z = "1", NativeMakeFunc), Category = "UI") // Z 是初始值
FVector TestMakeVector(float x, float y, float z);
FVector ATestActor::TestMakeVector(float x, float y, float z)
{
FVector v;
v.X = x;
v.Y = y;
v.Z = z;
return v;
}
- 枚举到蓝图
UENUM()
namespace MyType
{
enum Type
{
Type_1,
Type_2,
};
}
UPROPERTY(EditAnywhere, Category = "UI")
TEnumAsByte<MyType::Type> Var;