Class LogAnalysis
Game service that orchestrates the entire data-collection pipeline in a scenario: triggering DataCollectors at fixed intervals, caching results in memory, persisting them to disk, and asynchronously uploading them via IDataSendService.
public class LogAnalysis : MonoBehaviour, IDataAnalysisService, IUnityGameService
Inheritance
Implements
Examples
The sequence below shows the full service-locator wiring used in a typical scene.
LogAnalysis self-registers as IDataAnalysisService in Awake,
so any DataCollector that starts after it can retrieve the service and
subscribe immediately. At scene end, EndSession triggers
GameInstance.LoadLevelSequenceCoroutine, which calls Action)
via the chained SubscribleDataSend event; the upload must complete before the
next scene loads.
// ── Scene setup (executed in Awake/Start order) ──────────────────────────────
// 1. LogAnalysis.Awake() — runs first because it lives on a persistent prefab.
// Registers itself so every later DataCollector can find it.
// UnityServiceLocator.Current.Register(this);
// 2. LogAnalysis.OnEnable() — hooks into GameMode so SendToServer is called
// automatically when the scene ends.
// gameMode.SubscribleDataSend(SendToServer);
// 3. DataCollector.OnEnable() on each actor — subscribes this collector so
// LogAnalysis polls it on each LateUpdate interval.
// logAnalysis.SubscribeForAnalysis(this); // called inside DataCollector
// ── Scene end (triggered by GameMode.EndSession()) ───────────────────────────
// 4. GameInstance fires SendDataToServerEvent; LogAnalysis.SendToServer receives
// the callback and begins the coroutine.
public class MySceneEndTrigger : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
// Calls GameInstance.LoadNextLevel() after all SubscribleDataSend
// listeners (including LogAnalysis.SendToServer) have finished.
if (UnityServiceLocator.Current.TryGet(out var mode))
mode.EndSession();
}
}
Remarks
Each driving scenario that needs data collection should have exactly one GameObject with an instance of this component attached. This class handles the lifecycle of gameplay analytics data, including:
- Collecting log entries from subscribed DataCollectors.
- Temporarily storing log data in an in-memory cache.
- Persisting cached data to disk to prevent data loss.
- Asynchronously transmitting collected data to a remote server.
Data is discarded from disk once the server acknowledges receipt.
The CacheKeyFormatPrefix namespaces disk keys so that partial logs from
different sessions do not overwrite each other when the save system uses a flat
key-value store (SaveDataFormat).
Methods
CollectFromDataCollectors(out GameplayAnalyticsDataLog)
Collects data from all the subscribed data collectors and stores the results in the provided log info.
public bool CollectFromDataCollectors(out GameplayAnalyticsDataLog logInfo)
Parameters
| Name | Type | Description |
|---|---|---|
logInfo | GameplayAnalyticsDataLog | The log information object that will hold the collected data. |
Returns
bool — True if data was successfully collected and stored in the log info, otherwise false.
Remarks
This method gathers the collected data from all the subscribed data collectors and adds the relevant information such as timestamp, game time, frame rate, and the collected logs. If no data is collected, the provided logInfo object will remain unmodified.
SendToServer(Action)
Sends the logs and cahce collected by LogAnalysis to the server asynchronously, invoking the provided callback upon completion.
public void SendToServer(Action callback)
Parameters
| Name | Type | Description |
|---|---|---|
callback | Action | The callback function to invoke after the data has been successfully sent to the server. |
Remarks
This method first ensures that any remaining cache is saved before initiating the data transmission process. It then starts an asynchronous coroutine to send the data to the server. The provided callback is invoked once the transmission completes.
SubscribeForAnalysis(DataCollector)
Subscribes a data collector for analysis if it meets the required conditions, such as being tagged correctly.
public void SubscribeForAnalysis(DataCollector collector)
Parameters
| Name | Type | Description |
|---|---|---|
collector | DataCollector | The data collector to subscribe. |
Remarks
The subscription will fail if the collector does not meet the validation criteria (e.g., the tags do not match).
UnsubscribeFromAnalysis(DataCollector)
Unsubscribes a data collector from the data analysis process.
public void UnsubscribeFromAnalysis(DataCollector collector)
Parameters
| Name | Type | Description |
|---|---|---|
collector | DataCollector | The data collector to unsubscribe. |
Remarks
After unsubscribing, the collector will no longer be part of the analysis process.