Unity3d The Android platform Clipboard implementation

Source: Internet
Author: User

Also in the internet turned a lot of information, here refer to the Netizen's http://www.cnblogs.com/xiaozefeng/p/Unity_Android_IOS.html

But he didn't write it all, which led to the Android program eclipse will end up with an error

He wrote here

Unity3d Call Android Clipboard
 Public classClipboardtools { Public StaticClipboardmanager Clipboard =NULL; //add text to the Clipboard   Public voidCopytexttoclipboard (Final context context, Final String str) {if(Lopper.mylooper () = =NULL) {looper.prepare (); } Clipboard=(Clipboardmanager) Activity.getsystemservice (Activity.clipboard_service); Clipdata Textcd= Clipdata.newplaintext ("Data", str);    Clipboard.setprimaryclip (TEXTCD);  Handler.getlooper (). Quit (); }  //get text from the Clipboard   PublicString Gettextfromclipboard () {if(Clipboard! =NULL&& Clipboard.hasprimaryclip () &&clipboard.getprimaryclipdescription (). Hasmimetype (Clipdescription.mimetype_text_plain)) {Clipdata Cdtext=Clipboard.getprimaryclip (); Clipdata.item Item= Cdtext.getitemat (0); returnItem.gettext (). toString (); }    return NULL; }}

However, running the program, the inevitable error

The workaround is to use the function to add text to the Clipboard as an example, because when a new thread is started, the code is as follows

     Public voidCopytexttoclipboard (Final Context activity, Final String str) {NewThread (NewRunnable () {@Override Public voidrun () {looper.prepare (); M_handler=NewHandler () { Public voidhandlemessage (Message msg) {Clipboard=(Clipboardmanager) Activity.getsystemservice (Activity.clipboard_service); Clipdata Textcd= Clipdata.newplaintext ("Data", str); Clipboard.setprimaryclip (TEXTCD);                    }                }; Message Message=NewMessage ();                M_handler.sendmessage (message);             Looper.loop ();    }}). Start (); }

That's right.

Next:

Android Clipboard Item Import Unity3d

1. Create the plugins directory under the assets directory of the Untiy3d project.

2. Create an Android directory under the plugins directory.

3. Create the Bin directory in the Android directory.

4. In the bin directory, place the jar package for the class you are writing.

And then

Unity3d calling Android Classes and methods
#ifUnity_androidAndroidjavaobject Androidobject=NewAndroidjavaobject ("Com.test.ClipboardTools"); Androidjavaobject Activity=NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer"). Getstatic<androidjavaobject> ("currentactivity");if(Activity = =NULL)    return ; //Copy to ClipboardAndroidobject.call ("Copytexttoclipboard", activity, input); //get text from the ClipboardString Text =androidobject.call<string> ("Gettextfromclipboard");#endif

Ok,ios hasn't studied yet.

Unity3d The Android platform Clipboard implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.