• UE4 Android、IOS监听应用状态



    前言

    移动端开发,有时候需要能知道应用启动、挂起等状态;熟悉移动端开发的朋友,以Android为例,可使用APL+JAVA自行实现OnPause、OnResume事件回调到C++或者蓝图中,当过程相对较为麻烦;
    实际上,UE已经提供此类回调接口,可很方便的进行使用。


    一、CoreDelegates

    Core层的代理委托
    代码如下,摘取部分接口,其中接口ApplicationWillEnterBackgroundDelegate相当于Android的OnPause, ApplicationHasEnteredForegroundDelegate相当于OnResume;
    具体接口详见代码文件。

    // 代码文件路径:**\UE_4.26\Engine\Source\Runtime\Core\Public\Misc\CoreDelegates.h
    
    // This is called when the application is being backgrounded (e.g., due to switching
    	// to another app or closing it via the home button)
    	// The game should release shared resources, save state, etc..., since it can be
    	// terminated from the background state without any further warning.
    	static FApplicationLifetimeDelegate ApplicationWillEnterBackgroundDelegate; // for instance, hitting the home button
    
    	// Called when the application is returning to the foreground (reverse any processing done in the EnterBackground delegate)
    	static FApplicationLifetimeDelegate ApplicationHasEnteredForegroundDelegate;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    C++使用示例

    // 以下代码摘取至引擎源码AndroidMediaPlayer模块.
    
    // 添加绑定
    ResumeHandle = FCoreDelegates::ApplicationHasEnteredForegroundDelegate.AddRaw(this, &FAndroidMediaPlayer::HandleApplicationWillEnterBackground);
    
    -- 分割线 --
    
    // 回调接收函数
    void FAndroidMediaPlayer::HandleApplicationWillEnterBackground()
    {
    	// TODO ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    二、蓝图中使用

    1.PlatformGameInstance

    蓝图示例:
    PlatformGameInstance继承自GameInstance,差别就在于前者提供能OS状态相关监听接口,如图
    在这里插入图片描述
    所以GameInstace可以配置使用PlatformGameInstance类型的.

    2.Application Lifecycle Component

    • 可在任意Actor中添加该组件,使用更为方便自由,需要的模块添加并绑定相关事件即可。
      蓝图示例:
      在这里插入图片描述

    总结

    C++、蓝图都有方法可以监听到这些事件;其中IOS还有低电量回调接口等,具体可以查看源码。

    ~ 小尾巴 ~
    文章内容有错误、理解不到位以及有更优方案,欢迎指正 补充 讨论!!! 
    
    • 1
    • 2
  • 相关阅读:
    大数据Doris(十二):扩容缩容
    git学习——第5节 远程仓库
    【Java】开发工具IntelliJ IDEA
    Python下课时间表:
    智云通CRM:CRM数据库在经营客户中有什么作用?
    基于SpringBoot+Vue前后端分离的学校心理健康测试系统
    数学建模学习笔记:层次分析法(AHP)
    C#结合OpenCVSharp4使用直方图算法比较图片相似度
    bootloader介绍
    飞书应用配置+蓝鲸流水线+jump server
  • 原文地址:https://blog.csdn.net/boonti/article/details/125462353