Unity Application Integration¶
Collect metrics data from Unity applications and analyze application performance through visualization.
Reading Path¶
- First-time Integration: Start with Quick Start
- Complete Integration: Continue reading this article
- Parameter Details: Refer to SDK Initialization, RUM Configuration, Log Configuration, Trace Configuration
- Custom Capabilities: Refer to Custom Tag Usage, Custom Data Collection Rules, Data Collection Masking
- Advanced Scenarios: Refer to Native and Unity Hybrid Development
- Troubleshooting: Refer to Troubleshooting
Prerequisites¶
Note
If the RUM Headless service has been activated, the prerequisites are automatically configured, and you can proceed directly with application integration.
- Install DataKit
- Configure the RUM Collector
- Configure DataKit to be publicly accessible and install the IP Geolocation Database
Application Integration¶
- Navigate to RUM > Create > Android/iOS
- Create two separate applications for Unity Android and Unity iOS to receive RUM data from the Android and iOS platforms respectively
- Enter the corresponding application name and application ID for each platform's application
-
Choose the application integration method:
- Public DataWay: Directly receives RUM data without installing the DataKit collector
- Local Environment Deployment: Receives RUM data after meeting the prerequisites
Installation¶
Source Code Address: https://github.com/GuanceCloud/datakit-unity
Demo Address: https://github.com/GuanceCloud/datakit-unity/blob/dev/Assets/Scenes
- Download the latest ft-sdk-unity.unitypackage
- Import
ft-sdk-unity.unitypackageviaAssets->Import Package->Custom Package... - Add the JSON parsing third-party library
"com.unity.nuget.newtonsoft-json", which can be done inPackage Manager->Add Package by name ... - Drag
FTSDK.prefabto the first scene page and initialize the SDK within the_InitSDKmethod inFTSDK.cs - Drag
FTViewObserver.prefabto other scene pages to monitor the pageViewlifecycle, as well as application suspension and resumption - Monitor and convert Unity crash data and regular log data via
Application.logMessageReceived. For examples, refer to Quick Start and Custom Data Collection Rules
Assets/Plugins
├── Android
│ ├── FTUnityBridge.java // Android bridge
│ ├── InnerClassProxy.java // Android Inner Setting Proxy
│ ├── ft-sdk-release.aar // Android SDK
│ ├── gson-2.8.5.jar // Android SDK dependent third-party library
├── iOS
│ ├── FTMobileSDK.xcframework // iOS SDK
│ ├── FTUnityBridge.mm // iOS bridge
├── FTSDK.cs // Script bound to FTSDK.prefab
├── FTSDK.prefab // SDK initialization prefab
├── FTUnityBridge.cs // Unity bridge connecting iOS, Android, and other platform methods
├── FTViewObserver.cs // Script bound to FTViewObserver.prefab
├── FTViewObserver.prefab // View page monitoring prefab
├── UnityMainThreadDispatcher.cs // Script bound to UnityMainThreadDispatcher.prefab
├── UnityMainThreadDispatcher.prefab // Main thread consumer queue prefab
- If the native Android and iOS projects have already integrated the native SDK, you need to comment out the
_InitSDKmethod to avoid duplicate settings. For specific scenarios, refer to Native and Unity Hybrid Development - iOS Plugin Inspector Settings:
FTMobileSDK.xcframeworkis a dynamic library, andAdd to Embedded Binariesmust be checked. Selecting this option will cause Unity to set the Xcode project options to copy the plugin files into the final application bundle.
If the native SDK is already integrated,
gson-2.8.5.jar,ft-sdk-release.aarfor Android, andFTMobileSDK.frameworkfor iOS can be removed from the Unity project. Android's OkHttp request and startup time-consuming features require the use offt-plugin. For detailed configuration, see Android SDK.
Initialization Instructions¶
For a minimal initialization example, read Quick Start.
For a complete explanation of SDKConfig parameters, read SDK Initialization.
Detailed Configuration Entries¶
Custom Data Collection Rules¶
The Unity SDK currently primarily implements custom RUM data collection through manual method calls.
Action¶
Usage¶
/// <summary>
/// Add an Action
/// </summary>
/// <param name="actionName">action name</param>
/// <param name="actionType">action type</param>
public static void StartAction(string actionName, string actionType)
/// <summary>
/// Add an Action
/// </summary>
/// <param name="actionName">action name</param>
/// <param name="actionType">action type</param>
/// <param name="property">additional property parameters</param>
public static void StartAction(string actionName, string actionType, Dictionary<string, object> property)
/// <summary>
/// Add an Action
/// </summary>
/// <param name="actionName">action name</param>
/// <param name="actionType">action type</param>
public static void AddAction(string actionName, string actionType)
/// <summary>
/// Add an Action
/// </summary>
/// <param name="actionName">action name</param>
/// <param name="actionType">action type</param>
/// <param name="property">additional property parameters</param>
public static void AddAction(string actionName, string actionType, Dictionary<string, object> property)
Code Example¶
View¶
Usage¶
/// <summary>
/// Start View
/// </summary>
/// <param name="viewName">current page name</param>
public static void StartView(string viewName)
/// <summary>
/// Start View
/// </summary>
/// <param name="viewName">current page name</param>
/// <param name="property">additional property parameters</param>
public static void StartView(string viewName, Dictionary<string, object> property)
/// <summary>
/// Stop View
/// </summary>
public static void StopView()
/// <summary>
/// Stop View
/// </summary>
/// <param name="property">additional property parameters</param>
public static void StopView(Dictionary<string, object> property)
Code Example¶
Resource¶
Usage¶
/// <summary>
/// Start resource
/// </summary>
/// <param name="resourceId">resource Id</param>
public static async Task StartResource(string resourceId)
/// <summary>
/// Start resource
/// </summary>
/// <param name="resourceId">resource Id</param>
/// <param name="property">additional property parameters</param>
public static async Task StartResource(string resourceId, Dictionary<string, object> property)
/// <summary>
/// Stop resource
/// </summary>
/// <param name="resourceId">resource Id</param>
public static async Task StopResource(string resourceId)
/// <summary>
/// Stop resource
/// </summary>
/// <param name="resourceId">resource Id</param>
/// <param name="property">additional property parameters</param>
public static async Task StopResource(string resourceId, Dictionary<string, object> property)
/// <summary>
/// Add network transmission content and metrics
/// </summary>
/// <param name="resourceId">resource Id</param>
/// <param name="resourceParams">data transmission content</param>
public static async Task AddResource(string resourceId, ResourceParams resourceParams)
ResourceParams¶
| Method Name | Type | Required | Description |
|---|---|---|---|
| url | string | Yes | url address |
| requestHeader | string | No | request header parameters, no format restrictions |
| responseHeader | string | No | response header parameters, no format restrictions |
| responseConnection | string | No | response connection |
| responseContentType | string | No | response ContentType |
| responseContentEncoding | string | No | response ContentEncoding |
| resourceMethod | string | No | request method GET, POST, etc. |
| responseBody | string | No | returned body content |
Code Example¶
FTUnityBridge.StartResource(resourceId);
FTUnityBridge.StopResource(resourceId);
ResourceParams resourceParams = new ResourceParams();
resourceParams.url = url;
resourceParams.requestHeader = client.DefaultRequestHeaders.ToDictionary(header => header.Key, header => string.Join(",", header.Value));
resourceParams.responseHeader = response.Headers.ToDictionary(header => header.Key, header => string.Join(",", header.Value));
resourceParams.resourceStatus = (int)response.StatusCode;
resourceParams.responseBody = responseData;
resourceParams.resourceMethod = "GET";
FTUnityBridge.AddResource(resourceId, resourceParams);
Error¶
Usage¶
/// <summary>
/// Add error information
/// </summary>
/// <param name="log">log</param>
/// <param name="message">message</param>
public static async Task AddError(string log, string message)
/// <summary>
/// Add error information
/// </summary>
/// <param name="log">log</param>
/// <param name="message">message</param>
/// <param name="property">additional property parameters</param>
public static async Task AddError(string log, string message, Dictionary<string, object> property)
Code Example¶
void OnEnable()
{
Application.logMessageReceived += LogCallBack;
}
void OnDisable()
{
Application.logMessageReceived -= LogCallBack;
}
void LogCallBack(string condition, string stackTrace, LogType type)
{
if (type == LogType.Exception)
{
FTUnityBridge.AddError(stackTrace, condition);
}
}
LongTask¶
Usage¶
/// <summary>
/// Add a long-duration task
/// </summary>
/// <param name="log">log content</param>
/// <param name="duration">duration, in nanoseconds</param>
public static async Task AddLongTask(string log, long duration)
/// <summary>
/// Add a long-duration task
/// </summary>
/// <param name="log">log content</param>
/// <param name="duration">duration, in nanoseconds</param>
/// <param name="property">additional property parameters</param>
public static async Task AddLongTask(string log, long duration, Dictionary<string, object> property)
Code Example¶
Advanced Scenarios¶
Frequently Asked Questions¶
Add Prefix to Variables to Avoid Conflicting Fields¶
To avoid conflicts between custom fields and SDK data, it is recommended to add a project abbreviation prefix to tag names, such as df_tag_name. The key values used in the project can be queried from the source code. When variables in the SDK global variables conflict with those in RUM or Log, the RUM and Log variables will override the global variables in the SDK.
