C++实现UMG中自定义不规则形状按钮

本文介绍了如何在Unreal Engine的UMG(Unreal Motion Graphics)中通过C++实现自定义不规则形状的按钮。通过修改'项目名称.Build.cs'文件,创建Button的子类,并实现特定的交互逻辑,使得按钮仅在指定图形区域响应鼠标点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


因为要在项目中使用UMG功能,所以找到项目中名称为 "项目名称.Build.cs" 文件,加入如下代码





创建一个Button的子类






生成的初始代码:

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

#pragma once

#include "Components/Button.h"
#include "CustomButton.generated.h"

/**
 * 
 */
UCLASS()
class LEARNUE4CPP_API UCustomButton : public UButton
{
	GENERATED_BODY()
};

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

#include "LearnUE4Cpp.h"
#include "CustomButton.h"

在上面代码的基础上修改后的代码:

//Custo
Using CVtxButton Implementing the CVtxButton class is surprisingly easy: Include Vtx.h, Vtx.ccp, VtxButton.h, and VtxButton.cpp in your project. Drop a button on your dialog in Developer Studio. Add #include "VtxButton.h" immediately before #include "MyDlg.h" in MyDlg.cpp and MyApp.cpp. For each button on the dialog that you want to be a CVtxButton, add CVtxButton m_cButton1; immediately after //{{AFX_DATA(CMyDlg) in the public section of your dialog class' specification. Also add DDX_Control(pDX, IDC_BUTTON1, m_cButton1); immediately after //{{AFX_DATA_MAP(CMyDlg) in DoDataExchange() for each button. This is enough to create a default CVtxButton. The button is drawn as a rectangle that takes up the entire client area. The sides will not be shaded the same as CButton because they are colored according to the difference of their angle and the angle of light source. If you use at least one CVtxButton on a dialog, it is suggested that you change all your CButton's to CVtxButton's to maintain a coordinated look. If you want to change the look of the CVtxBuUtton, there are two ways of doing so: Use a predefined shape by adding m_cButton1.SetVtx(VTX_RECT); to OnInitDialog() in MyDlg.cpp. There are four predefined shapes which can be passed as an argument to SetVtx(): VTX_RECT VTX_DIAMOND VTX_CIRCLE VTX_STRETCHEDCIRCLE Create a CVtxPolygons object and pass it as an argument to SetVtx(): CRect rect; m_cButton1.GetClientRect(&rect); // Get the button's original dimensions CVtxPolygons vtxPolygons; int offset[4] = {0, 1, 2, 4}; for (int i = 0; i < 4; i++) // Iterate through each of the polygons { // Add the corners vtxPolygons.Add(i, CVertex(rect.left + offset[i], rect.top + offset[i])); vtxPolygons.Add(i, CVertex(rect.right - offset[i] - 1, rect.top + offset[i])); vtxPolygons.Add(i, CVertex(rect.right - offset[i] - 1, rect.bottom - offset[i] - 1)); vtxPolygons.Add(i, CVertex(rect.left + offset[i], rect.bottom - offset[i]
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值