1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using UnityEngine;
- using System.IO;
- using System.Runtime.InteropServices;
- using Random = UnityEngine.Random;
- public class NativeShare {
- public static void Share(string shareText, Texture2D image)
- {
- var bytes = image.EncodeToPNG();
- var path = Application.persistentDataPath + "/" + CreateRandomScreenshotFileName();
- File.WriteAllBytes(path,bytes);
- Debug.LogWarning(path);
- Share(shareText,path,"");
- }
- private static string CreateRandomScreenshotFileName()
- {
- var arr = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
- var result = string.Empty;
- for (int i = 0; i < 15; i++)
- {
- result += arr[Random.Range(0, arr.Length)];
- }
- return result + ".png";
- }
- public static void Share(string shareText, string imagePath, string url, string subject = "")
- {
- #if UNITY_ANDROID
- AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
- AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
-
- intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
- AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
- AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + imagePath);
- intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
- intentObject.Call<AndroidJavaObject>("setType", "image/png");
-
- intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareText);
-
- AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
-
- AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, subject);
- currentActivity.Call("startActivity", jChooser);
- #elif UNITY_IOS
- //CallSocialShareAdvanced(shareText, subject, url, imagePath);
- #else
- Debug.Log("No sharing set up for this platform.");
- #endif
- }
- #if UNITY_IOS
- /*public struct ConfigStruct
- {
- public string title;
- public string message;
- }
- [DllImport ("__Internal")] private static extern void showAlertMessage(ref ConfigStruct conf);
-
- public struct SocialSharingStruct
- {
- public string text;
- public string url;
- public string image;
- public string subject;
- }
-
- [DllImport ("__Internal")] private static extern void showSocialSharing(ref SocialSharingStruct conf);
-
- public static void CallSocialShare(string title, string message)
- {
- ConfigStruct conf = new ConfigStruct();
- conf.title = title;
- conf.message = message;
- showAlertMessage(ref 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);
- }*/
- #endif
- }
|