LoginSignup
1
2

More than 1 year has passed since last update.

[Unity] 画面キャプチャをドライブにアップロードする

Posted at

概要

Unityエディタ上で画面をキャプチャして、それをファイル出力せずドライブにアップロードする

経緯

UI実装してデザイナーに確認する度にキャプチャをとることが増え、多少なりとも自動化できないかと思い至った

実装

GASでウェブアプリ作ってそれを叩くだけ

function doPost(e) {
  var json = JSON.parse(e.postData.contents);

  var folder = DriveApp.getFolderById(json.folderId);
  var blob = Utilities.newBlob(json.data, json.mime, json.fileName);
  folder.createFile(blob);

  var text = "upload succeeded";
  var output = ContentService.createTextOutput(text);
  output.setMimeType(ContentService.MimeType.TEXT);
  return output;
}
IEnumerator Co(string gasId, string folderId, Action<string> callback)
{
    yield return new WaitForEndOfFrame();

    var texture = ScreenCapture.CaptureScreenshotAsTexture();

    var data = new PostData
    {
        folderId = folderId,
        fileName = DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss") + ".png",
        mime = "image/png",
        data = texture.EncodeToPNG(),
    };

    var request = new UnityWebRequest(
        $"https://script.google.com/macros/s/{gasId}/exec",
        UnityWebRequest.kHttpVerbPOST,
        new DownloadHandlerBuffer(),
        new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonUtility.ToJson(data)))
    );

    yield return request.SendWebRequest();

    try
    {
        callback(request.downloadHandler.text);
    }
    catch (Exception e)
    {
        Debug.LogError(e);
    }
}

何番煎じか分からないがとりあえず備忘録的な感じで
加えて細かい設定ができるようにエディタウィンドウ実装したり、フック作って通知したり

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2