首先要打开几个插件
再在项目设置下找到python设置开发者模式
返回编辑器,点击文件>创建C++类>Blueprint Function Library
#pragma once#include "CoreMinimal.h"#include "Kismet/BlueprintFunctionLibrary.h"#include "ZFunction.generated.h"UCLASS()class UEPYTHON_API UZFunction : public UBlueprintFunctionLibrary{GENERATED_BODY()public:UFUNCTION(BlueprintCallable)//将函数暴露给pythonstatic void CalledFromPython(FString InputString);};
修改.cpp中的内容#include "ZFunction.h"void UZFunction::CalledFromPython(FString InputString) {UE_LOG(LogTemp, Error, TEXT("%s"), *InputString);}
回到UE4编辑器,在输出日志中输入如下代码import unrealunreal.ZFunction.call_from_python("hello")
得到如下结果