本文档指导开发者完成阿里云移动监控 Unity SDK 的接入配置,包含 SDK 下载、启动、验证等核心流程,支持 Android 和 iOS 平台。
一、前提条件
二、参考Demo
移动监控 Unity SDK接入工程样例请参见Demo工程。
三、将SDK添加到您的应用
下载 Unity SDK : https://ios-repo.oss-cn-shanghai.aliyuncs.com/unity/alicloud-apm/
${ApmVersion}
/alicloud-apm.unitypackage重要下载链接中的
${ApmVersion}
请从Unity SDK 发布说明中获取最新版本。下载的SDK不包含平台原生依赖包,需在平台构建过程中完成对应平台依赖管理。
双击或拖动下载的 unitypackage 包到您的 Unity 工程中 Assets 目录下,如下图所示
四、使用SDK
方式一:游戏场景加载前即启动(推荐)
using UnityEngine;
using Alicloud.Apm;
namespace Alicloud.Apm.QuickStart
{
internal static class ApmBootstrap
{
// 游戏加载前即启动,确保尽早采集
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void AutoStart()
{
var options = BuildOptions();
if (options == null)
{
Debug.Log("Alicloud.Apm not supported on this platform; startup skipped.");
return;
}
Apm.Start(options);
}
private static ApmOptions? BuildOptions()
{
#if UNITY_IOS
const string appKey = "Your iOS AppKey";
const string appSecret = "Your iOS AppSecret";
const string appRsaSecret = "Your iOS AppRsaSecret";
return new ApmOptions(appKey, appSecret, appRsaSecret)
{
// 可选配置项
UserNick = "Your UserNick",
UserId = "Your UserId",
};
#elif UNITY_ANDROID
const string appKey = "Your Android AppKey";
const string appSecret = "Your Android AppSecret";
const string appRsaSecret = "Your Android AppRsaSecret";
return new ApmOptions(appKey, appSecret, appRsaSecret)
{
// 可选配置项
UserNick = "Your UserNick",
UserId = "Your UserId",
};
#else
return null;
#endif
}
}
}
方式二:任意脚本文件启动
脚本文件需挂载到任意启动场景GameObject上
using UnityEngine;
using Alicloud.Apm;
namespace Alicloud.Apm.QuickStart
{
public sealed class YourScript : MonoBehaviour
{
private void Awake()
{
var options = BuildOptions();
if (options == null)
{
Debug.Log("Alicloud.Apm not supported on this platform; startup skipped.");
return;
}
Apm.Start(options);
}
private static ApmOptions? BuildOptions()
{
#if UNITY_IOS
const string appKey = "Your iOS AppKey";
const string appSecret = "Your iOS AppSecret";
const string appRsaSecret = "Your iOS AppRsaSecret";
return new ApmOptions(appKey, appSecret, appRsaSecret)
{
// 可选配置项
UserNick = "Your UserNick",
UserId = "Your UserId",
};
#elif UNITY_ANDROID
const string appKey = "Your Android AppKey";
const string appSecret = "Your Android AppSecret";
const string appRsaSecret = "Your Android AppRsaSecret";
return new ApmOptions(appKey, appSecret, appRsaSecret)
{
// 可选配置项
UserNick = "Your UserNick",
UserId = "Your UserId",
};
#else
return null;
#endif
}
}
}
五、接入验证
1. Unity 平台构建
Android
SDK 通过 Maven 实现 android 原生依赖包的管理,默认会在Unity完成Gradle项目生成后添加阿里云Maven仓库、Android崩溃分析SDK依赖和proguard规则,如下 Unity 控制台构建日志表明 Android 原生依赖包安装完成。
[AlicloudApm.Editor] ApmAndroidGradleDependencies completed Android Gradle post-processing.
重要考虑到本地开发环境的差异性,SDK自动添加阿里云Maven仓库、Android崩溃分析SDK依赖和proguard规则可能会失败,请参考Android SDK接入手动在Unity的模板文件中添加。
iOS
SDK 通过 CocoaPods 实现 iOS 原生依赖包的管理,默认会在 iOS 平台构建过程中执行
pod install
安装原生依赖包,如下 Unity 控制台构建日志表明 iOS 原生依赖包安装完成。
[AlicloudApm.Editor] `pod install` completed successfully
考虑到本地开发环境的差异性,pod install
执行失败时,需要您手动安装原生依赖包,Podfile文件内容参照如下,AlicloudApmCBridge
的版本号${AlicloudApmCBridgeVersion}
请从aliyun-specs获取最新版本。
source 'https://github.com/aliyun/aliyun-specs.git'
source 'https://cdn.cocoapods.org/'
platform :ios, '12.0'
use_frameworks! :linkage => :static
target 'UnityFramework' do
pod 'AlicloudApmCBridge', '~> ${AlicloudApmCBridgeVersion}'
end
target 'Unity-iPhone' do
use_frameworks! :linkage => :static
inherit! :complete
end
2. 平台工具构建
Android
对于 Android,请查看 Android Studio Logcat输出,检查是否有以下日志信息:
Apm I Device unlocked: initializing Apm Apm-CrashAnalysis I Initializing Apm Crash Analysis 3.5.0 for com.UnityTechnologies.com.unity.template.urpblank Apm-CrashAnalysis I Initializing CrashAnalysis blocked main for 10 ms Apm I Apm initialization successful
此日志表明 崩溃分析SDK 在 Native 层已成功启动。
iOS
对于 iOS,请查看 Xcode 控制台输出,检查是否有以下日志信息:
[AlicloudApmCore] Started Successfully
[AlicloudApmSetting] Fetched Successfully
此日志表明 SDK 在 Native 层已成功启动,并且拉取配置成功。