One reason
For some reason, you need to rewrite the style of Android's soft keyboard in unity games, but unity has encapsulated the soft keyboard for Android and iOS iOS, Touchscreenkeyboard.open can open a soft keyboard in unity, However, the operating function of the soft keyboard is less pathetic, so there is this article.
Two purposes
The main purpose of this article is to rewrite Ngui's uiinput, which is the Touchscreenkeyboard.open function, which calls itself to write the Android native keyboard
Three preparations
Requires Android Environment JDK SDK and Eclise, will not install please Baidu self-install, unity and Ngui plugin
Four realization principle
First need to build an Android project, the content is very simple, as long as an activity and a edittext, and when the project to open the activity, automatically eject a soft keyboard, which is easy to achieve, when the need to open the soft keyboard in unity, Unity jumps to this activity and presses the soft keyboard's completion key to send the data back to unity.
Implementation of the five Android libraries
Public classSdkactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Resources Resources= This. Getresources ();//Load Res resourceString PackageName = This. Getpackagename ();//Package Name intid = resources.getidentifier ("Activity_sdk", "Layout", PackageName);//get the layout of the activity Super. OnCreate (savedinstancestate); Setcontentview (ID); //set the EditText in the activity in order to open the soft keyboard FinalEditText TextArea = (EditText) Findviewbyid (Resources.getidentifier ("TextArea", "id", PackageName)); Textarea.settext (""); Textarea.setbackgroundcolor (0x00000000); Textarea.settextcolor (0x00000000); Textarea.setfocusableintouchmode (true); Textarea.requestfocus (); Textarea.setcursorvisible (true); Textwatcher Textwatcher=NewTextwatcher () {@Override Public voidOnTextChanged (Charsequence S,intStartintbefore,intcount) {} @Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) {} @Override Public voidaftertextchanged (Editable s) {}}; Textarea.addtextchangedlistener (Textwatcher); //Click on the completion of the soft keyboardTextarea.setoneditoractionlistener (NewOneditoractionlistener () {@Override Public BooleanOneditoraction (TextView arg0,intarg1,keyevent arg2) {LOG.E ("Click", "Done"); SendData (0, Arg0.gettext (). toString ()); Finish (); return true; } }); } //returning data to Unity voidSendData (intcode, String info) {Unityplayer.unitysendmessage ("Plugins", "Oncustominputaction", info); } }
The code is simple to set the properties of EditText in OnCreate, SendData return data to unity after clicking the completion key of the soft keyboard.
A class is also required for unity to invoke to open the activity, which is to open the soft keyboard
Public classAndroidkeyboard {Activity context=NULL; Inputmethodmanager Inputmethodmanager=NULL; Textwatcher Textwatcher=NULL; Booleanmode =false; PublicAndroidkeyboard () {Context=unityplayer.currentactivity; Inputmethodmanager=(Inputmethodmanager) Context.getsystemservice (Context.input_method_service); } //open Activiy, and display Input method Public voidOpen (FinalString text,Final Booleanmode) { if(Context = =NULL) {LOG.E ("Unity", "context null when open keyboard"); return ; } context.runonuithread (NewRunnable () {@Override Public voidrun () {LOG.E ("Unity", "1111111111111"); Intent Intent=NewIntent (); Intent.setclassname (Context,"Com.android.keyboardsdk.SDKActivity"); Context.startactivity (Intent); } }); }}
The code has nothing to say, that is, when unity calls the Open function, it opens the activity and displays the soft keyboard
Six Unity resource settings and Androidmanifest.xml settings
The main setting is the layout of the activity above and the properties of the soft keyboard
With Acitvity XML, there's a edittext in it.
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:layout_gravity= "Center_vertical|center_horizontal" > <EditTextAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_centerhorizontal= "true"Android:inputtype= "Textnosuggestions|textmultiline"Android:ems= "Ten"android:imeoptions= "Actiondone"Android:id= "@+id/textarea"Android:layout_alignparenttop= "true"Android:layout_alignparentright= "true"Android:layout_alignparentend= "true"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true"Android:layout_alignparentbottom= "true"android:textsize= "0.01DP"android:gravity= "Top" /></Relativelayout>
Then you need to set the androidmanifest.xml in unity
< activity android:configchanges =" Keyboard|keyboardhidden|orientation " android:screenorientation = "Landscape" /span> Android:windowsoftinputmode = " Adjustresize|statevisible " Android:theme = "@android: Style/theme.translucent.notitlebar.fullscreen" android:name =" com.android.keyboardsdk.SDKActivity " > </ activity >
The main is to register the activity created above, or not open after unity call
Call this library in seven unity and open the soft keyboard
The Lib created above will be exported to unity and will be available to unity for use
Androidjavaobject _input= New Androidjavaobject ("Com.android.keyboardsdk.AndroidKeyboard");
_input. Call ("Open", text, multilines);
These two lines of code are very well understood, that is, new an instance of the Androidkeyboard class (which you created yourself), and called the Open function
Finally to receive the Android soft keyboard sent back the message, is the SendData function passed back
Unityplayer.unitysendmessage ("Plugins", "oncustominputaction", info);
You need to create a gameobject named plugins in Unity, and then hang a script that implements the Oncustominputaction function.
void Oncustominputaction (String data) {//data is the data returned by the soft keyboard }
Eight to replace the Ngui Uiinput Touchscreenkeyboard.open, and implement the relevant logic, because Ngui version is different, I don't have much nonsense
Unity rewrite soft keyboard for Android NGUI