Unity download file from google drive

I'm currently making two programs for work in Unity on two separate computers. I have managed to read a local text file on the main computer and upload the info into a google sheets document. For security reasons, I understand that it is not possible to then access the drive on the second computer and download the sheet as a csv file locally. Is there another way I can access the sheet data on the second computer from google drive without having to download it manually each morning?

asked Apr 8 at 12:18

Unity download file from google drive

1

I suggest you do the following:

  • I suggest you create a shared folder on google drive, then set this folder to be publicly accessible.

  • then you can upload files to this drive, and then download it else where

There is 1 problem with this, is that your info will be accessible publicly by anyone who has the link, this is a simple way to do it for testing for now.

answered Apr 8 at 12:22

Unity download file from google drive

Dean Van GreunenDean Van Greunen

4,0522 gold badges14 silver badges27 bronze badges

1

November 15, 2017

Assembled a plugin for Unity engine to work with Google Drive. The plugin provides API for listing, searching, creating, uploading, editing, copying, downloading and deleting files; works with Unity version 5.6 and higher and supports all major target platforms (including WebGL).

Project on GitHub (MIT license): github.com/Elringus/UnityGoogleDrive.

comments powered by Disqus

Read Next

September 14, 2016

UI distortion in Unreal Engine

Opensource Glitch Shader Ue4

April 12, 2018

Using Raw Input API in Unity

Opensource Control Event Native Keyboard

Tags

Stay organized with collections Save and categorize content based on your preferences.

Cloud Storage for Firebase allows you to quickly and easily download files from a Cloud Storage bucket provided and managed by Firebase.

Create a Reference

To download a file, first create a Cloud Storage reference to the file you want to download.

You can create a reference by appending child paths to the root of your Cloud Storage bucket, or you can create a reference from an existing gs:// or https:// URL referencing an object in 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");

Download Files

Once you have a reference, you can download files from Cloud Storage in four ways:

  1. Download from a URL
  2. Download to a byte array
  3. Download with a Stream
  4. Download to a local file

The method you will use to retrieve your files will depend on how you want to consume the data in your game.

Download from a URL

If you want to use a URL with Unity's WWW or UnityWebRequest you can get a download URL for a file by calling 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.
    }
});

Download to a byte array

You can download the file to a byte buffer in memory using the GetBytesAsync() method. This method will load the entire contents of your file into memory. If you request a file larger than your app's available memory, your app will crash. To protect against memory issues, make sure to set the max size to something you know your app can handle, or use another download method.

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

Download via a Stream

Downloading the file with a Stream allows you to process the data as its loaded. This gives you maximum flexibility when dealing with your download. Call GetStreamAsync() and pass your own stream processor as the first argument. This delegate will get called on a background thread with a Stream which allows you to perform latency intensive operations or calculations such as storing the contents to disk.

// 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() takes an optional arguments after the stream processor that allows you to cancel the operation or get notified of progress.

Download to a local file

The GetFileAsync() method downloads a file directly to a local device. Use this if your users want to have access to the file while offline or to share the file in a different app.

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

You can attach listeners to downloads in order to monitor the progress of the download. The listener follows the standard System.IProgress interface. You can use an instance of the StorageProgress class, to provide your own Action as a callback for progress ticks.

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

// Start downloading a file
Task task = reference.GetFileAsync(localFile,
    new StorageProgress(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.");
    }
});

Handle Errors

There are a number of reasons why errors may occur on download, including the file not existing, or the user not having permission to access the desired file. More information on errors can be found in the Handle Errors section of the docs.

Next Steps

You can also get and update metadata for files that are stored in Cloud Storage.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2022-11-07 UTC.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]

How do I access Google Drive from C#?

Introduction to Google Drive API Version 3.
Log into your Google account..
Register your application in Google API Console from here this URL..
Create a new project by clicking on the "Continue" button..
Click on the "Cancel" button..
Go to "OAuth consent screen"..
Enter the application name and click "Save"..

Does Google support Unity?

Stay organized with collections Save and categorize content based on your preferences. Google's official packages for Unity extend the default capabilities of Unity, enabling you to optimize game performance, reach new users, understand user behavior, and more.

How do I download files from Google Drive Unity?

Import the package; In the Unity editor navigate to Edit -> Settings -> Google Drive ; GoogleDriveSettings. asset file will be automatically created at Assets/UnityGoogleDrive/Resources , select the file (if it wasn't selected automatically);