Integrate the Unity SDK

更新时间:
复制 MD 格式

This document describes how to integrate the Alibaba Cloud App Monitor Unity SDK. It covers the core steps, such as downloading, starting, and validating the SDK for Android and iOS.

1. Prerequisites

  • You have created an application in EMAS and obtained the credentials.

    • Log on to the EMAS console.

    • Create an Android or iOS application to obtain the AppKey, AppSecret, and AppRsaSecret.

    • For more information, see the Quick Start document.

  • Development environment requirements

    Android

    • Android 5.0 or later

    iOS

    • Xcode 14.0 or later

    • iOS 12.0 or later

    • CocoaPods 1.12.0 or later

2. Demo reference

  • For a sample project that demonstrates how to integrate the App Monitor Unity SDK, see the Demo project.

3. Add the SDK to your application

  • Download the Unity SDK: https://ios-repo.oss-cn-shanghai.aliyuncs.com/unity/alicloud-apm/${ApmVersion}/alicloud-apm.unitypackage

    Important
    • In the download URL, replace ${ApmVersion} with the latest version number from the Unity SDK release notes.

    • The downloaded SDK does not include native platform dependency packages. You must manage the dependencies for each platform during the build process.

  • Double-click the downloaded unitypackage file or drag it to the Assets folder in your Unity project, as shown in the following figure.

4. Use the SDK

Method 1: Start before the game scene loads (Recommended)

using UnityEngine;
using Alicloud.Apm;

namespace Alicloud.Apm.QuickStart
{
  internal static class ApmBootstrap
  {  
    // Start before the game loads to ensure early data collection.
    [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)
      {
        // Optional configuration items
        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)
      {
        // Optional configuration items
        UserNick = "Your UserNick",
        UserId = "Your UserId",
      };
      #else
      return null;
      #endif
    }
  }
}

Method 2: Start from any script file

  • Attach the script file to a GameObject in any startup scene.

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)
      {
        // Optional configuration items
        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)
      {
        // Optional configuration items
        UserNick = "Your UserNick",
        UserId = "Your UserId",
      };
      #else
      return null;
      #endif
    }
  }
}

5. Verify the integration

1. Build on the Unity platform

Android

  • The SDK uses Maven to manage native Android dependency packages. By default, after Unity generates the Gradle project, the SDK adds the Alibaba Cloud Maven repository, the Android Crash Analysis SDK dependency, and the ProGuard rules. The following build log from the Unity console shows that the native Android dependency packages are installed.

    [AlicloudApm.Editor] ApmAndroidGradleDependencies completed Android Gradle post-processing.
    Important

    Due to differences in local development environments, the SDK might fail to automatically add the Alibaba Cloud Maven repository, the Android Crash Analysis SDK dependency, and the ProGuard rules. If this happens, you must add them manually to the template file in Unity. For more information, see Integrate the Android SDK.

iOS

  • The SDK uses CocoaPods to manage native iOS dependency packages. By default, it runs pod install to install the native dependency packages during the iOS platform build process. The following build log from the Unity console shows that the native iOS dependency packages are installed.

[AlicloudApm.Editor] `pod install` completed successfully
Important

Due to differences in local development environments, the pod install command might fail. If this happens, you must install the native dependency packages manually. Use the following content for your Podfile. Replace ${AlicloudApmCBridgeVersion} with the latest version number for AlicloudApmCBridge from 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. Build with platform tools

Android

  • For Android, check the Android Studio Logcat output for the following log messages:

    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

    This log indicates that the Crash Analysis SDK has started successfully on the native layer.

iOS

  • For iOS, check the Xcode console output for the following log messages:

[AlicloudApmCore] Started Successfully
[AlicloudApmSetting] Fetched Successfully

This log indicates that the SDK has started successfully on the native layer and pulled the configuration.

6. Additional resources