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

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

@ -12,12 +12,40 @@ using System;
using System.IO; using System.IO;
using Google.Apis.Upload; using Google.Apis.Upload;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading;
public class GoogleDriveUtils 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) public static string getFolder(DriveService service, string path, string parentFolderId)
@ -33,8 +61,8 @@ public class GoogleDriveUtils
request.Q = $"mimeType='application/vnd.google-apps.folder' and name='{folder}' and '{currentFolderId}' in parents and trashed=false"; request.Q = $"mimeType='application/vnd.google-apps.folder' and name='{folder}' and '{currentFolderId}' in parents and trashed=false";
request.Fields = "files(id, name)"; request.Fields = "files(id, name)";
request.SupportsAllDrives = true; request.SupportsAllDrives = true;
request.IncludeItemsFromAllDrives = true; request.IncludeItemsFromAllDrives = true;
var result = request.Execute(); var result = request.Execute();
if (result.Files.Count > 0) if (result.Files.Count > 0)
{ {
@ -99,9 +127,9 @@ public class GoogleDriveUtils
request.Fields = "id, name"; request.Fields = "id, name";
request.SupportsAllDrives = true; 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})"); request.ResponseReceived += (file) => Debug.Log($"File uploaded successfully: {file.Name} (ID: {file.Id})");
var results = await request.UploadAsync(); var results = await request.UploadAsync();

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

Loading…
Cancel
Save