Unity3D调用原生Android和IOS复制粘贴功能

Unity

今天要实现用Unity调用设备的复制到粘贴板功能,Unity没有实现这个功能,所以需要调用设备原生的功能了,在网上找了一下,不算太多而且大多都不能使用,或者一使用程序就卡死的情况。没办法只能靠自己了,但对于Android和IOS开发的小白的我来说,自己实现是不可能的了,这辈子都不可能的。

不过还好,今天我在网上找到一篇靠谱的文章:http://www.andrewnoske.com/wiki/Unity_-_Clipboard

下面的代码就是Unity怎么使用这个插件,包括复制到粘贴板和粘贴功能都已经详细说明怎么使用了。

using UnityEngine;
using System.Runtime.InteropServices;

public class UniClipboard
{
  static IBoard _board;
  static IBoard board{
    get{
      if (_board == null) {
        #if UNITY_EDITOR
        _board = new EditorBoard();
        #elif UNITY_ANDROID
        _board = new AndroidBoard();
        #elif UNITY_IOS
        _board = new IOSBoard ();
        #endif
      }
      return _board;
    }
  }

  public static void SetText(string str){
    Debug.Log ("SetText");
    board.SetText (str);
  }

  public static string GetText(){
    return board.GetText ();
  }
}

interface IBoard{
  void SetText(string str);
  string GetText();
}

class EditorBoard : IBoard {
  public void SetText(string str){
    GUIUtility.systemCopyBuffer = str;
  }

  public string GetText(){
    return GUIUtility.systemCopyBuffer;
  }
}

#if UNITY_IOS
class IOSBoard : IBoard {
  [DllImport("__Internal")]
  static extern void SetText_ (string str);
  [DllImport("__Internal")]
  static extern string GetText_();

  public void SetText(string str){
    if (Application.platform != RuntimePlatform.OSXEditor) {
      SetText_ (str);
    }
  }

  public string GetText(){
    return GetText_();
  }
}
#endif

#if UNITY_ANDROID
class AndroidBoard : IBoard {

  AndroidJavaClass cb = new AndroidJavaClass("jp.ne.donuts.uniclipboard.Clipboard");

  public void SetText(string str){
    Debug.Log ("Set Text At AndroidBoard: " + str);
    cb.CallStatic ("setText", str);
  }

  public string GetText(){
    return cb.CallStatic<string> ("getText");
  }
}
#endif

本人测试完美成功

重点就是原生功能的实现了。没关系我已经在下面送出了整个工程源码,里面包含了Android的Jar文件,和IOS的.m文件。

送上整个工程源码:https://pan.baidu.com/s/1mUiw5HK4GYU5y8ZdbmqdDA

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值