LoginSignup
1
2

More than 1 year has passed since last update.

UnityでAndroidでビルドした後にSlackにapkを自動アップロードする

Last updated at Posted at 2021-11-21

とにかく便利なので、Unityでモバイルゲームを作っている人はみんなやってほしい。他のメンバーへのapk共有にも便利。

事前準備

https://api.slack.com/apps の「Create New App」から slack appを作成します。

左のサイドバーから OAuth & Permissions にアクセス
image.png

Bot Token Scopes に以下を追加。

  • files:read
  • files:write
  • chat:write

Install Workspace で自身のワークスペースに連携しましょう。 Bot User OAuth Tokenxoxb- で始まるbotトークンをコピーしておきます。

スクリプトの作成

Editor/ ディレクトリの下(ない場合は作成して良い)に次のスクリプトを配置する。

using System.Collections.Specialized;
using System.Net;
using System.Text;
using UnityEditor;
using UnityEditor.Callbacks;
using Debug = UnityEngine.Debug;

namespace Editor
{
    public static class SlackPoster
    {
        [PostProcessBuild]
        public static void OnPostProcessBuild(BuildTarget target, string path) {
            if (target == BuildTarget.Android) {
                Post(path);
            }
        }

        private static void Post(string path) 
        {
            var client = new WebClient();
            var parameters = new NameValueCollection();
            parameters["token"] = "トークン";
            parameters["channels"] = "channel名";
            parameters["initial_comment"] = "できたよ。";
            client.QueryString = parameters;

            byte[] responseBytes = client.UploadFile("https://slack.com/api/files.upload", path);
            string responseString = Encoding.UTF8.GetString(responseBytes); 
            Debug.Log(responseString);
        }
    }
}

parameters["channels"] にapkをアップロードするチャンネル名(先頭#は含まない)を指定し、
parameters["token"] に先ほどコピーしたbotトークンを指定しておいてください。

ただし、botトークンは秘匿情報なので、ソースコードに直書きした場合は、パブリックリポジトリにコミットしてはいけません!

上記のファイルを .gitignore に追加するなどの対応をしましょう。

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