- First, we will update the MyCustomAsset class to be editable in Blueprints and reflect what we'll be doing in this recipe. Go to MyCustomAsset.h and update it to the following code:
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyCustomAsset.generated.h"
UCLASS(BlueprintType, EditInlineNew)
class CHAPTER_10_API UMyCustomAsset : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Custom Asset")
FString ColorName;
};
- From the Chapter_10Editor folder, create a new file called MyCustomAssetPinFactory.h .
- Inside the header, add the following code:
#pragma once
#include "EdGraphUtilities.h"
#include "MyCustomAsset.h"
#include "SGraphPinCustomAsset.h"
struct CHAPTER_10EDITOR_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;
}
};
};
- Create another header file called SGraphPinCustomAsset.h:
#pragma once
#include "SGraphPin.h"
class CHAPTER_10EDITOR_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);
};
- Implement SGraphPinCustomAsset by creating the .cpp file:
#include "SGraphPinCustomAsset.h"
#include "Chapter_10Editor.h"
#include "SColorPicker.h"
#include "MyCustomAsset.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);
}
- Regenerate your Visual Studio project.
- Add #include "MyCustomAssetPinFactory.h" to the Chapter_10Editor.h module implementation file.
- Add the following member to the editor module class (FChapter_10EditorModule):
TSharedPtr<FMyCustomAssetPinFactory> PinFactory;
- Open Chapter_10Editor.cpp and then add the following to StartupModule():
PinFactory = MakeShareable(new FMyCustomAssetPinFactory()); FEdGraphUtilities::RegisterVisualPinFactory(PinFactory);
- Also add the following code to ShutdownModule():
FEdGraphUtilities::UnregisterVisualPinFactory(PinFactory); PinFactory.Reset();
- Compile your code and launch the editor.
- Create a new Function inside of the Level Blueprint by clicking on the plus symbol beside Functions within the My Blueprint panel:
- Add an input parameter:
- Set its type to MyCustomAsset (Object Reference):
- In the Level Blueprint's Event Graph, place an instance of your new function and verify that the input pin now has a custom visualizer in the form of a color picker:
Newly added color picker visualizer