NativeShare.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using Random = UnityEngine.Random;
  5. public class NativeShare {
  6. public static void Share(string shareText, Texture2D image)
  7. {
  8. var bytes = image.EncodeToPNG();
  9. var path = Application.persistentDataPath + "/" + CreateRandomScreenshotFileName();
  10. File.WriteAllBytes(path,bytes);
  11. Debug.LogWarning(path);
  12. Share(shareText,path,"");
  13. }
  14. private static string CreateRandomScreenshotFileName()
  15. {
  16. var arr = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
  17. var result = string.Empty;
  18. for (int i = 0; i < 15; i++)
  19. {
  20. result += arr[Random.Range(0, arr.Length)];
  21. }
  22. return result + ".png";
  23. }
  24. public static void Share(string shareText, string imagePath, string url, string subject = "")
  25. {
  26. #if UNITY_ANDROID
  27. AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
  28. AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
  29. intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
  30. AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
  31. AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + imagePath);
  32. intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
  33. intentObject.Call<AndroidJavaObject>("setType", "image/png");
  34. intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareText);
  35. AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  36. AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
  37. AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, subject);
  38. currentActivity.Call("startActivity", jChooser);
  39. #elif UNITY_IOS
  40. //CallSocialShareAdvanced(shareText, subject, url, imagePath);
  41. #else
  42. Debug.Log("No sharing set up for this platform.");
  43. #endif
  44. }
  45. #if UNITY_IOS
  46. /*public struct ConfigStruct
  47. {
  48. public string title;
  49. public string message;
  50. }
  51. [DllImport ("__Internal")] private static extern void showAlertMessage(ref ConfigStruct conf);
  52. public struct SocialSharingStruct
  53. {
  54. public string text;
  55. public string url;
  56. public string image;
  57. public string subject;
  58. }
  59. [DllImport ("__Internal")] private static extern void showSocialSharing(ref SocialSharingStruct conf);
  60. public static void CallSocialShare(string title, string message)
  61. {
  62. ConfigStruct conf = new ConfigStruct();
  63. conf.title = title;
  64. conf.message = message;
  65. showAlertMessage(ref conf);
  66. }
  67. public static void CallSocialShareAdvanced(string defaultTxt, string subject, string url, string img)
  68. {
  69. SocialSharingStruct conf = new SocialSharingStruct();
  70. conf.text = defaultTxt;
  71. conf.url = url;
  72. conf.image = img;
  73. conf.subject = subject;
  74. showSocialSharing(ref conf);
  75. }*/
  76. #endif
  77. }