main
reng 3 months ago
parent 95f13621dc
commit bfe3734365
  1. 24
      Assets/Scenes/SampleScene.unity
  2. 30
      Assets/Scripts/GoogleDriveUtils.cs
  3. 39
      Assets/Scripts/UploadToGoogleDrive.cs

@ -1115,7 +1115,7 @@ MonoBehaviour:
ImageGenerate: {fileID: 73630329}
ImageDiscardPostcard: {fileID: 1814244730}
ImageGeneratePostcard: {fileID: 580836533}
GoogleDriveFolderId: 1m6qxzRrWfj3UcVxJNgACabS4aM8arbHq
GoogleDriveFolderId: 19A37k7X-W2BSE989e-ejJU7b-7Bbzu89
--- !u!1 &366298493
GameObject:
m_ObjectHideFlags: 0
@ -1152,8 +1152,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 140.25, y: 0}
m_SizeDelta: {x: 280.5, y: 0}
m_AnchoredPosition: {x: 140, y: 0}
m_SizeDelta: {x: 280, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &366298495
MonoBehaviour:
@ -3351,7 +3351,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 659.1428, y: -280.5}
m_AnchoredPosition: {x: 659.1428, y: -280}
m_SizeDelta: {x: 219.71428, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &696492449
@ -3838,7 +3838,7 @@ RectTransform:
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 137.32143, y: 0}
m_SizeDelta: {x: 280.5, y: 0}
m_SizeDelta: {x: 280, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &830016229
MonoBehaviour:
@ -4519,8 +4519,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 140.25, y: 0}
m_SizeDelta: {x: 280.5, y: 0}
m_AnchoredPosition: {x: 140, y: 0}
m_SizeDelta: {x: 280, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1060364296
MonoBehaviour:
@ -5063,10 +5063,10 @@ RectTransform:
- {fileID: 555167254}
m_Father: {fileID: 1317646465}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 617.5, y: -70}
m_SizeDelta: {x: 0, y: 70}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 70, y: 0}
m_Pivot: {x: 1, y: 0}
--- !u!114 &1255800684
MonoBehaviour:
@ -6443,7 +6443,7 @@ RectTransform:
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 411.9643, y: 0}
m_SizeDelta: {x: 315.5625, y: 0}
m_SizeDelta: {x: 315, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1811530096
MonoBehaviour:

@ -12,12 +12,40 @@ using System;
using System.IO;
using Google.Apis.Upload;
using System.Threading.Tasks;
using System.Threading;
public class GoogleDriveUtils
{
static string[] Scopes = { DriveService.Scope.Drive }; // or DriveService.Scope.Drive for full access
static string ApplicationName = "MyDriveUploader";
public static DriveService service;
public static void setupService()
{
UserCredential credential;
using (var stream = new FileStream("../Material/client_secret_30989913678-hk8sipecu98lp5vdsk0ictf5gufmn33p.apps.googleusercontent.com.json", FileMode.Open, FileAccess.Read))
{
// credential = GoogleCredential.FromStream(stream)
// .CreateScoped(Scopes);
string credPath = "../Material/token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
}
public static string getFolder(DriveService service, string path, string parentFolderId)
@ -99,7 +127,7 @@ public class GoogleDriveUtils
request.Fields = "id, name";
request.SupportsAllDrives = true;
request.ProgressChanged += (progress) => Debug.Log($"Upload Progress: {progress.Status} - Bytes Sent: {progress.BytesSent}");
request.ProgressChanged += (progress) => Debug.Log($"Upload Progress: {progress.Status} - Bytes Sent: {progress.BytesSent} {progress.Exception?.Message}");
request.ResponseReceived += (file) => Debug.Log($"File uploaded successfully: {file.Name} (ID: {file.Id})");

@ -1,16 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.IO;
using Google.Apis.Upload;
@ -41,7 +36,7 @@ public class UploadToGoogleDrive : MonoBehaviour
public string GoogleDriveFolderId = "1m6qxzRrWfj3UcVxJNgACabS4aM8arbHq";
DriveService service;
// DriveService service;
void Start()
{
@ -167,24 +162,26 @@ public class UploadToGoogleDrive : MonoBehaviour
}
void setupService()
{
Debug.Log("Setting up Google Drive service...");
// Initialize Google Drive API service
service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = GoogleCredential.FromFile("../Material/tech-277909-8bd38efb7464.json").CreateScoped(DriveService.Scope.DriveFile),
ApplicationName = "24070-Upload",
});
// Debug.Log("Setting up Google Drive service...");
// // Initialize Google Drive API service
// service = new DriveService(new BaseClientService.Initializer()
// {
// HttpClientInitializer = GoogleCredential.FromFile("../Material/tech-277909-8bd38efb7464.json").CreateScoped(DriveService.Scope.DriveFile),
// ApplicationName = "24070-Upload",
// });
GoogleDriveUtils.setupService();
}
async void upload(string _uploadFile, string _fileId, string type)
{
if(service == null)
{
Debug.Log("service is null, setting up service again.");
setupService();
}
// if(service == null)
// {
// Debug.Log("service is null, setting up service again.");
// setupService();
// }
string path = _fileId;
@ -195,8 +192,8 @@ public class UploadToGoogleDrive : MonoBehaviour
}
Debug.Log("Uploading file to Google Drive: " + _uploadFile + " folder=" + path);
string folderId = GoogleDriveUtils.getFolder(service, path, GoogleDriveFolderId);
await GoogleDriveUtils.upload(service, _uploadFile, folderId, _fileId, type);
string folderId = GoogleDriveUtils.getFolder(GoogleDriveUtils.service, path, GoogleDriveFolderId);
await GoogleDriveUtils.upload(GoogleDriveUtils.service, _uploadFile, folderId, _fileId, type);
}

Loading…
Cancel
Save