LoginSignup
19
14

More than 5 years have passed since last update.

Unityで画像付きツイートをSocialConnectorを使って投稿する

Posted at

はじめに

UnityからTwitterに投稿したい。iOS向けにはSocial.frameworkが使えると使い勝手がよさそうだなー考えていたのですが、それっぽい便利ライブラリは見つけられず、UIActivityを使えるようにできるライブラリのsocial-connectorを使うことにしました。

使い方

使い方はいろいろな方が記事を書いていますので、それを参考にしました。
http://clrmemory.com/unity/socialconnector-pic-error/

Unityで使っている画像を使う

いろいろとネット情報探したんですが、キャプチャを使ったサンプルはありましたが、Unityで使っている画像をAndroid, iOSで動かせるサンプルが見当たらなかったので、自分が書いたコードを貼っておきます。

using UnityEngine;
using System.Collections;
public class Sample : MonoBehaviour {
    public void OnShareClick() {
        StartCoroutine(Share("share text", "url", Application.streamingAssetsPath + "srcimage.png"));
    }
    IEnumerator Share(string text, string url, string streamAssetsImagePath) {
        #if UNITY_ANDROID
        WWW www = new WWW(streamAssetsImagePath); // ローカルファイルを読む
        yield return www;

        string imagePath = Application.temporaryCachePath + "/shareimage.png";
        System.IO.File.WriteAllBytes(imagePath, www.bytes);  
        SocialConnector.SocialConnector.Share(text, url, imagePath);
        #else
        // iOSなら直接StreamAssetsが読める
        SocialConnector.SocialConnector.Share(text, url, streamAssetsImagePath);
        yield break;
        #endif
    }
}

ポイント:

  • Unity上のファイルをそのまま使うにはストリームアセットを使うが、AndroidだとAPKファイルに含まれるため、WWWでアクセスしてコピーする必要がある
  • 永続にする必要がないのでtemporaryCachePathを利用した
    • AndroidのPlayerSettingsでWriteAccessをExternalにするのを忘れずに

今回のアプリは絶賛配信中です

動画

配信:
iOS: https://itunes.apple.com/jp/app/id1189800250
Android: https://play.google.com/store/apps/details?id=com.keepgamesimple.rollingsphere

19
14
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
19
14