You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unreal引擎中如何根据选中的Enum隐藏可蓝图编辑函数的输入参数?

实现Unreal几何脚本节点参数动态显隐(匹配Blender Quadrilateral节点)

核心思路

Unreal原生UFunction参数没有内置类似EditCondition的显隐控制,需要通过C++编辑器扩展,结合自定义元数据+节点参数监听的方式实现。


步骤1:标记参数的Enum关联元数据

在几何脚本函数库中,给目标UFunction的参数添加自定义元数据,明确每个参数对应Enum的哪个值才显示:

// 定义Quad类型枚举
UENUM(BlueprintType)
enum class EQuadCreationType : uint8
{
    Rectangle,
    Points
};

UCLASS()
class YOUR_PROJECT_API UMyGeometryScriptLib : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

    UFUNCTION(BlueprintCallable, Category="Geometry Script|Mesh Creation", meta=(DisplayName="Create Quadrilateral"))
    static UStaticMesh* CreateQuadrilateral(
        // 第一个Enum参数
        UPARAM(DisplayName="Creation Type") EQuadCreationType CreationType,
        // Rectangle模式下显示的参数,标记EnumCondition元数据
        UPARAM(meta=(EnumCondition="Rectangle")) float Width = 100.f,
        UPARAM(meta=(EnumCondition="Rectangle")) float Height = 100.f,
        // Points模式下显示的参数
        UPARAM(meta=(EnumCondition="Points")) FVector Point1 = FVector(0,0,0),
        UPARAM(meta=(EnumCondition="Points")) FVector Point2 = FVector(100,0,0),
        UPARAM(meta=(EnumCondition="Points")) FVector Point3 = FVector(100,100,0),
        UPARAM(meta=(EnumCondition="Points")) FVector Point4 = FVector(0,100,0)
    );
};

步骤2:编写编辑器扩展逻辑

创建编辑器模块,监听Enum参数的变化,动态调整其他参数引脚的可见性:

2.1 配置模块依赖

在项目的.Build.cs文件中添加依赖:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "UnrealEd", "BlueprintGraph", "GeometryScriptingCore" });

2.2 实现节点监听与显隐控制

#include "Editor/UnrealEd/Public/BlueprintGraph/BlueprintGraphNode.h"
#include "Editor/UnrealEd/Public/Kismet2/BlueprintNodeSpawner.h"

class FMyGeometryScriptEditorModule : public IModuleInterface
{
public:
    virtual void StartupModule() override
    {
        // 注册目标函数的节点创建回调
        FBlueprintNodeSpawner::RegisterSpawnerDelegateForFunction(
            UMyGeometryScriptLib::StaticClass(),
            GET_FUNCTION_NAME_CHECKED(UMyGeometryScriptLib, CreateQuadrilateral)
        ).SetCustomNodeHandling(FOnCreateBlueprintNodeHandler::CreateStatic(&FMyGeometryScriptEditorModule::HandleQuadNodeCreation));
    }

    virtual void ShutdownModule() override
    {
        // 注销回调
        FBlueprintNodeSpawner::UnregisterSpawnerDelegateForFunction(
            UMyGeometryScriptLib::StaticClass(),
            GET_FUNCTION_NAME_CHECKED(UMyGeometryScriptLib, CreateQuadrilateral)
        );
    }

private:
    static UEdGraphNode* HandleQuadNodeCreation(UEdGraph* ParentGraph, const FNodeSpawnData& SpawnData)
    {
        // 创建默认的函数节点
        UEdGraphNode* Node = FBlueprintNodeSpawner::CreateNodeFromSpawnData(ParentGraph, SpawnData);
        if (UBlueprintFunctionNode* FuncNode = Cast<UBlueprintFunctionNode>(Node))
        {
            // 绑定参数变化的回调,当Enum参数修改时触发
            FuncNode->OnParameterChanged().AddStatic(&FMyGeometryScriptEditorModule::OnQuadTypeChanged, FuncNode);
            // 初始化时设置一次参数可见性
            OnQuadTypeChanged(FuncNode, FuncNode->FindPin(TEXT("CreationType")));
        }
        return Node;
    }

    static void OnQuadTypeChanged(UBlueprintFunctionNode* FuncNode, UEdGraphPin* TypePin)
    {
        if (!FuncNode || !TypePin) return;

        // 获取当前选中的Enum值
        EQuadCreationType CurrentType = static_cast<EQuadCreationType>(TypePin->GetDefaultValue().ToInt());

        // 遍历所有参数引脚,根据元数据控制显隐
        for (UEdGraphPin* Pin : FuncNode->Pins)
        {
            if (Pin->GetName() == TEXT("CreationType")) continue;

            // 读取参数的EnumCondition元数据
            const FString ConditionStr = Pin->GetMetaData(TEXT("EnumCondition"));
            if (ConditionStr.IsEmpty()) continue;

            // 解析元数据对应的Enum值
            EQuadCreationType ConditionType = EQuadCreationType::Rectangle;
            UEnum::GetValueByNameString(ConditionStr, ConditionType);

            // 设置引脚是否隐藏
            Pin->bHidden = (CurrentType != ConditionType);
        }

        // 强制节点重绘更新布局
        FuncNode->MarkNodeDirty();
        FuncNode->GetGraph()->NotifyGraphChanged();
    }
};

IMPLEMENT_MODULE(FMyGeometryScriptEditorModule, MyGeometryScriptEditor)

注意事项

  1. 该方案仅在编辑器生效,运行时即使参数隐藏,也会使用蓝图中设置的默认值或自定义值。
  2. 拖动Enum下拉框时,参数显隐会实时切换,符合Blender节点的交互逻辑。
  3. 若需自动适配节点尺寸,可在OnQuadTypeChanged末尾添加节点宽高的计算调整代码。

内容的提问来源于stack exchange,提问作者Mike

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:15:28