UniClipboard.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. public class UniClipboard
  4. {
  5. static IBoard _board;
  6. static IBoard board{
  7. get{
  8. if (_board == null) {
  9. #if UNITY_EDITOR
  10. _board = new EditorBoard();
  11. #elif UNITY_ANDROID
  12. _board = new AndroidBoard();
  13. #elif UNITY_IOS
  14. _board = new IOSBoard ();
  15. #endif
  16. }
  17. return _board;
  18. }
  19. }
  20. public static void SetText(string str){
  21. Debug.Log ("SetText");
  22. board.SetText (str);
  23. }
  24. public static string GetText(){
  25. return board.GetText ();
  26. }
  27. }
  28. interface IBoard{
  29. void SetText(string str);
  30. string GetText();
  31. }
  32. class EditorBoard : IBoard {
  33. public void SetText(string str){
  34. GUIUtility.systemCopyBuffer = str;
  35. }
  36. public string GetText(){
  37. return GUIUtility.systemCopyBuffer;
  38. }
  39. }
  40. #if UNITY_IOS
  41. class IOSBoard : IBoard {
  42. [DllImport("__Internal")]
  43. static extern void SetText_ (string str);
  44. [DllImport("__Internal")]
  45. static extern string GetText_();
  46. public void SetText(string str){
  47. if (Application.platform != RuntimePlatform.OSXEditor) {
  48. SetText_ (str);
  49. }
  50. }
  51. public string GetText(){
  52. return GetText_();
  53. }
  54. }
  55. #endif
  56. #if UNITY_ANDROID
  57. class AndroidBoard : IBoard {
  58. AndroidJavaClass cb = new AndroidJavaClass("jp.ne.donuts.uniclipboard.Clipboard");
  59. public void SetText(string str){
  60. Debug.Log ("Set Text At AndroidBoard: " + str);
  61. cb.CallStatic ("setText", str);
  62. }
  63. public string GetText(){
  64. return cb.CallStatic<string> ("getText");
  65. }
  66. }
  67. #endif