facebook twitter hatena line email

「Unity/画像共有/NatShare」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(Androidビルド時にcore-1.6.0とjetified-core-1.0.0-rc02が競合するとき)
(iosでシェアの画像を保存ボタンを押すとクラッシュする問題)
 
(同じ利用者による、間の6版が非表示)
行26: 行26:
 
     {
 
     {
 
         GameObject.Find("TextButton").GetComponent<Button>().onClick.AddListener(ShareText);
 
         GameObject.Find("TextButton").GetComponent<Button>().onClick.AddListener(ShareText);
         GameObject.Find("ImageButton").GetComponent<Button>().onClick.AddListener(ShareImage);
+
         GameObject.Find("ImageButton").GetComponent<Button>().onClick.AddListener(ShareCaptureImage);
         GameObject.Find("TextImageButton").GetComponent<Button>().onClick.AddListener(ShareTextImage);
+
         GameObject.Find("TextImageButton").GetComponent<Button>().onClick.AddListener(ShareTextCaptureImage);
 
     }
 
     }
 
     // テキスト共有
 
     // テキスト共有
行36: 行36:
 
         payload.Commit();
 
         payload.Commit();
 
     }
 
     }
     // 画像共有
+
     // キャプチャ画像共有
     void ShareImage()
+
     void ShareCaptureImage()
 
     {
 
     {
 
         var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
 
         var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
行44: 行44:
 
         payload.Commit();
 
         payload.Commit();
 
     }
 
     }
     // テキスト&画像共有
+
     // テキスト&キャプチャ画像共有
     void ShareTextImage()
+
     void ShareTextCaptureImage()
 
     {
 
     {
 
         var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
 
         var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
行53: 行53:
 
         payload.Commit();
 
         payload.Commit();
 
     }
 
     }
 +
    // 通常画像共有
 +
    void ShareImage()
 +
    {
 +
        Texture2D texture = image.texture;
 +
        var payload = new SharePayload();
 +
        payload.AddImage(screetexturenshot);
 +
        payload.Commit();
 +
    }
 +
    void
 +
    CanvasImage.texture
 
}
 
}
 
</pre>
 
</pre>
行66: 行76:
  
 
参考:https://marumaro7.hatenablog.com/entry/natshareerror
 
参考:https://marumaro7.hatenablog.com/entry/natshareerror
 +
 +
==iosでシェアの画像を保存ボタンを押すとクラッシュする問題==
 +
エラー詳細
 +
<pre>
 +
[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.
 +
</pre>
 +
NSPhotoLibraryAddUsageDescriptionの権限がないため。
 +
 +
NSPhotoLibraryAddUsageDescriptionを自動で権限を付ける方法
 +
 +
Assets/Editor/XcodePhotoLibraryPostProcess.cs
 +
<pre>
 +
using System.Collections;
 +
using System.Collections.Generic;
 +
using System.IO;
 +
using UnityEngine;
 +
#if UNITY_IOS
 +
using UnityEditor;
 +
using UnityEditor.Build;
 +
using UnityEditor.Build.Reporting;
 +
using UnityEditor.iOS.Xcode;
 +
 +
public class XcodePhotoLibraryPostProcess : IPostprocessBuildWithReport
 +
{
 +
    public int callbackOrder { get { return 0; } }
 +
 +
    public void OnPostprocessBuild(BuildReport report)
 +
    {
 +
        if (report.summary.platform == BuildTarget.iOS)
 +
        {
 +
            string path = Path.Combine(report.summary.outputPath, "Info.plist");
 +
            PlistDocument plist = new PlistDocument();
 +
            plist.ReadFromFile(path);
 +
            plist.root.SetString("NSPhotoLibraryUsageDescription", "シェアボタンで共有時に画像を選択します");
 +
            plist.root.SetString("NSPhotoLibraryAddUsageDescription", "シェアボタンで画像を保存します");
 +
            plist.WriteToFile(path);
 +
        }
 +
    }
 +
}
 +
#endif
 +
</pre>
 +
参考:https://nobushiueshi.com/unityios%E3%83%93%E3%83%AB%E3%83%89%E6%99%82%E3%81%ABnsphotolibraryaddusagedescription%E3%81%A8nsphotolibraryusagedescription%E3%82%92%E8%87%AA%E5%8B%95%E3%81%A7%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B/

2023年4月23日 (日) 00:55時点における最新版

NatShareとは

画像共有プラグイン

androidはversion7.0以上のみ対応

参考:https://nobushiueshi.com/unitysns%E5%85%B1%E6%9C%89%E3%81%AFnatshare%E3%81%8C%E3%81%8A%E3%81%99%E3%81%99%E3%82%81/

DL&Import

NatShare(github):https://github.com/natmlx/NatShare

githubのcodeからNatSuite-mainをDLし、Assets以下に設置。

x NatShare(UnityAssets):https://assetstore.unity.com/packages/tools/integration/natshare-mobile-sharing-api-117705

UnityAssetsでのインストールは、coreが競合する恐れがあるので、やめておく。詳しくは下の競合項目を。

テキストと画像共有

using UnityEngine;
using UnityEngine.UI;
using NatSuite.Sharing;

public class SampleScene : MonoBehaviour
{
    void Start()
    {
        GameObject.Find("TextButton").GetComponent<Button>().onClick.AddListener(ShareText);
        GameObject.Find("ImageButton").GetComponent<Button>().onClick.AddListener(ShareCaptureImage);
        GameObject.Find("TextImageButton").GetComponent<Button>().onClick.AddListener(ShareTextCaptureImage);
    }
    // テキスト共有
    void ShareText()
    {
        var payload = new SharePayload();
        payload.AddText("ここに共有したいテキストを入力");
        payload.Commit();
    }
    // キャプチャ画像共有
    void ShareCaptureImage()
    {
        var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
        var payload = new SharePayload();
        payload.AddImage(screenshot);
        payload.Commit();
    }
    // テキスト&キャプチャ画像共有
    void ShareTextCaptureImage()
    {
        var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
        var payload = new SharePayload();
        payload.AddText("ここに共有したいテキストを入力");
        payload.AddImage(screenshot);
        payload.Commit();
    }
    // 通常画像共有
    void ShareImage()
    {
        Texture2D texture = image.texture;
        var payload = new SharePayload();
        payload.AddImage(screetexturenshot);
        payload.Commit();
    }
    void 
    CanvasImage.texture
}

Androidビルド時にcore-1.6.0とjetified-core-1.0.0-rc02が競合するとき

エラー詳細

java.lang.RuntimeException: Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules core-1.6.0-runtime.jar (androidx.core:core:1.6.0) and jetified-core-1.0.0-rc02-runtime.jar (:core-1.0.0-rc02:)

AdMobプラグインが、core-1.6.0を使ってるぽい。

対応方法

  1. NatShareプラグインをUnityAssetsからDLしてる場合は、githubからDLするように
  2. NatShare-main/Plugins/Android/core-1.0.0-rc02.aarを削除

参考:https://marumaro7.hatenablog.com/entry/natshareerror

iosでシェアの画像を保存ボタンを押すとクラッシュする問題

エラー詳細

[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

NSPhotoLibraryAddUsageDescriptionの権限がないため。

NSPhotoLibraryAddUsageDescriptionを自動で権限を付ける方法

Assets/Editor/XcodePhotoLibraryPostProcess.cs

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
#if UNITY_IOS
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;

public class XcodePhotoLibraryPostProcess : IPostprocessBuildWithReport
{
    public int callbackOrder { get { return 0; } }

    public void OnPostprocessBuild(BuildReport report)
    {
        if (report.summary.platform == BuildTarget.iOS)
        {
            string path = Path.Combine(report.summary.outputPath, "Info.plist");
            PlistDocument plist = new PlistDocument();
            plist.ReadFromFile(path);
            plist.root.SetString("NSPhotoLibraryUsageDescription", "シェアボタンで共有時に画像を選択します");
            plist.root.SetString("NSPhotoLibraryAddUsageDescription", "シェアボタンで画像を保存します");
            plist.WriteToFile(path);
        }
    }
}
#endif

参考:https://nobushiueshi.com/unityios%E3%83%93%E3%83%AB%E3%83%89%E6%99%82%E3%81%ABnsphotolibraryaddusagedescription%E3%81%A8nsphotolibraryusagedescription%E3%82%92%E8%87%AA%E5%8B%95%E3%81%A7%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B/