UMG使用UE的取色器/吸色器 SColorPicker

创建一个UWidget,然后里面保存一个 SColorPicker的指针,然后监听它颜色改变的回调
.h

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

#pragma once

#include "CoreMinimal.h"
#include "Components/Widget.h"
#include "Widgets/Colors/SColorPicker.h"
#include "SimpleColorPicker.generated.h"

/**
 * 
 */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnColorChangeEvent, FLinearColor ,NewColor);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnColorConfirmEvent, FLinearColor, NewColor);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnColorCancleEvent, FLinearColor, NewColor);

UCLASS()
class MYCOLORPICKER_API USimpleColorPicker : public UWidget
{
    
    
        GENERATED_BODY()
public:
        USimpleColorPicker();

        UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "ColorPicker")
                bool bExpandAdvancedSection = true;

        void ColorChange(FLinearColor NewColor);

        UPROPERTY(BlueprintAssignable)
                FOnColorChangeEvent OnColorChange;

                // UWidget interface
        virtual void SynchronizeProperties() override;
        // End of UWidget interface
protected:
        //重写函数,用于生产slate对象
        virtual TSharedRef< SWidget > RebuildWidget() override;
        //~ Begin UVisual Interface  释放slate所用的资源
        virtual void ReleaseSlateResources(bool bReleaseChildren) override;
        //~ End UVisual Interface

private:
        //指针
        TSharedPtr<SColorPicker> m_MyColorPicker;
};

.cpp

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


#include "SimpleColorPicker.h"

USimpleColorPicker::USimpleColorPicker()
{
    
    
        //SColorPicker::OnColorPickerDestroyOverride.BindUObject(this, &USimpleColorPicker::ColorClose);
}

void USimpleColorPicker::ColorChange(FLinearColor NewColor)
{
    
    
        OnColorChange.Broadcast(NewColor);
}

void USimpleColorPicker::SynchronizeProperties()
{
    
    
        Super::SynchronizeProperties();

}

TSharedRef< SWidget > USimpleColorPicker::RebuildWidget()
{
    
    
        //TSharedRef<SColorPicker> CreatedColorPicker = SNew(SColorPicker)
         SAssignNew(m_MyColorPicker, SColorPicker)
                .ExpandAdvancedSection(bExpandAdvancedSection)
                 .PreColorCommitted(FOnLinearColorValueChanged::CreateUObject(this, &USimpleColorPicker::ColorChange))
                 ;

        return m_MyColorPicker.ToSharedRef();
}

void USimpleColorPicker::ReleaseSlateResources(bool bReleaseChildren)
{
    
    
        Super::ReleaseSlateResources(bReleaseChildren);

        m_MyColorPicker.Reset();

}

效果(和UE的取色器一模一样,只不过不是窗口打开了):
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41130251/article/details/127982840