当前位置:   article > 正文

UE4 C++ UMG框架搭建_ue4 c++ 创建模型

ue4 c++ 创建模型

B站教学链接:https://space.bilibili.com/449549424?spm_id_from=333.1007.0.0
一、基础UI

       UMG全程叫做Unreal Motion Graphics UI Designer,是一种开发UI的工具。使用Unreal来进行UI开发,主要有两种方式,一种是Slate,另一种就是UMG,UMG是对Slate的一种包装。使用Slate进行UI开发,难度还是非常大的,需要使用C++来进行页面设计,调试非常不方便。UMG对初级开发者就非常的友好,一种所见即所得的开发模式,逻辑也可以直接使用蓝图进行编写。

      虚幻引擎中分类分为两类:第一类是2DUI也就是显示在平面上的UI界面,第二类是3DUI显示在3D空间上的3D界面。

  2DUI:屏幕上看到的一些文字数字等

3DUI:3D空间上的UI


两者区别:

2DUI创建方式为:creatwidget节点

3DUI的创建方式是,先创建Actor,将Widget以组件的方式加载到Actor里面

二、常用的UI空间button、image、text

          

    第一部分:Anchors锚点可以设置控件的对其方式和锚点位置,尺寸大小等

    

    第二部分:Appearance外观可以设置控件的图片样式,正常的样式,点击的样式,放上去的样式,点击的声音等,每个控件都有自己的样式,可以根据自己的喜好设置

     

    第三部分:点击事件可以通过点击来实现不通的功能

    

三、案例

  创建C++接口 interface命名为StateInterface

 在C++里面写两个接口

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "UObject/Interface.h"
  5. #include "StateInterface.generated.h"
  6. // This class does not need to be modified.
  7. UINTERFACE(MinimalAPI)
  8. class UStateInterface : public UInterface
  9. {
  10. GENERATED_BODY()
  11. };
  12. /**
  13. *
  14. */
  15. class NEWOBJECT_API IStateInterface
  16. {
  17. GENERATED_BODY()
  18. // Add interface functions to this class. This is the class that will be inherited to implement this interface.
  19. public:
  20. virtual void EnterState() = 0;
  21. virtual void ExitState() = 0;
  22. };

C++创建UserWidget命名为BaseStateWidget

 继承接口,重写虚函数

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Blueprint/UserWidget.h"
  5. #include "StateInterface.h"
  6. #include "BaseStateWidget.generated.h"
  7. /**
  8. *
  9. */
  10. UCLASS()
  11. class NEWOBJECT_API UBaseStateWidget : public UUserWidget,public IStateInterface
  12. {
  13. GENERATED_BODY()
  14. public:
  15. virtual void EnterState() override;
  16. virtual void ExitState() override;
  17. UFUNCTION(BlueprintNativeEvent,BlueprintCallable,Category = "State Pattern")
  18. void OnEnterState();
  19. UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "State Pattern")
  20. void OnExitState();
  21. };
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "BaseStateWidget.h"
  3. void UBaseStateWidget::EnterState()
  4. {
  5. OnEnterState();
  6. }
  7. void UBaseStateWidget::ExitState()
  8. {
  9. OnExitState();
  10. }
  11. void UBaseStateWidget::OnEnterState_Implementation()
  12. {
  13. AddToViewport();
  14. }
  15. void UBaseStateWidget::OnExitState_Implementation()
  16. {
  17. RemoveFromParent();
  18. }

C++创建Actor命名为UIStateManager

  

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "GameFramework/Actor.h"
  5. #include "BaseStateWidget.h"
  6. #include "Kismet/GameplayStatics.h"
  7. #include "UIStateManager.generated.h"
  8. UCLASS()
  9. class NEWOBJECT_API AUIStateManager : public AActor
  10. {
  11. GENERATED_BODY()
  12. public:
  13. // Sets default values for this actor's properties
  14. AUIStateManager();
  15. protected:
  16. // Called when the game starts or when spawned
  17. virtual void BeginPlay() override;
  18. public:
  19. // Called every frame
  20. virtual void Tick(float DeltaTime) override;
  21. UFUNCTION(BlueprintCallable, Category = "State Pattern")
  22. void EnterState(TSubclassOf<UBaseStateWidget> StateWidgetClass);
  23. UFUNCTION(BlueprintCallable, Category = "State Pattern")
  24. void ExitAllState();
  25. UPROPERTY(BlueprintReadWrite,Category = "State Pattern")
  26. UBaseStateWidget* CurrentStateWidget;
  27. private:
  28. TMap<TSubclassOf<UBaseStateWidget>,UBaseStateWidget*> WidgetIntsance;
  29. };
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "UIStateManager.h"
  3. // Sets default values
  4. AUIStateManager::AUIStateManager()
  5. {
  6. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  7. PrimaryActorTick.bCanEverTick = true;
  8. }
  9. // Called when the game starts or when spawned
  10. void AUIStateManager::BeginPlay()
  11. {
  12. Super::BeginPlay();
  13. }
  14. // Called every frame
  15. void AUIStateManager::Tick(float DeltaTime)
  16. {
  17. Super::Tick(DeltaTime);
  18. }
  19. void AUIStateManager::EnterState(TSubclassOf<UBaseStateWidget> StateWidgetClass)
  20. {
  21. if (CurrentStateWidget != nullptr)
  22. {
  23. CurrentStateWidget->ExitState();
  24. }
  25. if (WidgetIntsance.Contains(StateWidgetClass))
  26. {
  27. CurrentStateWidget = WidgetIntsance.FindRef(StateWidgetClass);
  28. }
  29. else
  30. {
  31. APlayerController* PC = UGameplayStatics::GetPlayerController(GetWorld(),0);
  32. CurrentStateWidget = CreateWidget<UBaseStateWidget>(PC,StateWidgetClass);
  33. WidgetIntsance.Add(StateWidgetClass,CurrentStateWidget);
  34. }
  35. CurrentStateWidget->EnterState();
  36. }
  37. void AUIStateManager::ExitAllState()
  38. {
  39. for (auto& Elem:WidgetIntsance)
  40. {
  41. (Elem.Value)->ExitState();
  42. }
  43. }

创建UMG继承自刚刚创建的BaseStateWidget

 

 样式你可以自己设计

创建蓝图

 

打开

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/894951
推荐阅读
相关标签
  

闽ICP备14008679号