当前位置:   article > 正文

UE4 游戏窗口前台后台监听_ue4手游切前后台(android)

ue4手游切前后台(android)

【原创文章 转载请注明出处】

最近有个需求,记录在窗口切到后台和切回前台时的时间。找了很久,没有具体的方法,光看到有个委托事件也不知道怎么用。查资料+测试了1天,感谢各位大佬对UE 委托DELEGATE的介绍,现把我也把自己关于这部分功能的详细代码方上来,做个记录吧。

由于喜欢在蓝图中实现具体逻辑,所以有些代码可能会有些多余。

这部分比较乱完整的代码会在下面放出来。如果觉得有帮助请给我点个赞,哈哈

就不新建文件了,用自己的项目说了,

在CEditorGameInstance.h中

在开头出 通过委托宏自己定义2个动态多播返回值为viod的委托类型

  1. DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowActive);
  2. DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowDeActive);

在类中声明2个公有变量,开放给蓝图使用,之后会在蓝图中对这2个事件进行绑定。

  1. UPROPERTY(BlueprintAssignable)
  2. FOnWindowActive onWindowActivatedEvent;
  3. UPROPERTY(BlueprintAssignable)
  4. FOnWindowDeActive onWindowDeactivatedEvent;

之后就是获取窗口的通知,UE自带的窗口通知事件是由SWindow发起的,我们先参考一下有哪些类型 SWindow.h

  1. /** Notification that a window has been activated */
  2. DECLARE_DELEGATE( FOnWindowActivated );
  3. DECLARE_MULTICAST_DELEGATE( FOnWindowActivatedEvent );
  4. /** Notification that a window has been deactivated */
  5. DECLARE_DELEGATE( FOnWindowDeactivated );
  6. DECLARE_MULTICAST_DELEGATE( FOnWindowDeactivatedEvent );
  7. /** Notification that a window is about to be closed */
  8. DECLARE_DELEGATE_OneParam( FOnWindowClosed, const TSharedRef<SWindow>& );
  9. /** Notification that a window has been moved */
  10. DECLARE_DELEGATE_OneParam( FOnWindowMoved, const TSharedRef<SWindow>& );
  11. /** Override delegate for RequestDestroyWindow */
  12. DECLARE_DELEGATE_OneParam( FRequestDestroyWindowOverride, const TSharedRef<SWindow>& );
  13. /** Called when we need to switch game worlds for a window */
  14. DECLARE_DELEGATE_RetVal_OneParam( int32, FOnSwitchWorldHack, int32 );

我们需要用到的是这两个:

  1. DECLARE_DELEGATE( FOnWindowActivated );
  2. DECLARE_DELEGATE( FOnWindowDeactivated );

回到CEditorGameInstance.h中

在声明2个变量

  1. FOnWindowActivated onWindowActivedHandle;
  2. FOnWindowDeactivated onWindowDeactivedHandle;

这样需要的变量就已经定义完了,如图:

之后写绑定函数

  1. ​​UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Actived"), Category = "Application")
  2. void HandleOnWindowActived();//获取窗口被激活时的信息
  3. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Deactived"), Category = "Application")
  4. void HandleOnWindowDeactived();//获取窗口失效时的信息
  5. //获得各自信息后调用委托事件(这样蓝图中已经绑定事件的话就会收到信息)
  6. void OnRequestWindowActived();
  7. void OnRequestWindowDeactived();

 

  1. //---------------窗口激活时的绑定------------------
  2. void UCEditorGameInstance::HandleOnWindowActived()
  3. {
  4. //为Handle绑定函数
  5. onWindowActivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowActived);
  6. if (GEngine)
  7. {
  8. //为SWindow设置句柄
  9. TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
  10. Application->SetOnWindowActivated(onWindowActivedHandle);
  11. UE_LOG(LogTemp, Log, TEXT("Application request Actived is handled!"));
  12. }
  13. else
  14. {
  15. UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Actived request failed!"));
  16. }
  17. }
  18. void UCEditorGameInstance::OnRequestWindowActived()
  19. {
  20. UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowActived is handled!---dcf"));
  21. if (onWindowActivatedEvent.IsBound())
  22. {
  23. onWindowActivatedEvent.Broadcast();
  24. }
  25. }

 

  1. //---------------窗口失效时的绑定------------------
  2. void UCEditorGameInstance::HandleOnWindowDeactived()
  3. {
  4. onWindowDeactivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowDeactived);
  5. if (GEngine)
  6. {
  7. TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
  8. Application->SetOnWindowDeactivated(onWindowDeactivedHandle);
  9. UE_LOG(LogTemp, Log, TEXT("Application request Deactived is handled!"));
  10. }
  11. else
  12. {
  13. UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Deactived request failed!"));
  14. }
  15. }
  16. void UCEditorGameInstance::OnRequestWindowDeactived()
  17. {
  18. UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowDeactived is handled!---dcf"));
  19. if (onWindowDeactivatedEvent.IsBound())
  20. {
  21. onWindowDeactivatedEvent.Broadcast();
  22. }
  23. }

编译启动项目,到EditorGameInstance蓝图创建函数

 

到自己的项目的第一个Widget界面,初始化的时候调用刚才的方法(我的是登录界面Login_bp)

测试 启动游戏,点击其他区域 在点回窗口 看到日志已经打印出来了

 

完整的C++代码:

.h

  1. #include "Runtime/SlateCore/Public/Widgets/SWindow.h"
  2. DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowActive);
  3. DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowDeActive);
  4. UCLASS()
  5. class AJDR_API UCEditorGameInstance : public UGameInstance
  6. GENERATED_UCLASS_BODY()
  7. public:
  8. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Actived"), Category = "Application")
  9. void HandleOnWindowActived();
  10. void OnRequestWindowActived();
  11. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Deactived"), Category = "Application")
  12. void HandleOnWindowDeactived();
  13. void OnRequestWindowDeactived();
  14. public:
  15. FOnWindowActivated onWindowActivedHandle;
  16. FOnWindowDeactivated onWindowDeactivedHandle;
  17. UPROPERTY(BlueprintAssignable)
  18. FOnWindowActive onWindowActivatedEvent;
  19. UPROPERTY(BlueprintAssignable)
  20. FOnWindowDeActive onWindowDeactivatedEvent;

.cpp

  1. void UCEditorGameInstance::HandleOnWindowActived()
  2. {
  3. onWindowActivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowActived);
  4. if (GEngine)
  5. {
  6. TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
  7. Application->SetOnWindowActivated(onWindowActivedHandle);
  8. UE_LOG(LogTemp, Log, TEXT("Application request Actived is handled!"));
  9. }
  10. else
  11. {
  12. UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Actived request failed!"));
  13. }
  14. }
  15. void UCEditorGameInstance::OnRequestWindowActived()
  16. {
  17. UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowActived is handled!---dcf"));
  18. if (onWindowActivatedEvent.IsBound())
  19. {
  20. onWindowActivatedEvent.Broadcast();
  21. }
  22. }
  23. void UCEditorGameInstance::HandleOnWindowDeactived()
  24. {
  25. onWindowDeactivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowDeactived);
  26. if (GEngine)
  27. {
  28. TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
  29. Application->SetOnWindowDeactivated(onWindowDeactivedHandle);
  30. UE_LOG(LogTemp, Log, TEXT("Application request Deactived is handled!"));
  31. }
  32. else
  33. {
  34. UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Deactived request failed!"));
  35. }
  36. }
  37. void UCEditorGameInstance::OnRequestWindowDeactived()
  38. {
  39. UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowDeactived is handled!---dcf"));
  40. if (onWindowDeactivatedEvent.IsBound())
  41. {
  42. onWindowDeactivatedEvent.Broadcast();
  43. }
  44. }

 

 

 

 

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