Unity Mobile Copy So write

Source: Internet
Author: User

Game on-line for a long time, some players slowly lost, in order to let just lost players back again so do a recall function! If a Level 200 player is not online for 10 days and a successful recall, it will give the recall player a generous bonus!

Q: How do I recall this lost player?

A: The recall must have a similar recall code East Ah, yes. The server generates a certain number of letters and numbers based on the player ID, which is sent to the client, and the recall code is displayed on the client's recall screen

Q: Is it impossible for the player to type in a chat box? Players will certainly scold: "What silly design, I do not play,,,"

A: The player is uncle, so do copy recall it is necessary to function!

So how do you implement this function? Does unity not support replication? Yes, unity is support, only PC-side, that's the thing texteditor!

Newnew  guicontent (yourtext); text. Onfocus (); text. Copy ();

So the realization of the PC-side replication, then the mobile side of how to do it, don't worry, lad, one by one, first see how Android is copied!

First create the ANDROCU project, add this code in the mainactivity, the export jar package is called in unity, and the next thing you say is how to call unity!
Steps:

1. 在Untiy3D项目的Assets目录下创建Plugins目录。

2. 在Plugins目录下创建Android目录。

3. 在Android 目录下创建bin目录。

4. 在bin 目录下放置你编写的类的jar包

Package Com.wuzhang.testandroid;import Com.unity3d.player.unityplayer;import Com.unity3d.player.unityplayeractivity;import Android. R.string; import Android.app.activity;import Android.app.activitymanager;import Android.app.activitymanager.runningappprocessinfo;import Android.app.service;import Android.content.ClipData; Import Android.content.clipdescription;import Android.content.clipboardmanager;import android.content.Context; Import Android.os.looper;import Android.support.v4.app.notificationcompat;import Android.support.v4.content.fileprovider;import Android.view.ContextThemeWrapper; Public classMainactivity extends Unityplayeractivity {PrivateVibrator MVibrator01;//declaring a Vibrator object    Private StaticContext instance; PrivateString TAG ="Log";  Public StaticString Gameobgectname ="Main Camera";  Public StaticString MethodName ="Oncoderreturn";  Public StaticClipboardmanager Clipboard =NULL; PrivateBatterylistener Listener; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Instance=Getapplicationcontext (); }     Public StaticContext GetContext () {returninstance; }     /** Add text to the Clipboard*/     Public voidCopytexttoclipboard (Final String str) throws Exception {if(Looper.mylooper () = =NULL) {looper.prepare (); } Clipboard=(Clipboardmanager) MainActivity.instance.getSystemService (Activity.clipboard_service); Clipdata Textcd= Clipdata.newplaintext ("Data", str);      Clipboard.setprimaryclip (TEXTCD); }          /** 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"; }}
View Code

How does wirelessly call this code?

 /// <summary>    ///Cut Text/// </summary>    /// <param name= "input" ></param>     Public voidCopyToClipboard (stringinput) {#ifUnity_android//calls to AndroidAndroidjavaclass JC =NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer"); Androidjavaobject Activity= JC. Getstatic<androidjavaobject> ("currentactivity"); if(Activity = =NULL)            return; //Copy to ClipboardActivity. Call ("Copytexttoclipboard", input); //get text from the Clipboard        stringText = activity. call<string> ("Gettextfromclipboard");#endif    }

Android is good, then it is the big iOS, some people see iOS is counseling, do not know where to start, do not know how to write, completely no concept. Sorry, I and you are the same counseling, I am also a big girl on the car, the first time! Do not counsel, is to do, big deal the first time will not succeed, go to debug Bai! There is this mentality, you are still lucky, we are directly on the line project, no time to debug, when finished, I did not have a little bottom ah! The kind of super-counseling,, and later out of the iphone test package, did not expect one time, the heart of the Big Stone finally fell! So let's see how iOS is implemented, right? iOS uses OBJECTC and C languages that are similar, in OC. h file is a declaration,. m file is implemented, so you need to Clipboard.h and clipboard.mm two files!
First to declare this clipboard class,

Interface Clipboard:nsobject extern " C " {     /*  Compare the namelist with system  processes*/      void _copytexttoclipboard (constChar *textlist);} @end

Next is the clipboard.mm file, remember to never forget to quote #import "Clipboard.h"

#import"Clipboard.h"@implementation Clipboard//copy Text to the iOS clipboard- (void) Objc_copytexttoclipboard: (nsstring*) text{Uipasteboard*pasteboard =[Uipasteboard Generalpasteboard]; Pasteboard.string=text;} @endextern "C" {    StaticClipboard *Iosclipboard; void_copytexttoclipboard (Const Char*textlist) {NSString*text =[NSString stringwithutf8string:textlist]; if(Iosclipboard = =NULL) {Iosclipboard=[[Clipboard alloc] init];    } [Iosclipboard Objc_copytexttoclipboard:text]; }}

. h files and. mm files need to be placed under any path under the assets/plugins/of the Unity Project, most of the programmers are neat and tidy not put into the assets/plugins/ios/folder, the heart is uncomfortable! What is the difference between M and. mm? Really, ". M" can only write object-c code, and ". MM" in the C code. So there is extern "C" to show that we are using C code!

Unity3d calls to the iOS class

1. 在Untiy3D项目的Assets目录下创建Plugins目录。

2. 在Plugins目录下创建IOS目录。

3. 在IOS 目录下放置".h"文件和".mm"文件。

Well, quickly say how to call it, this will be 1 o'clock at night, and then wordy on the dawn! call the function declared by the. h:

// calling an interface  in Clipboard.h _copytexttoclipboard (input);  Debug.logerror ("copytoclipboard_______"

Just call on the OK, is not very disappointed! This is a call to all platforms

 Public Static voidCopyToClipboard (stringinput) {#ifUnity_android//calls to AndroidAndroidjavaclass JC =NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer"); Androidjavaobject Activity= JC. Getstatic<androidjavaobject> ("currentactivity"); if(Activity = =NULL)            return; //Copy to ClipboardActivity. Call ("Copytexttoclipboard", input);#elifUnity_iphone//calling an interface in Clipboard.h_copytexttoclipboard (input); Debug.logerror ("copytoclipboard_______"+input); #elifUnity_editortexteditor te=NewTextEditor (); Te.content=Newguicontent (input); Te.        Onfocus (); Te. Copy ();#endif    }

Well, to this finish, now feel up late to write blog events very extravagant things, sleep! The problem of the small partners remember @ me, common communication, the next write to get the phone's remaining storage space plugin!
Portal: Gitee.com/wuzhang/unitycallclipboard.git

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.