Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NatShare - Free Sharing API

Discussion in 'Assets and Asset Store' started by Lanre, Apr 17, 2018.

  1. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hey there, this is correct. The Asset Store package simply imports NatShare from GitHub (specifically, modifying your project's `manifest.json` file).
     
  2. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hey there, thanks for reporting this. I recommend creating an issue on GitHub to track this. I'll look into it when I can.
     
  3. dreamtravis

    dreamtravis

    Joined:
    Aug 3, 2020
    Posts:
    2
    Ahh got it got it. Must have done something wrong with my Package Manager. Thanks Lanre!
     
    Lanre likes this.
  4. AcodeBank

    AcodeBank

    Joined:
    Oct 13, 2015
    Posts:
    2
  5. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
  6. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    hello.
    how can i share mp3?
     
  7. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    Code (CSharp):
    1. using UnityEngine;
    2. using NatSuite.Sharing;
    3.  
    4.  
    5. public class ShareMp4 : MonoBehaviour {
    6.  
    7.  
    8.     public void ShareVideo()
    9.     {
    10.         var payload = new SharePayload();
    11.         payload.AddMedia("/Resources/Clip001.mp4");
    12.         payload.Commit();
    13.     }
    14.  
    15. }

    what is wrong?
    Isn't everything as you wrote?
     
  8. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    That path looks invalid. When sharing media files, the path has to be the absolute path of the media file on the file system. On iOS and Android, this means that the file must be inside `Application.persistentDataPath` because almost no other directory on the file system is accessible by apps, due to sandboxing. You can't use files in the `Resources` folder because after building from Unity, these are not plain files on the file system.
     
  9. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31

    Could you please share the script for an example?
     
  10. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    There isn't anything different about the code. It depends on how you get the path to your file.
     
  11. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    because I couldn't get it to work no matter what I did
     
  12. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    Code (CSharp):
    1. using UnityEngine;
    2. using NatSuite.Sharing;
    3. using System.Collections;
    4. using System.IO;
    5.  
    6.  
    7. public class ShareMedia : MonoBehaviour {
    8.  
    9.  
    10.     public void ClichkShare()
    11.     {
    12.         StartCoroutine(ShareFile());
    13.     }
    14.  
    15.     private IEnumerator ShareFile()
    16.     {
    17.         var payload = new SharePayload();
    18.         WWW www = new WWW(Application.streamingAssetsPath + "/Clip001.mp4");
    19.         yield return www;
    20.         string filePath = Path.Combine(Application.temporaryCachePath, "Clip001.mp4");
    21.         File.WriteAllBytes(filePath, www.bytes);
    22.         payload.AddMedia(filePath);
    23.         payload.Commit();
    24.  
    25.         Debug.Log( filePath + " " + new FileInfo( filePath ).Length );
    26.     }
    27. }
    Android.
    but still not working
     
    Last edited: Mar 12, 2022
  13. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hm your code looks like it should work, but I would use `persistentDataPath` instead of `temporaryCachePath` for the output file path. What platform are you running on? Can you share the runtime logs?
     
  14. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31

    Android
     
    Lanre likes this.
  15. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31




    Wonderful :). that's the "temporaryCachePath" problem. worked. thank you.
    how to send via whatsapp?
    so that the first queue opens whatsapp?
     
    Lanre likes this.
  16. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    NatShare does not support sharing to a specific app. The user has to pick which app they want to share to.
     
  17. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    I understand it's not a "NatShare" issue. but how can I make it "UnityWebRequest". I couldn't find anyone to help.

    Code (CSharp):
    1.         var payload = new SharePayload();
    2.         WWW www = new WWW(Application.streamingAssetsPath + "/Clip001.mp4");
    3.         yield return www;
    4.         string filePath = Path.Combine(Application.persistentDataPath, "Clip001.mp4");
    5.         File.WriteAllBytes(filePath, www.bytes);
    6.         payload.AddMedia(filePath);
    7.         payload.Commit();
     
  18. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    I'm not sure what you're asking. What do you mean?
     
  19. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    UnityWebRequest instead of www
     
  20. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    Code (CSharp):
    1.         var payload = new SharePayload();
    2.         UnityWebRequest www = new UnityWebRequest(Application.streamingAssetsPath + "/Clip001.mp4");
    3.         yield return www;
    4.         string filePath = Path.Combine(Application.persistentDataPath, "Clip001.mp4");
    5.         File.WriteAllBytes(filePath, www.downloadHandler.data); // there is a problem here. but i don't know how to fix it
    6.  
    7.         payload.AddMedia(filePath);
    8.         payload.Commit();
     
  21. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    See this answer on Unity Answers.
     
    Tural-Muslim likes this.
  22. Tural-Muslim

    Tural-Muslim

    Joined:
    Oct 24, 2019
    Posts:
    31
    Lanre likes this.
  23. zre142g18985

    zre142g18985

    Joined:
    Nov 7, 2020
    Posts:
    6
    Thank you for your wonderful assets.

    In android and ios environment
    I want to save a png image instead of a jpg image.

    NativePayload.cs
    public virtual void AddImage (Texture2D image)
    With the code in
    ImageConversion.EncodeToJPG () is used.
    I changed it like this and tried it on an Android device, but the jpg was saved and it wasn't a png.
    Is there a way?

    NativePayload.cs
    Code (CSharp):
    1. public virtual void AddImage (Texture2D image) {
    2.             // Check
    3.             if (payload == IntPtr.Zero)
    4.                 return;
    5.             if (!image.isReadable) {
    6.                 Debug.LogError("NatShare Error: Cannot add non-readable texture to payload");
    7.                 return;
    8.             }
    9.             // Add
    10.             //var jpegData = ImageConversion.EncodeToJPG(image); // Faster than PNG #85
    11.             var png = ImageConversion.EncodeToPNG(image); //←I added this code
    12.  
    13.             //payload.AddImage(jpegData, jpegData.Length);
    14.             payload.AddImage(png, png.Length);//←I added this code    
    15.         }

    Sorry to ask this of you when you are busy but, I want some advice.
    Best Regards.
     
  24. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hey there, you can save a PNG by writing the PNG data to a file (use System.IO.File API's), then use `payload.AddMedia`.
     
    zre142g18985 likes this.
  25. zre142g18985

    zre142g18985

    Joined:
    Nov 7, 2020
    Posts:
    6
    Thank you for taking the time to reply.
    I have successfully output the png file.

    Very nice asset.
    Thank you very much.
     
  26. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Great to hear it!
     
  27. DevSpur

    DevSpur

    Joined:
    Jul 19, 2014
    Posts:
    20
    Hi I'm looking to use natshare and would like to know if it works in WebGL I need to take a screenshot and share it on social media
     
  28. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hey there, NatShare doesn't support WebGL. It only supports iOS and Android.
     
  29. yinzcam

    yinzcam

    Joined:
    Aug 2, 2016
    Posts:
    3
    Hi!

    I run into this error when using NatShare with an app that has unity library integrated.

    2022-08-19 19:37:01.404 26509-27835/.UnityThread E/AndroidRuntime: FATAL EXCEPTION: SharePayload
    Process: .UnityThread, PID: 26509
    java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority xxx.xxxxxx.xxx.natshare
    at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:606)
    at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417)
    at api.natsuite.natshare.SharePayload.addMedia(SharePayload.java:76)
    at api.natsuite.natshare.SharePayload.lambda$addImage$0$SharePayload(SharePayload.java:67)
    at api.natsuite.natshare.-$$Lambda$SharePayload$ww2jlkwG0eLTgCqdfVlg_g1WcnY.run(Unknown Source:4)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.os.HandlerThread.run(HandlerThread.java:67)

    I'm using
    NatShare: 1.2.6
    Unity: 2020.3.20f1

    Notice: I deleted the core-1.0.0-rc02 because it conflicts with the core-1.2.0 in my project.
     
  30. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hm are you building your Unity project directly to an APK? Or are you exporting to a Gradle project? This error means that a required tag in your app's AndroidManifest.xml is missing.
     
  31. yinzcam

    yinzcam

    Joined:
    Aug 2, 2016
    Posts:
    3
    I was exporting to a gradle project. Like integrate Unity export as a library. What is the required tag?
    I saw a provider is declared in the AndroidManifest inside NatShare.aar. Should I add something in the Library AndroidManifest?

    Here is my Library AndroidManifest:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools">
    <application>
    <activity android:name="com.unity3d.player.UnityPlayerActivity"
    android:process=".UnityThread"
    android:theme="@StyLe/UnityThemeSelector"
    android:screenOrientation="fullSensor"
    android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
    android:resizeableActivity="false"
    android:hardwareAccelerated="false"
    android:launchMode="singleTask"
    android:documentLaunchMode="always">
    <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    <meta-data android:name="android.notch_support" android:value="true" />
    </activity>
    <activity android:name="myapppackagename.PermissionGateActivity" android:process=".UnityThread" />
    <meta-data android:name="unity.splash-mode" android:value="0" />
    <meta-data android:name="unity.splash-enable" android:value="True" />
    <meta-data android:name="unity.allow-resizable-window" android:value="False" />
    <meta-data android:name="notch.config" android:value="portrait|landscape" />
    <meta-data android:name="unity.build-id" android:value="9526bdba-86b2-424a-9329-27cf1b5a5acb" />
    <meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
    <meta-data android:name="com.google.ar.core" android:value="optional" />
    </application>
    <uses-feature android:glEsVersion="0x00030000" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
    <uses-feature android:name="android.hardware.camera.front" android:required="false" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-feature android:name="android.hardware.microphone" android:required="false" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
    <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
    <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
    </manifest>
     

    Attached Files:

  32. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Yup, that's the necessary line. You need to define a <provider> tag in the manifest. This is what NatShare defines:
    Code (CSharp):
    1. <provider
    2.     android:name="androidx.core.content.FileProvider"
    3.     android:authorities="${applicationId}.natshare"
    4.     android:grantUriPermissions="true"
    5.     android:exported="false"
    6. >
    Where `${applicationId}` will be substituted with your app bundle ID by Gradle.
     
  33. GutierManu

    GutierManu

    Joined:
    Jun 7, 2020
    Posts:
    8
    Hi, I'm trying to save videos to the camera roll. Following the documentation, I wrote this solution
    upload_2022-12-1_22-7-57.png

    However, my app crashes each time I try to save. Is there anything wrong in my code?
     
  34. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hey there, sorry for the late response on this. Can you share the crash logs in a `.txt` attachment? Also, you're using an outdated version of NatShare, so upgrade to NatShare 1.3 on GitHub.
     
  35. skoteskote

    skoteskote

    Joined:
    Feb 15, 2017
    Posts:
    85
    Hi everyone, we're using natshare to share video from an iOS AR app to Instagram, but get the error message "Something went wrong" when sharing to anything else but reels (same as in below github issue).

    It does seem like other people have managed to get it to work though, so wanted to check in here to see if you know some workarounds?

    https://github.com/natmlx/natshare/issues/109
     
  36. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hey! Just responded to you on Discord.