はじめに
前回の記事でEasy Mobile ProをUnityプロジェクトへ導入し、さらにExternal Dependency ManagerをUnity Package Manager管理へ変更するやり方を紹介しました。
今回はEasy Mobile Proはいよいよ実装編です。今回はSNS共有とスクリーンショットのやり方を紹介します。
前回の記事はこちら
Easy Mobile Proとは?
Easy Mobile Pro は、広告表示、アプリ内購入、通知、共有など、モバイルゲームごとの共通機能の実装を大幅に簡素化する多機能プラグインです。
モバイルゲーム開発をしている人にとっては必須と言えるほどプラグインと言っても過言ではありません。
Asset Store
環境
- Unity 2021.2.7f1(Apple Silicon)
- Easy Mobile Pro 2.17.5
準備
[Window]→[Mobile Pro]でEasy Mobile Proの設定画面を開き、「SHARING」のスイッチを「ON」にします。

すると権限の設定画面に切り替わるので、ユーザーへ見せる権限を使う理由の文章を記入します。

これで準備完了です。
サンプルコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using EasyMobile;
public class SampleShareSceneController : MonoBehaviour
{
[SerializeField] private Button saveScreenshotButton;
[SerializeField] private Button textShareButton;
[SerializeField] private Button saveImageShareButton;
[SerializeField] private Button textureShareButton;
[SerializeField] private Button urlShareButton;
// Start is called before the first frame update
void Start()
{
saveScreenshotButton?.onClick.AddListener(OnSaveScreenshotButtonClicked);
textShareButton?.onClick.AddListener(OnTextShareButtonClicked);
saveImageShareButton?.onClick.AddListener(OnSaveImageShareButton);
textureShareButton?.onClick.AddListener(OnTextureShareButton);
urlShareButton?.onClick.AddListener(OnUrlShareButton);
}
private void OnSaveScreenshotButtonClicked()
{
StartCoroutine(SaveScreenshot());
}
private IEnumerator SaveScreenshot()
{
yield return new WaitForEndOfFrame();
// スクリーンショットを保存.
string path = Sharing.SaveScreenshot("screenshot.png");
}
private void OnTextShareButtonClicked()
{
// テキストをSNS共有.
Sharing.ShareText("This is a sample.");
}
private void OnSaveImageShareButton()
{
string path = System.IO.Path.Combine(Application.persistentDataPath, "screenshot.png");
if (!System.IO.File.Exists(path))
{
return;
}
// 保存された画像をSNS共有.
Sharing.ShareImage(path, "This is a sample.");
}
private void OnTextureShareButton()
{
StartCoroutine(CaptureScreenshotAndShare());
}
private IEnumerator CaptureScreenshotAndShare()
{
yield return new WaitForEndOfFrame();
// スクリーンショットをTexture2Dで取得.
Texture2D texture = Sharing.CaptureScreenshot();
// Texture2Dと文字列をSNS共有.
Sharing.ShareTexture2D(texture, "screenshot", "This is a sample.");
}
private void OnUrlShareButton()
{
// URLをSNS共有.
Sharing.ShareURL("nobushiueshi.com");
}
}
結果
まずはiOS、テキストのみのSNS共有ができました。

保存した画像とTexture2DからSNS共有できました。

URLのSNS共有ももちろんできます。

AndroidでもテキストのSNS共有ができました。

もちろん、保存した画像とTexture2DからSNS共有できました。

最後にAndroidでもURLのSNS共有もできました。

さいごに
Easy Mobile Proめっちゃ簡単だし、めっちゃええやん。
コメント