NativeMethods.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using Opencoding.Shared.Utils;
  6. using UnityEngine;
  7. namespace Opencoding.Console
  8. {
  9. static class NativeMethods
  10. {
  11. #if UNITY_IOS
  12. [DllImport("__Internal")]
  13. private static extern void _opencodingConsoleBeginEmail(string toAddress, string subject, string body, bool isHTML);
  14. [DllImport("__Internal")]
  15. private static extern void _opencodingConsoleAddAttachment(byte[] bytes, int length, string mimeType, string filename);
  16. [DllImport("__Internal")]
  17. private static extern void _opencodingConsoleFinishEmail();
  18. [DllImport("__Internal")]
  19. private static extern void _opencodingConsoleCopyToClipboard(string text);
  20. [DllImport("__Internal")]
  21. private static extern int _opencodingConsoleGetNativeScreenWidth();
  22. #endif
  23. public static void SendEmail(Email email)
  24. {
  25. #if UNITY_IOS && !UNITY_EDITOR
  26. _opencodingConsoleBeginEmail(email.ToAddress, email.Subject, email.Message, email.IsHTML);
  27. foreach (var attachment in email.Attachments)
  28. {
  29. _opencodingConsoleAddAttachment(attachment.Data, attachment.Data.Length, attachment.MimeType, attachment.Filename);
  30. }
  31. _opencodingConsoleFinishEmail();
  32. #elif UNITY_ANDROID && !UNITY_EDITOR
  33. AndroidJavaClass androidEmailClass = new AndroidJavaClass("net.opencoding.console.Email");
  34. // For reasons unknown, sometimes only one of these paths works.
  35. foreach (var path in new string[] {Application.persistentDataPath, Application.temporaryCachePath})
  36. {
  37. try
  38. {
  39. androidEmailClass.CallStatic("beginEmail", email.ToAddress, email.Subject, email.Message, email.IsHTML);
  40. var emailAttachmentsDirectory = Path.Combine(path, "EmailAttachments");
  41. Directory.CreateDirectory(emailAttachmentsDirectory);
  42. foreach (var attachment in email.Attachments)
  43. {
  44. var attachmentPath = Path.Combine(emailAttachmentsDirectory, attachment.Filename);
  45. File.WriteAllBytes(attachmentPath, attachment.Data);
  46. androidEmailClass.CallStatic("addAttachment", attachmentPath);
  47. }
  48. androidEmailClass.CallStatic("finishEmail");
  49. }
  50. catch (AndroidJavaException)
  51. {
  52. continue;
  53. }
  54. break;
  55. }
  56. #else
  57. throw new InvalidOperationException("Emailing is not supported on this platform. Please contact support@opencoding.net and I'll do my best to support it!");
  58. #endif
  59. }
  60. public static void CopyTextToClipboard(string text)
  61. {
  62. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
  63. #if UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0
  64. var textEditor = new TextEditor { content = new GUIContent(text) };
  65. #else
  66. var textEditor = new TextEditor { text = text };
  67. #endif
  68. textEditor.SelectAll();
  69. textEditor.Copy();
  70. #elif UNITY_IOS
  71. _opencodingConsoleCopyToClipboard(text);
  72. #elif UNITY_ANDROID
  73. AndroidJavaClass androidJavaClass = new AndroidJavaClass("net.opencoding.console.NativeMethods");
  74. androidJavaClass.CallStatic("copyToClipboard", text);
  75. #else
  76. Debug.LogError("Copy and paste isn't supported on this platform currently. Please contact support@opencoding.net and I'll do my best to support it!");
  77. #endif
  78. }
  79. public static float GetNativeScreenScaleFactor()
  80. {
  81. #if UNITY_EDITOR || !UNITY_IOS
  82. return 1.0f;
  83. #else
  84. return Screen.width / (float)_opencodingConsoleGetNativeScreenWidth();
  85. #endif
  86. }
  87. }
  88. }