赞
踩
【原创文章 转载请注明出处】
最近有个需求,记录在窗口切到后台和切回前台时的时间。找了很久,没有具体的方法,光看到有个委托事件也不知道怎么用。查资料+测试了1天,感谢各位大佬对UE 委托DELEGATE的介绍,现把我也把自己关于这部分功能的详细代码方上来,做个记录吧。
由于喜欢在蓝图中实现具体逻辑,所以有些代码可能会有些多余。
这部分比较乱完整的代码会在下面放出来。如果觉得有帮助请给我点个赞,哈哈
就不新建文件了,用自己的项目说了,
在CEditorGameInstance.h中
在开头出 通过委托宏自己定义2个动态多播返回值为viod的委托类型
- DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowActive);
- DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowDeActive);
在类中声明2个公有变量,开放给蓝图使用,之后会在蓝图中对这2个事件进行绑定。
- UPROPERTY(BlueprintAssignable)
- FOnWindowActive onWindowActivatedEvent;
- UPROPERTY(BlueprintAssignable)
- FOnWindowDeActive onWindowDeactivatedEvent;
之后就是获取窗口的通知,UE自带的窗口通知事件是由SWindow发起的,我们先参考一下有哪些类型 SWindow.h
- /** Notification that a window has been activated */
- DECLARE_DELEGATE( FOnWindowActivated );
- DECLARE_MULTICAST_DELEGATE( FOnWindowActivatedEvent );
-
- /** Notification that a window has been deactivated */
- DECLARE_DELEGATE( FOnWindowDeactivated );
- DECLARE_MULTICAST_DELEGATE( FOnWindowDeactivatedEvent );
-
- /** Notification that a window is about to be closed */
- DECLARE_DELEGATE_OneParam( FOnWindowClosed, const TSharedRef<SWindow>& );
-
- /** Notification that a window has been moved */
- DECLARE_DELEGATE_OneParam( FOnWindowMoved, const TSharedRef<SWindow>& );
-
- /** Override delegate for RequestDestroyWindow */
- DECLARE_DELEGATE_OneParam( FRequestDestroyWindowOverride, const TSharedRef<SWindow>& );
-
- /** Called when we need to switch game worlds for a window */
- DECLARE_DELEGATE_RetVal_OneParam( int32, FOnSwitchWorldHack, int32 );
我们需要用到的是这两个:
- DECLARE_DELEGATE( FOnWindowActivated );
- DECLARE_DELEGATE( FOnWindowDeactivated );
回到CEditorGameInstance.h中
在声明2个变量
- FOnWindowActivated onWindowActivedHandle;
- FOnWindowDeactivated onWindowDeactivedHandle;
这样需要的变量就已经定义完了,如图:
之后写绑定函数
- UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Actived"), Category = "Application")
- void HandleOnWindowActived();//获取窗口被激活时的信息
- UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Deactived"), Category = "Application")
- void HandleOnWindowDeactived();//获取窗口失效时的信息
- //获得各自信息后调用委托事件(这样蓝图中已经绑定事件的话就会收到信息)
- void OnRequestWindowActived();
- void OnRequestWindowDeactived();
- //---------------窗口激活时的绑定------------------
- void UCEditorGameInstance::HandleOnWindowActived()
- {
- //为Handle绑定函数
- onWindowActivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowActived);
- if (GEngine)
- {
- //为SWindow设置句柄
- TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
- Application->SetOnWindowActivated(onWindowActivedHandle);
-
- UE_LOG(LogTemp, Log, TEXT("Application request Actived is handled!"));
- }
- else
- {
- UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Actived request failed!"));
- }
- }
-
- void UCEditorGameInstance::OnRequestWindowActived()
- {
- UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowActived is handled!---dcf"));
- if (onWindowActivatedEvent.IsBound())
- {
- onWindowActivatedEvent.Broadcast();
- }
- }
- //---------------窗口失效时的绑定------------------
- void UCEditorGameInstance::HandleOnWindowDeactived()
- {
- onWindowDeactivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowDeactived);
- if (GEngine)
- {
- TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
- Application->SetOnWindowDeactivated(onWindowDeactivedHandle);
-
- UE_LOG(LogTemp, Log, TEXT("Application request Deactived is handled!"));
- }
- else
- {
- UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Deactived request failed!"));
- }
- }
-
- void UCEditorGameInstance::OnRequestWindowDeactived()
- {
- UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowDeactived is handled!---dcf"));
- if (onWindowDeactivatedEvent.IsBound())
- {
- onWindowDeactivatedEvent.Broadcast();
- }
- }
编译启动项目,到EditorGameInstance蓝图创建函数
到自己的项目的第一个Widget界面,初始化的时候调用刚才的方法(我的是登录界面Login_bp)
测试 启动游戏,点击其他区域 在点回窗口 看到日志已经打印出来了
完整的C++代码:
.h
- #include "Runtime/SlateCore/Public/Widgets/SWindow.h"
-
- DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowActive);
- DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWindowDeActive);
-
- UCLASS()
- class AJDR_API UCEditorGameInstance : public UGameInstance
- {
- GENERATED_UCLASS_BODY()
-
- public:
- UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Actived"), Category = "Application")
- void HandleOnWindowActived();
- void OnRequestWindowActived();
- UFUNCTION(BlueprintCallable, meta = (DisplayName = "Handle Application Deactived"), Category = "Application")
- void HandleOnWindowDeactived();
- void OnRequestWindowDeactived();
-
-
- public:
- FOnWindowActivated onWindowActivedHandle;
- FOnWindowDeactivated onWindowDeactivedHandle;
- UPROPERTY(BlueprintAssignable)
- FOnWindowActive onWindowActivatedEvent;
- UPROPERTY(BlueprintAssignable)
- FOnWindowDeActive onWindowDeactivatedEvent;
-
- }
.cpp
- void UCEditorGameInstance::HandleOnWindowActived()
- {
- onWindowActivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowActived);
- if (GEngine)
- {
- TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
- Application->SetOnWindowActivated(onWindowActivedHandle);
-
- UE_LOG(LogTemp, Log, TEXT("Application request Actived is handled!"));
- }
- else
- {
- UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Actived request failed!"));
- }
- }
-
- void UCEditorGameInstance::OnRequestWindowActived()
- {
- UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowActived is handled!---dcf"));
- if (onWindowActivatedEvent.IsBound())
- {
- onWindowActivatedEvent.Broadcast();
- }
- }
-
- void UCEditorGameInstance::HandleOnWindowDeactived()
- {
- onWindowDeactivedHandle.BindUObject(this, &UCEditorGameInstance::OnRequestWindowDeactived);
- if (GEngine)
- {
- TSharedPtr<SWindow> Application = GEngine->GameViewport->GetWindow();
- Application->SetOnWindowDeactivated(onWindowDeactivedHandle);
-
- UE_LOG(LogTemp, Log, TEXT("Application request Deactived is handled!"));
- }
- else
- {
- UE_LOG(LogTemp, Warning, TEXT("Can not get viewport reference, handle application Deactived request failed!"));
- }
- }
-
- void UCEditorGameInstance::OnRequestWindowDeactived()
- {
- UE_LOG(LogTemp, Log, TEXT("Application request OnRequestWindowDeactived is handled!---dcf"));
- if (onWindowDeactivatedEvent.IsBound())
- {
- onWindowDeactivatedEvent.Broadcast();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。