介绍
对于标记有BlueprintImplementableEvent或者BlueprintNativeEvent的C++和蓝图函数,能够被C#重写函数逻辑。
注意
UE5.5开始对C++类BlueprintNativeEvent函数做了改动,插件从UE5.5开始不再支持覆盖C++类BlueprintNativeEvent函数,参看Change the code generation of UHT for BP implementable events to check for any script implementations of the event.。
流程
- 通过Partial,新建一个函数所属类的Partial类,并且标记
Override
- 声明一个拥有相同函数签名的函数,并且同样需要标记
Override
- 普通函数:C++或蓝图函数名
Test
,C#函数名Test
- RPC函数:C++或蓝图函数名
Server_Test
,C#函数名Server_Test_Implementation
- 普通函数:C++或蓝图函数名
示例:覆盖函数
- C++
- C#
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestCSharpFunctionActor.generated.h"
UCLASS()
class UNREALCSHARPTEST_API ATestCSharpFunctionActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestCSharpFunctionActor();
public:
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void SetInt32ValueFunction(int32 InInt32Value);
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
int32 GetInt32ValueFunction() const;
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void OutInt32ValueFunction(int32& OutInt32Value) const;
};
using Script.CoreUObject;
namespace Script.UnrealCSharpTest
{
[Override]
public partial class ATestCSharpFunctionActor
{
[Override]
public void SetInt32ValueFunction(int InInt32Value)
{
Int32Value = InInt32Value;
}
[Override]
public int GetInt32ValueFunction()
{
return Int32Value;
}
[Override]
public void OutInt32ValueFunction(ref int OutInt32Value)
{
OutInt32Value = Int32Value;
}
}
}
代码扫描
在生成C#工程的时候,会先通过Microsoft.CodeAnalysis.CSharp对项目代码进行一次代码扫描,分析出标记有Override
的类和函数,并且生成到项目/Intermediate/CodeAnalysis
中,避免覆盖函数后续再生成,导致重定义。