栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

UE4 Android、IOS监听应用状态

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UE4 Android、IOS监听应用状态

文章目录
  • 前言
  • 一、CoreDelegates
  • 二、蓝图中使用
    • 1.PlatformGameInstance
    • 2.Application Lifecycle Component
  • 总结


前言

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


一、CoreDelegates

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

// 代码文件路径:**UE_4.26EngineSourceRuntimeCorePublicMiscCoreDelegates.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;

C++使用示例

// 以下代码摘取至引擎源码AndroidMediaPlayer模块.

// 添加绑定
ResumeHandle = FCoreDelegates::ApplicationHasEnteredForegroundDelegate.AddRaw(this, &FAndroidMediaPlayer::HandleApplicationWillEnterBackground);

-- 分割线 --

// 回调接收函数
void FAndroidMediaPlayer::HandleApplicationWillEnterBackground()
{
	// TODO ...
}
二、蓝图中使用 1.PlatformGameInstance

蓝图示例:
PlatformGameInstance继承自GameInstance,差别就在于前者提供能OS状态相关监听接口,如图

所以GameInstace可以配置使用PlatformGameInstance类型的.

2.Application Lifecycle Component
  • 可在任意Actor中添加该组件,使用更为方便自由,需要的模块添加并绑定相关事件即可。
    蓝图示例:

总结

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

~ 小尾巴 ~
文章内容有错误、理解不到位以及有更优方案,欢迎指正 补充 讨论!!! 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号