गेम के लोड होने से जुड़े इवेंट को रिकॉर्ड करना दो वजहों से ज़रूरी है:
- लोड होने के दौरान, फ़्रेम टाइम के डेटा को खराब होने से बचाने के लिए.
- लोड होने में लगने वाले समय का विश्लेषण करना, ताकि यह पता लगाया जा सके कि लोड होने में लगने वाला समय कब और कहां स्वीकार किए जाने वाले समय से ज़्यादा है.
लोडिंग इवेंट से जुड़ा मेटाडेटा हो सकता है:
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);
यह फ़ंक्शन, दिए गए मेटाडेटा और एनोटेशन से जुड़े लोडिंग ग्रुप को शुरू करता है. साथ ही, Result<ulong>.value को भरता है, ताकि उसका इस्तेमाल StopLoadingGroup() फ़ंक्शन में किया जा सके. फ़िलहाल, Play बैकएंड में मेटाडेटा और एनोटेशन का इस्तेमाल नहीं किया जाता. साथ ही, सिर्फ़ एनोटेशन को null पर सेट किया जा सकता है. इसके बाद के सभी लोडिंग इवेंट को एक यूनीक ग्रुप आईडी से टैग किया जाता है.
ErrorCode StopLoadingGroup(ulong handle);
यह फ़ंक्शन, StartLoadingGroup() से पहले शुरू किए गए लोडिंग ग्रुप को रोकता है.
इसके बाद लोड होने वाले इवेंट में तब तक कोई ग्रुप आईडी नहीं होगा, जब तक StartLoadingGroup() को फिर से कॉल नहीं किया जाता.
उदाहरण
यहां कुछ उदाहरण दिए गए हैं, जिनसे पता चलता है कि अपने गेम में लोड होने में लगने वाले समय से जुड़े फ़ंक्शन कैसे जोड़े जा सकते हैं.
फ़ाइल लोड होने से जुड़े इवेंट
यहां दिए गए कोड के उदाहरण में, आपके गेम में फ़ाइल लोड होने के इवेंट रिकॉर्ड करने का तरीका बताया गया है.
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);
}