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

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

पहचान फ़ाइल बनाना

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

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

// 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!");
    }
});

स्ट्रीम के ज़रिए डाउनलोड करना

स्ट्रीम की मदद से फ़ाइल डाउनलोड करने पर, डेटा लोड होने के साथ-साथ उसे प्रोसेस किया जा सकता है. इससे आपको डाउनलोड करने के लिए ज़्यादा विकल्प मिलते हैं. Call 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> इंटरफ़ेस का इस्तेमाल करता है. प्रोग्रेस टिक के लिए कॉलबैक के तौर पर, अपने Action<T> को उपलब्ध कराने के लिए, StorageProgress क्लास के इंस्टेंस का इस्तेमाल किया जा सकता है.

// 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 में सेव की गई फ़ाइलों के लिए, मेटाडेटा पाया और अपडेट किया जा सकता है.