Within the Blueprint system, we can use instances of our MyCustomAsset
class as variables, provided we mark that class as a BlueprintType
in its UCLASS
macro.
However, by default, our new asset is simply treated as UObject
, and we can't access any of its members:
For some types of assets, we might wish to enable in-line editing of literal values in the same way that classes such as FVector
support the following:
In order to enable this, we need to use a Graph Pin visualizer. This recipe will show you how to enable in-line editing of an arbitrary type using a custom widget defined by you.
MyCustomAssetPinFactory.h
.#pragma once #include "EdGraphUtilities.h" #include "MyCustomAsset.h" #include "SGraphPinCustomAsset.h" struct UE4COOKBOOKEDITOR_API FMyCustomAssetPinFactory : public FGraphPanelPinFactory { public: virtual TSharedPtr<class SGraphPin> CreatePin(class UEdGraphPin* Pin) const override { if (Pin->PinType.PinSubCategoryObject == UMyCustomAsset::StaticClass()) { return SNew(SGraphPinCustomAsset, Pin); } else { return nullptr; } }; };
SGraphPinCustomAsset
:#pragma once #include "SGraphPin.h" class UE4COOKBOOKEDITOR_API SGraphPinCustomAsset : public SGraphPin { SLATE_BEGIN_ARGS(SGraphPinCustomAsset) {} SLATE_END_ARGS() void Construct(const FArguments& InArgs, UEdGraphPin* InPin); protected: virtual FSlateColor GetPinColor() const override { return FSlateColor(FColor::Black); }; virtual TSharedRef<SWidget> GetDefaultValueWidget() override; void ColorPicked(FLinearColor SelectedColor); };
SGraphPinCustomAsset
in the .cpp
file:#include "UE4CookbookEditor.h" #include "SColorPicker.h" #include "SGraphPinCustomAsset.h" void SGraphPinCustomAsset::Construct(const FArguments& InArgs, UEdGraphPin* InPin) { SGraphPin::Construct(SGraphPin::FArguments(), InPin); } TSharedRef<SWidget> SGraphPinCustomAsset::GetDefaultValueWidget() { return SNew(SColorPicker) .OnColorCommitted(this, &SGraphPinCustomAsset::ColorPicked); } void SGraphPinCustomAsset::ColorPicked(FLinearColor SelectedColor) { UMyCustomAsset* NewValue = NewObject<UMyCustomAsset>(); NewValue->ColorName = SelectedColor.ToFColor(false).ToHex(); GraphPinObj->GetSchema()->TrySetDefaultObject(*GraphPinObj, NewValue); }
#include "Chapter8/MyCustomAssetDetailsCustomization.h"
to the UE4Cookbook
editor module implementation file.TSharedPtr<FMyCustomAssetPinFactory> PinFactory;
StartupModule()
:PinFactory = MakeShareable(new FMyCustomAssetPinFactory()); FEdGraphUtilities::RegisterVisualPinFactory(PinFactory);
ShutdownModule()
:FEdGraphUtilities::UnregisterVisualPinFactory(PinFactory); PinFactory.Reset();
MyCustomAsset
(Reference):FGraphPanelPinFactory
class.virtual TSharedPtr<class SGraphPin> CreatePin(class UEdGraphPin* Pin) const
CreatePin
, as the name implies, is to create a new visual representation of the graph pin.UEdGraphPin
instance. UEdGraphPin
contains information about the object that the pin represents so that our factory class can make an informed decision regarding which visual representation we should be displaying.PinSubCategoryObject
property, which contains a UClass
, and comparing it to the UClass
associated with our custom asset class.SGraphPinCustomAsset
, is the Slate Widget class, which is a visual representation of our object as a literal.SGraphPin
, the base class for all graph pins.SGraphPinCustomAsset
class has a Construct
function, which is called when the widget is created.GetPinColor()
and GetDefaultValueWidget()
.ColorPicked
, a handler for when a user selects a color in our custom pin.Construct
. GetDefaultValueWidget
is to actually create the widget that is the custom representation of our class, and return it to the engine code.SColorPicker
instance—we want the user to be able to select a color, and store the hex-based representation of that color inside the FString
property in our custom class.SColorPicker
instance has a property called OnColorCommitted
— this is a slate event that can be assigned to a function on an object instance.SColorPicker
, we link OnColorCommitted
to the ColorPicked
function on this current object, so it will be called if the user selects a new color.ColorPicked
function receives the selected color as an input parameter.NewObject
template function.SpawnActor
function discussed in other chapters, and initializes a new instance of the specified class before returning a pointer to it.ColorName
property. FLinearColors
can be converted to FColor
objects, which define a ToHex()
function that returns an FString
with the hexadecimal representation of the color that was selected on the new widget.GetSchema
function. This function returns the Schema for the graph that owns the node that contains our pin.PinFactory
class instance.StartupModule
, we create a new shared pointer that references an instance of our PinFactory
class.FEdGraphUtilities::RegisterVisualPinFactory(PinFactory)
to tell the engine to use our PinFactory
to create the visual representation.ShutdownModule
, we unregister the pin factory using UnregisterVisualPinFactory
.PinFactory
instance by calling Reset()
on the shared pointer that contains it.3.149.244.86