publicclassLoadingTimeMetadata{publicenumLoadingState{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}publicLoadingStatestate;publicenumLoadingSource{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}publicLoadingSourcesource;/// <summary>/// 0 = no compression, 100 = max compression/// </summary>publicintcompression_level;publicenumNetworkConnectivity{Unknown=0,Wifi=1,CellularNetwork=2}publicNetworkConnectivitynetwork_connectivity;/// <summary>/// Bandwidth in bits per second./// </summary>publiculongnetwork_transfer_speed_bps;/// <summary>/// Latency in nanoseconds./// </summary>publiculongnetwork_latency_ns;}
publicRawImageimage;IEnumeratorLoadImageFromStreamingAssets(stringimageName){stringimagePath="file://"+Path.Combine(Application.streamingAssetsPath,imageName);using(varr=UnityWebRequestTexture.GetTexture(imagePath)){LoadingTimeMetadatafileLoadingMetadata=newLoadingTimeMetadata(){state=LoadingTimeMetadata.LoadingState.InterLevel,source=LoadingTimeMetadata.LoadingSource.DeviceStorage,// Fields are zero by default but they could be set as followscompression_level=0,network_connectivity=0,network_transfer_speed_bps=0,network_latency_ns=0};Annotationannotation=newAnnotation(){Scene=Scene.MagicalForest};// Start recording loading time.Result<ulong>result=performanceTuner.StartRecordingLoadingTime(fileLoadingMetadata,annotation);yieldreturnr.SendWebRequest();// Stop recording loading time.performanceTuner.StopRecordingLoadingTime(result.value);if(r.isNetworkError||r.isHttpError){Debug.Log(r.error);}else{Texture2Dtex=DownloadHandlerTexture.GetContent(r);image.texture=tex;}}}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-26 (世界標準時間)。"],[],[],null,["It's important to record when your game is performing loading events for\ntwo reasons:\n\n1. To avoid polluting your frame time data while loading.\n2. To analyze load times to see when and where load times are longer than acceptable.\n\nA loading event can have associated metadata: \n\n public class LoadingTimeMetadata\n {\n public enum LoadingState\n {\n Unknown = 0,\n\n /// \u003csummary\u003e\n /// The first time the game is run.\n /// \u003c/summary\u003e\n FirstRun = 1,\n\n /// \u003csummary\u003e\n /// App is not backgrounded.\n /// \u003c/summary\u003e\n ColdStart = 2,\n\n /// \u003csummary\u003e\n /// App is backgrounded.\n /// \u003c/summary\u003e\n WarmStart = 3,\n\n /// \u003csummary\u003e\n /// App is backgrounded, least work needed.\n /// \u003c/summary\u003e\n HotStart = 4,\n\n /// \u003csummary\u003e\n /// Asset loading between levels.\n /// \u003c/summary\u003e\n InterLevel = 5\n }\n\n public LoadingState state;\n\n public enum LoadingSource\n {\n UnknownSource = 0,\n\n /// \u003csummary\u003e\n /// Uncompressing data.\n /// \u003c/summary\u003e\n Memory = 1,\n\n /// \u003csummary\u003e\n /// Reading assets from APK bundle.\n /// \u003c/summary\u003e\n Apk = 2,\n\n /// \u003csummary\u003e\n /// Reading assets from device storage.\n /// \u003c/summary\u003e\n DeviceStorage = 3,\n\n /// \u003csummary\u003e\n /// Reading assets from external storage, e.g. SD card.\n /// \u003c/summary\u003e\n ExternalStorage = 4,\n\n /// \u003csummary\u003e\n /// Loading assets from the network.\n /// \u003c/summary\u003e\n Network = 5,\n\n /// \u003csummary\u003e\n /// Shader compilation.\n /// \u003c/summary\u003e\n ShaderCompilation = 6,\n\n /// \u003csummary\u003e\n /// Time spent between process starting and onCreate.\n /// \u003c/summary\u003e\n PreActivity = 7,\n\n /// \u003csummary\u003e\n /// Total time spent between process starting and first render frame.\n /// \u003c/summary\u003e\n FirstTouchToFirstFrame = 8,\n\n /// \u003csummary\u003e\n /// Time from start to end of a group of events.\n /// \u003c/summary\u003e\n TotalUserWaitForGroup = 9\n }\n\n public LoadingSource source;\n\n /// \u003csummary\u003e\n /// 0 = no compression, 100 = max compression\n /// \u003c/summary\u003e\n public int compression_level;\n\n public enum NetworkConnectivity\n {\n Unknown = 0,\n Wifi = 1,\n CellularNetwork = 2\n }\n\n public NetworkConnectivity network_connectivity;\n\n /// \u003csummary\u003e\n /// Bandwidth in bits per second.\n /// \u003c/summary\u003e\n public ulong network_transfer_speed_bps;\n\n /// \u003csummary\u003e\n /// Latency in nanoseconds.\n /// \u003c/summary\u003e\n public ulong network_latency_ns;\n }\n\nAny fields that aren't relevant to your needs can be zero.\n\nA loading event can also have an associated annotation. You can define in the\nsame way as frame time annotations, using one or more fields in the `Annotation`\nmessage.\n\n[`Result\u003culong\u003e StartRecordingLoadingTime(LoadingTimeMetadata eventMetadata,\nTAnnotation\nannotation);`](/games/sdk/reference/performance-tuner/unity/class/google/android/performance-tuner/android-performance-tuner-t-fidelity-t-annotation-#startrecordingloadingtime)\n\nThis function starts recording a loading time event associated with the given\nmetadata and annotation, and fills in a `Result\u003culong\u003e.value` to be used in the\n`StopRecordingLoadingTime()` function.\n\n[`ErrorCode StopRecordingLoadingTime(ulong handle);`](/games/sdk/reference/performance-tuner/unity/class/google/android/performance-tuner/android-performance-tuner-t-fidelity-t-annotation-#stoprecordingloadingtime)\n\nThis function stops recording an event previously started by\n`StartRecordingLoadingTime()`. The event is uploaded at the next session flush.\n\nLoading group functions **Warning:** All loading events must be part of a loading group. See [the code example](#loading-group-function) for loading group functions.\n\nIn your game, you may record several loading events for a single loading period\nseen by the user. Some examples include [file loading](#file-loading), [scene\nloading](#scene-loading), decompression and shader compilation.\n\nIt is important to inform Android Performance Tuner that loading events are part\nof such a group so that it can provide better insights. Bracket your loading\nevents with the following start and stop functions in order to do this.\n| **Note:** Only one loading group can be active at a time.\n\n[`Result\u003culong\u003e StartLoadingGroup(LoadingTimeMetadata eventMetadata, TAnnotation\nannotation);`](/games/sdk/reference/performance-tuner/unity/class/google/android/performance-tuner/android-performance-tuner-t-fidelity-t-annotation-#startloadinggroup)\n\nThis function starts a loading group associated with the given metadata and\nannotation, and fills in a `Result\u003culong\u003e.value` to be used in the\n`StopLoadingGroup()` function. The metadata and annotation are currently not\nused by the Play backend but only the annotation can be set to `null`. All subsequent loading events\nare tagged by a unique group ID.\n\n[`ErrorCode StopLoadingGroup(ulong handle);`](/games/sdk/reference/performance-tuner/unity/class/google/android/performance-tuner/android-performance-tuner-t-fidelity-t-annotation-#stoploadinggroup)\n\nThis function stops a loading group previously started by `StartLoadingGroup()`.\nSubsequent loading events will not have a group ID until `StartLoadingGroup()`\nis called again.\n\n\n**Figure 1.** Example of the loading group.\n\nExamples\n\nHere are some examples of how to add loading time functions to your game.\n\nFile loading events\n\nThe following code example shows how to record file loading events in your game. \n\n public RawImage image;\n\n IEnumerator LoadImageFromStreamingAssets(string imageName)\n {\n string imagePath = \"file://\" + Path.Combine(Application.streamingAssetsPath, imageName);\n using (var r = UnityWebRequestTexture.GetTexture(imagePath))\n {\n LoadingTimeMetadata fileLoadingMetadata = new LoadingTimeMetadata()\n {\n state = LoadingTimeMetadata.LoadingState.InterLevel,\n source = LoadingTimeMetadata.LoadingSource.DeviceStorage,\n // Fields are zero by default but they could be set as follows\n compression_level = 0,\n network_connectivity = 0,\n network_transfer_speed_bps = 0,\n network_latency_ns = 0\n };\n Annotation annotation = new Annotation()\n {\n Scene = Scene.MagicalForest\n };\n // Start recording loading time.\n Result\u003culong\u003e result = performanceTuner.StartRecordingLoadingTime(fileLoadingMetadata, annotation);\n yield return r.SendWebRequest();\n // Stop recording loading time.\n performanceTuner.StopRecordingLoadingTime(result.value);\n if (r.isNetworkError || r.isHttpError)\n {\n Debug.Log(r.error);\n }\n else\n {\n Texture2D tex = DownloadHandlerTexture.GetContent(r);\n image.texture = tex;\n }\n }\n }\n\nScene loading events\n\nThe following code example shows how to record scene loading events in your\ngame. \n\n IEnumerator LoadScene(int sceneIndex)\n {\n LoadingTimeMetadata metadata = new LoadingTimeMetadata()\n {state = LoadingTimeMetadata.LoadingState.InterLevel};\n Annotation annotation = new Annotation() {Scene = (Scene) (sceneIndex + 1)};\n Result\u003culong\u003e result = performanceTuner.StartRecordingLoadingTime(metadata, annotation);\n AsyncOperation asyncSceneLoad = SceneManager.LoadSceneAsync(sceneIndex, LoadSceneMode.Single);\n while (!asyncSceneLoad.isDone)\n {\n yield return null;\n }\n\n performanceTuner.StopRecordingLoadingTime(result.value);\n }\n\nLoading group functions\n\nThe following code example shows how to add loading group functions to your\ngame. \n\n IEnumerator LoadImages()\n {\n LoadingTimeMetadata groupMetadata = new LoadingTimeMetadata()\n {\n state = LoadingTimeMetadata.LoadingState.InterLevel,\n source = LoadingTimeMetadata.LoadingSource.DeviceStorage,\n };\n Result\u003culong\u003e result = performanceTuner.StartLoadingGroup(groupMetadata, null);\n yield return StartCoroutine(LoadImageFromStreamingAssets(\"image1.jpeg\"));\n yield return StartCoroutine(LoadImageFromStreamingAssets(\"image2.jpeg\"));\n yield return StartCoroutine(LoadImageFromStreamingAssets(\"image3.jpeg\"));\n yield return StartCoroutine(LoadImageFromStreamingAssets(\"image4.jpeg\"));\n var stopErrorCode = performanceTuner.StopLoadingGroup(0);\n }"]]