UWidget封装SWidgetUMG

2015年8月30日0

为了使用UMG中的一些高级或便利特性,需要将制作好的Slate控件封装UWidget中去。

当前UE4版本4.8.3。

将Slate封装UMG中去有很多的好处,因为在代码中对需要重用的控件进行不断的重新布局是一件非常繁琐的事情。同时,UMG也有在3D空间中进行显示这样的高级功能。

由于UMG本身就是对Slate的封装,所以这个过程可以参照UE4的源代码进行研究。

这里所做的是尝试对上一回所做的文件目录树进行封装

直接上代码

CListViewWidget.h

 
 
 
 
 
 

C++

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once
 
#include "CListViewWidget.generated.h"
 
 
UCLASS()
class TEST_MP_API UCListViewWidget : public UWidget
{
    GENERATED_UCLASS_BODY()
 
public:
 
#if WITH_EDITOR
    // UWidget interface
    virtual const FSlateBrush* GetEditorIcon() override;
    virtual const FText GetPaletteCategory() override;
    virtual void OnCreationFromPalette() override;
    // End UWidget interface
#endif
 
protected:
    // UWidget interface
    virtual TSharedRef<SWidget> RebuildWidget() override;
    virtual void OnBindingChanged(const FName& Property) override;
    // End of UWidget interface
 
protected:
    TSharedPtr<class SDDFileTree> MyFileTree;
};

封装类直接继承自UWidget即可,剩下的就是对UWidget中的函数进行实现。

CListViewWidget.cpp

 
 
 
 
 
 

C++

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Fill out your copyright notice in the Description page of Project Settings.
 
#include "Test_mp.h"
#include "SDDFileTree.h"
#include "CListViewWidget.h"
 
 
/** GENERATED_UCLASS_BODY() */
UCListViewWidget::UCListViewWidget(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    bIsVariable = false;
}
 
/** <编辑器属性 */
#if WITH_EDITOR
 
// <图标
const FSlateBrush* UCListViewWidget::GetEditorIcon()
{
    return FUMGStyle::Get().GetBrush("Widget.TextBlock");
}
 
// <分类目录
const FText UCListViewWidget::GetPaletteCategory()
{
    return NSLOCTEXT("Testmp", "Custom", "Ch_Custom");
}
 
// <创建时的默认值
void UCListViewWidget::OnCreationFromPalette()
{
    ;
}
 
#endif
 
TSharedRef<SWidget> UCListViewWidget::RebuildWidget()
{
    MyFileTree = SNew(SDDFileTree).OwnerHUD(nullptr);
 
    return MyFileTree.ToSharedRef();
}
 
void UCListViewWidget::OnBindingChanged(const FName& Property)
{
    Super::OnBindingChanged(Property);
 
    if (MyFileTree.IsValid())
    {
        ;
    }
}

在所有的函数中,RebuildWidget()是最重要的。

参看UWidget的源代码可以发现,所有对被封装SWidget的引用都是通过这个函数获得的,因此在这里面写上SWidget对应的构造生成就可以了。

OnBindingChanged函数是用于属性代理通知的,这里是简单的封装,没有提供属性代理,因此直接return也是可以的。

在WITH_EDITOR宏中的是编辑器属性,用于封装后的UWidget在蓝图编辑器中显示。具体每一个函数的含义已经在注释中了。

将上面的代码编译通过之后,就可以在UMG的编辑器中看到我们添加的控件了:

将CListViewWidget拖动控件蓝图上,并在HUD中展示此控件蓝图,就能在项目中与其他UMG控件一样使用了:

这样以来Slate的使用就会变得方便很多。

最新文章

  1. 1Z0-053 争议题目解析304
  2. Win7中怎么不经确认直接删除文件至回收站
  3. js 判断移动设备、pc端、android、iPhone、是否为微信、微博、qq空间
  4. Python开发【第十五篇】:Web框架之Tornado
  5. SQL错误码
  6. storm源码之storm代码结构【译】【转】
  7. Flink - Juggling with Bits and Bytes
  8. [原]在Fedora 20环境下安装系统内核源代码
  9. iOS开发——UI篇Swift篇&amp;玩转UItableView(三)分组功能
  10. POJ1328——Radar Installation
  11. DMG提取安装文件
  12. LA 3177 Beijing Guards(二分法 贪心)
  13. Android 更新UI的两个方法
  14. Spring注入静态变量(转)
  15. C# 真正能发邮件的源码
  16. python 单下划线/双下划线使用总结
  17. Js默认参数(多参数情况)
  18. JavaScript转unix时间戳
  19. 含服务端,客户端,数据库的注册/登录/聊天/在线/离线查看的聊天demo
  20. windows解决访问github慢问题

热门文章

  1. entityframework
  2. libvirt 网络手册(二):桥接网络
  3. KVM 虚拟机联网方式:NAT 和 Bridge
  4. CSS3学习基本记录
  5. 泛型DAO与泛型Service
  6. HBase 基本shell命令
  7. angularJs 控制文本长度,超出省略号过滤器
  8. python pip方式安装selenium
  9. [CodeWars][JS]如何判断给定的数字是否整数
  10. powershell例子