using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityGoogleDrive; using System.IO; public class UploadToDrive : MonoBehaviour { [SerializeField] private InputField name; [SerializeField] private InputField pwd; [SerializeField] private Button loginBtn; [SerializeField] private Button capture; [SerializeField] private Button uploadBtn; [SerializeField] private GameObject loginPanel; [SerializeField] private GameObject screenshotPenel; [SerializeField] private GameObject uploadPenel; WebCamTexture wc; public RawImage dispay; private string UploadFilePath; private string result; private GoogleDriveFiles.CreateRequest request; private void Start() { Debug.Log(Application.persistentDataPath + "\n" + Application.dataPath); wc = new WebCamTexture(); loginPanel.SetActive(true); screenshotPenel.SetActive(false); uploadPenel.SetActive(false); loginBtn.onClick.AddListener(delegate { OpenCamera(); }); capture.onClick.AddListener(delegate { TakePhoto(); }); uploadBtn.onClick.AddListener(delegate { Upload(false); }); } void OpenCamera() { screenshotPenel.SetActive(true); loginPanel.SetActive(false); dispay.texture = wc; dispay.material.mainTexture = wc; wc.Play(); } void TakePhoto() { string datetime = System.DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss"); string fileName = Application.dataPath + "/IMG-" + datetime + ".PNG"; UploadFilePath = fileName; ScreenCapture.CaptureScreenshot(fileName); wc.Stop(); screenshotPenel.SetActive(false); uploadPenel.SetActive(true); } private void Upload(bool toAppData) { var content = File.ReadAllBytes(UploadFilePath); if (content == null) return; var file = new UnityGoogleDrive.Data.File() { Name = Path.GetFileName(UploadFilePath), Content = content }; if (toAppData) file.Parents = new List { "appDataFolder" }; request = GoogleDriveFiles.Create(file); request.Fields = new List { "id", "name", "size", "createdTime" }; request.Send().OnDone += PrintResult; } private void PrintResult(UnityGoogleDrive.Data.File file) { result = string.Format("Name: {0} Size: {1:0.00}MB Created: {2:dd.MM.yyyy HH:MM:ss}\nID: {3}", file.Name, file.Size * .000001f, file.CreatedTime, file.Id); } }