yasirkula / UnityNativeShare

A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS
MIT License
890 stars 131 forks source link

Unity Native Share Plugin

NOTICE: This asset is no longer maintained! Consider using alternative sharing solutions that are actively being maintained. While using this package, expect issues when sharing a file/image/video with a text (see FAQ).

Available on Asset Store: https://assetstore.unity.com/packages/tools/integration/native-share-for-android-ios-112731

Forum Thread: https://forum.unity.com/threads/native-share-for-android-ios-open-source.519865/

Discord: https://discord.gg/UJJt549AaV

Support the Developer ☕

This plugin helps you natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS. A ContentProvider is used to share the media on Android.

INSTALLATION

There are 5 ways to install this plugin:

Android Setup

NativeShare no longer requires any manual setup on Android. If you were using an older version of the plugin, you need to remove NativeShare's <provider ... /> from your AndroidManifest.xml.

For reference, the legacy documentation is available at: https://github.com/yasirkula/UnityNativeShare/wiki/Manual-Setup-for-Android

iOS Setup

There are two ways to set up the plugin on iOS:

HOW TO

Simply create a new NativeShare object and customize it by chaining the following functions as you like (see example code):

Finally, calling the Share() function of the NativeShare object will present the share sheet.

UTILITY FUNCTIONS

FAQ

On Android, you can share on a specific app via AddTarget. For iOS, you can check out this post and see if it works for you: https://forum.unity.com/threads/native-share-for-android-ios-open-source.519865/page-4#post-4011874

It is just not possible to share an image/file with text/subject on some apps (e.g. Facebook), they intentionally omit either the image or the text from the shared content. These apps require you to use their own SDKs for complex share actions. For best compatibility, I'd recommend you to share either only image or only text.

NativeShare adds <queries> element to AndroidManifest.xml due to the new package visibility change. The build error can be fixed by following these steps: Android 11, Android 12. In the worst case, if you are OK with NativeShare not working on some of the affected devices, then you can open NativeShare.aar with WinRAR or 7-Zip and then remove the <queries>...</queries> element from AndroidManifest.xml.

If you're running the Unity activity in a separate process, then modify AndroidManifest.xml inside NativeShare.aar so that both NativeShareCustomShareDialogActivity and NativeShareBroadcastListener also run on the same process, e.g:

<activity android:name=".NativeShareCustomShareDialogActivity" ... android:process=":YourProcess" />
<receiver android:name=".NativeShareBroadcastListener" ... android:process=":YourProcess" />

If you are sure that your plugin is up-to-date, then enable Custom Proguard File option from Player Settings and add the following line to that file: -keep class com.yasirkula.unity.* { *; }

Add NativeShareNamespace.NSShareResultCallbackAndroid to the Skip Classes section of your obfuscator settings asset.

EXAMPLE CODE

The following code captures the screenshot of the game whenever you tap the screen, saves it in a temporary path and then shares it:

void Update()
{
    if( Input.GetMouseButtonDown( 0 ) )
        StartCoroutine( TakeScreenshotAndShare() );
}

private IEnumerator TakeScreenshotAndShare()
{
    yield return new WaitForEndOfFrame();

    Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
    ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
    ss.Apply();

    string filePath = Path.Combine( Application.temporaryCachePath, "shared img.png" );
    File.WriteAllBytes( filePath, ss.EncodeToPNG() );

    // To avoid memory leaks
    Destroy( ss );

    new NativeShare().AddFile( filePath )
        .SetSubject( "Subject goes here" ).SetText( "Hello world!" ).SetUrl( "https://github.com/yasirkula/UnityNativeShare" )
        .SetCallback( ( result, shareTarget ) => Debug.Log( "Share result: " + result + ", selected app: " + shareTarget ) )
        .Share();

    // Share on WhatsApp only, if installed (Android only)
    //if( NativeShare.TargetExists( "com.whatsapp" ) )
    //  new NativeShare().AddFile( filePath ).AddTarget( "com.whatsapp" ).Share();
}

KNOWN LIMITATIONS