Cloud Storage के साथ Unity के लिए फ़ाइलें डाउनलोड करना

Cloud Storage for Firebase की मदद से, Firebase की ओर से उपलब्ध कराए गए और मैनेज किए जाने वाले Cloud Storage बकेट से फ़ाइलें तुरंत और आसानी से डाउनलोड की जा सकती हैं.

*.appspot.com

रेफ़रंस बनाना

कोई फ़ाइल डाउनलोड करने के लिए, सबसे पहले उस फ़ाइल का Cloud Storage रेफ़रंस बनाएं जिसे डाउनलोड करना है.

आप अपने Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर रेफ़रंस बना सकते हैं या किसी मौजूदा gs:// या https:// यूआरएल से रेफ़रंस बना सकते हैं जो Cloud Storage में किसी ऑब्जेक्ट को रेफ़रंस करता है.

// Create a reference with an initial file path and name
StorageReference pathReference =
    storage.GetReference("images/stars.jpg");

// Create a reference from a Google Cloud Storage URI
StorageReference gsReference =
    storage.GetReferenceFromUrl("gs://bucket/images/stars.jpg");

// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
StorageReference httpsReference =
    storage.GetReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");

फ़ाइलें डाउनलोड करना

रेफ़रंस मिलने के बाद, Cloud Storage से चार तरीकों से फ़ाइलें डाउनलोड की जा सकती हैं:

  1. यूआरएल से डाउनलोड करना
  2. बाइट ऐरे में डाउनलोड करना
  3. स्ट्रीम की मदद से डाउनलोड करना
  4. लोकल फ़ाइल में डाउनलोड करना

फ़ाइलें वापस पाने के लिए, इस्तेमाल किया जाने वाला तरीका इस बात पर निर्भर करेगा कि आपको अपने गेम में डेटा का इस्तेमाल कैसे करना है.

यूआरएल से डाउनलोड करना

अगर आपको Unity के WWW या UnityWebRequest के साथ किसी यूआरएल का इस्तेमाल करना है, तो GetDownloadUrlAsync() को कॉल करके, किसी फ़ाइल के लिए डाउनलोड यूआरएल पाया जा सकता है.

// Fetch the download URL
reference.GetDownloadUrlAsync().ContinueWithOnMainThread(task => {
    if (!task.IsFaulted && !task.IsCanceled) {
        Debug.Log("Download URL: " + task.Result);
        // ... now download the file via WWW or UnityWebRequest.
    }
});

बाइट ऐरे में डाउनलोड करना

GetBytesAsync() तरीके का इस्तेमाल करके, फ़ाइल को मेमोरी में बाइट बफ़र में डाउनलोड किया जा सकता है. इस तरीके से, आपकी फ़ाइल का पूरा कॉन्टेंट मेमोरी में लोड हो जाएगा. अगर किसी ऐसी फ़ाइल का अनुरोध किया जाता है जिसका साइज़, आपके ऐप्लिकेशन की उपलब्ध मेमोरी से ज़्यादा है, तो आपका ऐप्लिकेशन क्रैश हो जाएगा. मेमोरी से जुड़ी समस्याओं से बचने के लिए, पक्का करें कि आपने ज़्यादा से ज़्यादा साइज़ को किसी ऐसी वैल्यू पर सेट किया हो जिसे आपका ऐप्लिकेशन हैंडल कर सकता है. इसके अलावा, डाउनलोड करने का कोई दूसरा तरीका इस्तेमाल करें.

// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
const long maxAllowedSize = 1 * 1024 * 1024;
reference.GetBytesAsync(maxAllowedSize).ContinueWithOnMainThread(task => {
    if (task.IsFaulted || task.IsCanceled) {
        Debug.LogException(task.Exception);
        // Uh-oh, an error occurred!
    }
    else {
        byte[] fileContents = task.Result;
        Debug.Log("Finished downloading!");
    }
});

स्ट्रीम की मदद से डाउनलोड करना

स्ट्रीम की मदद से फ़ाइल डाउनलोड करने पर, लोड होने के दौरान डेटा को प्रोसेस किया जा सकता है. इससे, डाउनलोड को मैनेज करने में ज़्यादा आसानी होती है. GetStreamAsync() को कॉल करें और पहले आर्ग्युमेंट के तौर पर, अपना स्ट्रीम प्रोसेसर पास करें. इस डेलिगेट को बैकग्राउंड थ्रेड पर स्ट्रीम के साथ कॉल किया जाएगा. इससे, इंतज़ार के समय के हिसाब से ज़्यादा समय लेने वाली कार्रवाइयां या कैलकुलेशन की जा सकती हैं. जैसे, कॉन्टेंट को डिस्क में सेव करना.

// Download via a Stream
reference.GetStreamAsync(stream => {
    // Do something with the stream here.
    //
    // This code runs on a background thread which reduces the impact
    // to your framerate.
    //
    // If you want to do something on the main thread, you can do that in the
    // progress eventhandler (second argument) or ContinueWith to execute it
    // at task completion.
}, null, CancellationToken.None);

GetStreamAsync() में स्ट्रीम प्रोसेसर के बाद, वैकल्पिक आर्ग्युमेंट लिए जा सकते हैं. इनकी मदद से, कार्रवाई रद्द की जा सकती है या प्रोग्रेस की सूचना पाई जा सकती है.

लोकल फ़ाइल में डाउनलोड करना

GetFileAsync() तरीके से, किसी फ़ाइल को सीधे लोकल डिवाइस पर डाउनलोड किया जा सकता है. इसका इस्तेमाल तब करें, जब आपके उपयोगकर्ताओं को ऑफ़लाइन होने पर भी फ़ाइल ऐक्सेस करनी हो या उन्हें किसी दूसरे ऐप्लिकेशन में फ़ाइल शेयर करनी हो.

// Create local filesystem URL
string localUrl = "file:///local/images/island.jpg";

// Download to the local filesystem
reference.GetFileAsync(localUrl).ContinueWithOnMainThread(task => {
    if (!task.IsFaulted && !task.IsCanceled) {
        Debug.Log("File downloaded.");
    }
});

डाउनलोड की स्थिति पर नज़र रखने के लिए, डाउनलोड में लिसनर जोड़े जा सकते हैं. लिसनर, स्टैंडर्ड System.IProgress<T> इंटरफ़ेस का पालन करता है. StorageProgress क्लास के इंस्टेंस का इस्तेमाल करके, प्रोग्रेस टिक के लिए कॉलबैक के तौर पर, अपना Action<T> दिया जा सकता है.

// Create local filesystem URL
string localUrl = "file:///local/images/island.jpg";

// Start downloading a file
Task task = reference.GetFileAsync(localFile,
    new StorageProgress<DownloadState>(state => {
        // called periodically during the download
        Debug.Log(String.Format(
            "Progress: {0} of {1} bytes transferred.",
            state.BytesTransferred,
            state.TotalByteCount
        ));
    }), CancellationToken.None);

task.ContinueWithOnMainThread(resultTask => {
    if (!resultTask.IsFaulted && !resultTask.IsCanceled) {
        Debug.Log("Download finished.");
    }
});

गड़बड़ियां ठीक करना

डाउनलोड में गड़बड़ियां आने की कई वजहें हो सकती हैं. इनमें, फ़ाइल का मौजूद न होना या उपयोगकर्ता के पास, अपनी पसंद की फ़ाइल को ऐक्सेस करने की अनुमति न होना शामिल है. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ों के गड़बड़ियां ठीक करना सेक्शन में देखी जा सकती है.

अगले चरण

Cloud Storage में सेव की गई फ़ाइलों के लिए, मेटाडेटा पाया और अपडेट भी किया जा सकता है Cloud Storage.