添加加载时间记录函数

记录游戏执行加载的时间非常重要,原因有两个:

  1. 避免在加载时污染帧时间数据。
  2. 分析加载时间,看看加载时间何时何地超过可接受的范围。

加载事件可以具有关联的元数据:

public class LoadingTimeMetadata
{
    public enum LoadingState
    {
        Unknown = 0,

        /// <summary>
        ///     The first time the game is run.
        /// </summary>
        FirstRun = 1,

        /// <summary>
        ///     App is not backgrounded.
        /// </summary>
        ColdStart = 2,

        /// <summary>
        ///     App is backgrounded.
        /// </summary>
        WarmStart = 3,

        /// <summary>
        ///     App is backgrounded, least work needed.
        /// </summary>
        HotStart = 4,

        /// <summary>
        ///     Asset loading between levels.
        /// </summary>
        InterLevel = 5
    }

    public LoadingState state;

    public enum LoadingSource
    {
        UnknownSource = 0,

        /// <summary>
        ///     Uncompressing data.
        /// </summary>
        Memory = 1,

        /// <summary>
        ///     Reading assets from APK bundle.
        /// </summary>
        Apk = 2,

        /// <summary>
        ///     Reading assets from device storage.
        /// </summary>
        DeviceStorage = 3,

        /// <summary>
        ///     Reading assets from external storage, e.g. SD card.
        /// </summary>
        ExternalStorage = 4,

        /// <summary>
        ///     Loading assets from the network.
        /// </summary>
        Network = 5,

        /// <summary>
        ///     Shader compilation.
        /// </summary>
        ShaderCompilation = 6,

        /// <summary>
        ///     Time spent between process starting and onCreate.
        /// </summary>
        PreActivity = 7,

        /// <summary>
        ///     Total time spent between process starting and first render frame.
        /// </summary>
        FirstTouchToFirstFrame = 8,

        /// <summary>
        ///     Time from start to end of a group of events.
        /// </summary>
        TotalUserWaitForGroup = 9
    }

    public LoadingSource source;

    /// <summary>
    ///     0 = no compression, 100 = max compression
    /// </summary>
    public int compression_level;

    public enum NetworkConnectivity
    {
        Unknown = 0,
        Wifi = 1,
        CellularNetwork = 2
    }

    public NetworkConnectivity network_connectivity;

    /// <summary>
    ///     Bandwidth in bits per second.
    /// </summary>
    public ulong network_transfer_speed_bps;

    /// <summary>
    ///     Latency in nanoseconds.
    /// </summary>
    public ulong network_latency_ns;
}

任何与您的需求无关的字段都可以为零。

加载事件还可以具有关联的注释。您可以采用与帧时间注释相同的方式来定义,那就是使用 Annotation 消息中的一个或多个字段。

Result<ulong> StartRecordingLoadingTime(LoadingTimeMetadata eventMetadata, TAnnotation annotation);

此函数开始记录与给定的元数据和注释关联的加载时间事件,并填充要在 StopRecordingLoadingTime() 函数中使用的 Result<ulong>.value

ErrorCode StopRecordingLoadingTime(ulong handle);

此函数停止记录之前由 StartRecordingLoadingTime() 开始的事件。该事件会在下次会话刷新时上传。

加载组函数

在您的游戏中,您可以为用户看到的一个加载期间记录多个加载事件。一些示例包括文件加载、解压缩和着色器编译。

务必告知 Android Performance Tuner,加载事件是此类组的一部分,以便它可以提供更好的数据分析。为此,请将加载事件与以下开始和停止函数括在一起。

Result<ulong> StartLoadingGroup(LoadingTimeMetadata eventMetadata, TAnnotation annotation);

此函数开始与给定的元数据和注释关联的加载组,并填充要在 StopLoadingGroup() 函数中使用的 Result<ulong>.value。Play 后端当前不使用元数据和注释,您可以将其设置为 null。所有后续加载事件都会由唯一的组 ID 来标记。

ErrorCode StopLoadingGroup(ulong handle);

此函数停止之前由 StartLoadingGroup() 开始的加载组。后续加载事件将没有组 ID,直到再次调用 StartLoadingGroup()