68 lines
1.7 KiB
C#
68 lines
1.7 KiB
C#
using UnityEngine;
|
|
|
|
#if UNITY_IOS
|
|
using System.Collections;
|
|
using System.Runtime.InteropServices;
|
|
#endif
|
|
|
|
/*
|
|
* https://github.com/ChrisMaire/unity-native-sharing
|
|
*/
|
|
|
|
public class NativeShare
|
|
{
|
|
|
|
public static void Share(string url)
|
|
{
|
|
#if UNITY_ANDROID
|
|
if(Application.platform == RuntimePlatform.Android)
|
|
{
|
|
// Create new intent
|
|
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
|
|
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
|
|
|
|
// Action
|
|
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
|
|
|
|
// Content
|
|
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), url);
|
|
|
|
// Content type
|
|
intentObject.Call<AndroidJavaObject>("setType", "text/plain");
|
|
|
|
// Start intent
|
|
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
|
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
|
|
currentActivity.Call("startActivity", intentObject);
|
|
}
|
|
#elif UNITY_IOS
|
|
CallSocialShareAdvanced(null, null, url, null);
|
|
#else
|
|
Debug.Log("No sharing set up for this platform.");
|
|
#endif
|
|
}
|
|
|
|
#if UNITY_IOS
|
|
[DllImport("__Internal")]
|
|
private static extern void showSocialSharing(ref SocialSharingStruct conf);
|
|
|
|
public static void CallSocialShareAdvanced(string defaultTxt, string subject, string url, string img)
|
|
{
|
|
SocialSharingStruct conf = new SocialSharingStruct();
|
|
conf.text = defaultTxt;
|
|
conf.url = url;
|
|
conf.image = img;
|
|
conf.subject = subject;
|
|
|
|
showSocialSharing(ref conf);
|
|
}
|
|
|
|
public struct SocialSharingStruct
|
|
{
|
|
public string text;
|
|
public string url;
|
|
public string image;
|
|
public string subject;
|
|
}
|
|
#endif
|
|
}
|