c++ 如何使一个继承了只能在UE5中使用的类的基于UObject的类在UE4和UE5中都能被编译?

xoshrz7s  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(422)

我正在做一个同时适用于UE4和UE5的项目。现在我想制作一些类似于几何脚本的蓝图函数。众所周知,UDynamicMesh只能用于UE5。我尝试使用"#if UE_VERSION_OLDER_THAN(5,0,0)"或"#if ENGINE_MAJOR_VERSION == 5 "来解决这个问题,但是我发现UCLASS和UFUNCTION不能在预处理器内部。我不知道如何制作它。有人能给一些建议吗?我将感激你能提供的任何帮助。我的代码在这里。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UDynamicMesh.h"
#include "GeometryScript/GeometryScriptTypes.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyGeometryScriptUtils.generated.h"

/**
 * 
 */
UCLASS(meta = (ScriptName = "GeometryScript_Primitives"))
class MyGEOMETRYPROCESSING_API UMyGeometryScriptUtils : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, Category = "MyGeometryScript", meta=(ScriptMethod))
    static UPARAM(DisplayName = "Target Mesh") UDynamicMesh* 
    Remesh( 
    UDynamicMesh* TargetMesh,
    float TargetTriangleCount = 100.f,
    int32 IterationTimes = 100,
    UGeometryScriptDebug* Debug = nullptr);
    
    
    UFUNCTION(BlueprintCallable, Category = "MyGeometryScript", meta=(ScriptMethod))
    static UPARAM(DisplayName = "Target Mesh") UDynamicMesh* 
    PlaneUV( 
    UDynamicMesh* TargetMesh,
    const bool bAutoRemap = false,
    const FVector2D InPivot = FVector2D::ZeroVector,
    const float InScale = 1.0f,
    UGeometryScriptDebug* Debug = nullptr);

};

我试着用

#if ENGINE_MAJOR_VERSION == 5

使其不会在UE4中编译。代码如下。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Misc/EngineVersionComparison.h"
#include "CoreMinimal.h"
#include "UDynamicMesh.h"
#include "GeometryScript/GeometryScriptTypes.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyGeometryScriptUtils.generated.h"

/**
 * 
 */
#if ENGINE_MAJOR_VERSION == 5

UCLASS(meta = (ScriptName = "GeometryScript_Primitives"))
class MyGEOMETRYPROCESSING_API UMyGeometryScriptUtils : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, Category = "MyGeometryScript", meta=(ScriptMethod))
    static UPARAM(DisplayName = "Target Mesh") UDynamicMesh* 
    Remesh( 
        UDynamicMesh* TargetMesh,
        float TargetTriangleCount = 100.f,
        int32 IterationTimes = 100,
        UGeometryScriptDebug* Debug = nullptr);
    
    
    UFUNCTION(BlueprintCallable, Category = "MyGeometryScript", meta=(ScriptMethod))
    static UPARAM(DisplayName = "Target Mesh") UDynamicMesh* 
    PlaneUV( 
        UDynamicMesh* TargetMesh,
        const bool bAutoRemap = false,
        const FVector2D InPivot = FVector2D::ZeroVector,
        const float InScale = 1.0f,
        UGeometryScriptDebug* Debug = nullptr);

};

#endif

但超高温处理会给我错误:

UCLASS must not be inside preprocessor blocks, except for WITH_EDITORONLY_DATA

我认为Mauro Dorni给出的解决方案可以解决以上问题。但是,我遇到了一个类似的问题,不能用他的解决方案解决它。代码如下所示。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GeometryActors/GeneratedDynamicMeshActor.h"
#include "MyGeneratedDynamicMeshActor.generated.h"

UCLASS()
class MyTest_API AMyGeneratedDynamicMeshActor : public AGeneratedDynamicMeshActor
{
    GENERATED_UCLASS_BODY()

public:

    UPROPERTY(BlueprintReadWrite)
    UMySplineComponent* SplineComponent;

    virtual void PostActorCreated() override;

    UFUNCTION(BlueprintCallable, CallInEditor, Category="Utils")
    void UpdateActor();
    
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
    
public:
    // Called every frame
    virtual void Tick(float DeltaTime) override;

};

主要区别是AGeneratedDynamicMeshActor类只在UE5中定义。如果我编写如下代码,UHT仍然会给我错误。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once
#include "Misc/EngineVersionComparison.h"
#include "CoreMinimal.h"

#if UE_VERSION_OLDER_THAN(5,0,0)
#include "GameFramework/Actor.h"
#else
#include "GeometryActors/GeneratedDynamicMeshActor.h"
#endif

#include "Manipulator/SplineComponents/MySplineComponent.h"
#include "MyGeneratedDynamicMeshActor.generated.h"

UCLASS()
#if UE_VERSION_OLDER_THAN(5,0,0)
class MyTest_API AMyGeneratedDynamicMeshActor : public AActor
#else
class MyTest_API AMyGeneratedDynamicMeshActor : public AGeneratedDynamicMeshActor
#endif
{
    GENERATED_UCLASS_BODY()

    UPROPERTY(BlueprintReadWrite)
    UMySplineComponent* SplineComponent;

    virtual void PostActorCreated() override;

    UFUNCTION(BlueprintCallable, CallInEditor, Category="Utils")
    void UpdateActor();
    
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
    
public:
    // Called every frame
    virtual void Tick(float DeltaTime) override;

};

错误报告为

[] Missing 'class' in Class declaration
31moq8wy

31moq8wy1#

我创建了一个代理结构体来存储构建几何体所需的所有数据。因为你可以将其封装在宏中,所以你可以很容易地进行版本控制。然后提供一个静态库或UObject来与数据进行适当的交互。
IE在ue4中,我使用代理结构构建FDynamicMesh3,但在ue5中,我使用相同的代理构建UDynamicMesh对象。

相关问题