• 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
  • 相关阅读:
    Hive SQL案例
    [C++从入门到精通] 11.回顾类内初始化、默认构造函数、=default
    【Transformers】第 2 章:主题的实践介绍
    VMware各版本镜像下载站
    Qt程序打包成安装包exe
    OFDM深入学习及MATLAB仿真
    【最终省二】全国大学生数学建模大赛-参赛经历
    多模数据库 | 星环科技多模数据库ArgoDB“一库多用“,构建高性能湖仓集一体平台
    SpringCloud入门学习
    【学习笔记】CF1713F Lost Array
  • 原文地址:https://blog.csdn.net/boonti/article/details/125462353