facebook twitter hatena line email

Unity/画像共有/NativeShare

提供: 初心者エンジニアの簡易メモ
2025年9月16日 (火) 07:27時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

公式

https://github.com/yasirkula/UnityNativeShare?tab=readme-ov-file

インストール

unitypackageからか、PackageManagerからか、どちらかでインストール

unitypackageからインストール

https://github.com/yasirkula/UnityNativeShare/releases unitypackageをDLしてインストール

PackageManagerからインストール

以下を追加

Packages/manifest.json

{
  "dependencies": {
    "com.yasirkula.nativeshare": "https://github.com/yasirkula/UnityNativeShare.git"
  },
  "scopedRegistries": [
    {
      "name": "OpenUPM",
      "url": "https://package.openupm.com",
      "scopes": [
        "com.yasirkula.nativeshare"
      ]
    }
  ]
}

テキストの共有のサンプルコード

new NativeShare()
            .SetText("テキスト!")
            .Share();

画像とテキストの同時共有のサンプルコード

using System.IO;
void OnClickShare()
{
    Texture2D tex = ScreenCapture.CaptureScreenshotAsTexture();
    byte[] png = tex.EncodeToPNG();
    UnityEngine.Object.Destroy(tex);

    // 一時ファイル保存
    string filePath = Path.Combine(Application.temporaryCachePath, "shared_img.png");
    File.WriteAllBytes(filePath, png);

    // 共有
    new NativeShare()
            .AddFile(filePath)
            .SetText("スクショと一緒に送るテキスト!")
            .Share();
}