เพิ่มฟังก์ชันการบันทึกเวลาที่ใช้ในการโหลด

คุณควรบันทึกเมื่อเกมกำลังดำเนินการโหลดด้วยเหตุผล 2 ประการต่อไปนี้

  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;
}

ฟิลด์ใดก็ตามที่ไม่เกี่ยวข้องกับความต้องการของคุณสามารถเป็น 0 ได้

เหตุการณ์การโหลดอาจมีคำอธิบายประกอบที่เชื่อมโยงด้วย คุณสามารถกำหนดได้ในลักษณะเดียวกับคำอธิบายประกอบเวลาเฟรม โดยใช้ฟิลด์อย่างน้อย 1 รายการในAnnotation ข้อความ

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

ฟังก์ชันนี้จะเริ่มบันทึกเหตุการณ์เวลาในการโหลดที่เชื่อมโยงกับข้อมูลเมตาและคำอธิบายประกอบที่ระบุ และกรอก Result<ulong>.value เพื่อใช้ในฟังก์ชัน StopRecordingLoadingTime()

ErrorCode StopRecordingLoadingTime(ulong handle);

ฟังก์ชันนี้จะหยุดบันทึกเหตุการณ์ที่เริ่มต้นก่อนหน้านี้โดย StartRecordingLoadingTime() ระบบจะอัปโหลดเหตุการณ์เมื่อล้างข้อมูลเซสชันครั้งถัดไป

กำลังโหลดฟังก์ชันกลุ่ม

ในเกม คุณอาจบันทึกเหตุการณ์การโหลดหลายรายการสำหรับระยะเวลาการโหลดเดียว ที่ผู้ใช้เห็น ตัวอย่างเช่น การโหลดไฟล์, การโหลดฉาก, การคลายการบีบอัด และการคอมไพล์ Shader

คุณควรแจ้งให้ Android Performance Tuner ทราบว่าเหตุการณ์การโหลดเป็นส่วนหนึ่งของกลุ่มดังกล่าวเพื่อให้ได้รับข้อมูลเชิงลึกที่ดีขึ้น ใส่วงเล็บเหตุการณ์การโหลด ด้วยฟังก์ชันเริ่มต้นและหยุดต่อไปนี้เพื่อดำเนินการนี้

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

ฟังก์ชันนี้จะเริ่มกลุ่มการโหลดที่เชื่อมโยงกับข้อมูลเมตาและ คำอธิบายประกอบที่ระบุ และกรอกข้อมูลใน Result<ulong>.value เพื่อใช้ในฟังก์ชัน StopLoadingGroup() ปัจจุบันแบ็กเอนด์ของ Play ไม่ได้ใช้ข้อมูลเมตาและคำอธิบายประกอบ แต่จะตั้งค่าคำอธิบายประกอบเป็น null ได้เท่านั้น เหตุการณ์การโหลดทั้งหมดที่ตามมา จะได้รับการติดแท็กด้วยรหัสกลุ่มที่ไม่ซ้ำกัน

ErrorCode StopLoadingGroup(ulong handle);

ฟังก์ชันนี้จะหยุดกลุ่มการโหลดที่เริ่มต้นก่อนหน้านี้โดย StartLoadingGroup() เหตุการณ์การโหลดที่ตามมาจะไม่มีรหัสกลุ่มจนกว่าจะมีการเรียกใช้ StartLoadingGroup() อีกครั้ง

รูปที่ 1 ตัวอย่างกลุ่มการโหลด

ตัวอย่าง

ตัวอย่างวิธีเพิ่มฟังก์ชันเวลาในการโหลดลงในเกมมีดังนี้

เหตุการณ์การโหลดไฟล์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกเหตุการณ์การโหลดไฟล์ในเกม

public RawImage image;

IEnumerator LoadImageFromStreamingAssets(string imageName)
{
    string imagePath = "file://" + Path.Combine(Application.streamingAssetsPath, imageName);
    using (var r = UnityWebRequestTexture.GetTexture(imagePath))
    {
        LoadingTimeMetadata fileLoadingMetadata = new LoadingTimeMetadata()
        {
            state = LoadingTimeMetadata.LoadingState.InterLevel,
            source = LoadingTimeMetadata.LoadingSource.DeviceStorage,
            // Fields are zero by default but they could be set as follows
            compression_level = 0,
            network_connectivity = 0,
            network_transfer_speed_bps = 0,
            network_latency_ns = 0
        };
        Annotation annotation = new Annotation()
        {
            Scene = Scene.MagicalForest
        };
        // Start recording loading time.
        Result<ulong> result = performanceTuner.StartRecordingLoadingTime(fileLoadingMetadata, annotation);
        yield return r.SendWebRequest();
        // Stop recording loading time.
        performanceTuner.StopRecordingLoadingTime(result.value);
        if (r.isNetworkError || r.isHttpError)
        {
            Debug.Log(r.error);
        }
        else
        {
            Texture2D tex = DownloadHandlerTexture.GetContent(r);
            image.texture = tex;
        }
    }
}

เหตุการณ์การโหลดฉาก

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกเหตุการณ์การโหลดฉากในเกม

IEnumerator LoadScene(int sceneIndex)
{
    LoadingTimeMetadata metadata = new LoadingTimeMetadata()
        {state = LoadingTimeMetadata.LoadingState.InterLevel};
    Annotation annotation = new Annotation() {Scene = (Scene) (sceneIndex + 1)};
    Result<ulong> result = performanceTuner.StartRecordingLoadingTime(metadata, annotation);
    AsyncOperation asyncSceneLoad = SceneManager.LoadSceneAsync(sceneIndex, LoadSceneMode.Single);
    while (!asyncSceneLoad.isDone)
    {
        yield return null;
    }

    performanceTuner.StopRecordingLoadingTime(result.value);
}

กำลังโหลดฟังก์ชันกลุ่ม

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟังก์ชันกลุ่มการโหลดลงในเกม

IEnumerator LoadImages()
{
    LoadingTimeMetadata groupMetadata = new LoadingTimeMetadata()
    {
        state = LoadingTimeMetadata.LoadingState.InterLevel,
        source = LoadingTimeMetadata.LoadingSource.DeviceStorage,
    };
    Result<ulong> result = performanceTuner.StartLoadingGroup(groupMetadata, null);
    yield return StartCoroutine(LoadImageFromStreamingAssets("image1.jpeg"));
    yield return StartCoroutine(LoadImageFromStreamingAssets("image2.jpeg"));
    yield return StartCoroutine(LoadImageFromStreamingAssets("image3.jpeg"));
    yield return StartCoroutine(LoadImageFromStreamingAssets("image4.jpeg"));
    var stopErrorCode = performanceTuner.StopLoadingGroup(0);
}