unity重写软键盘for Android NGUI - July7th
一 原因
以为某些原因,需要重写Unity游戏中Android的软键盘的样式,然而unity对android和ios的ios的软键盘都有封装,在Unity中TouchScreenKeyboard.Open可以打开一个软键盘,然而对这个软键盘的操作函数少的可怜,所以有了这篇文章.
二 目的
本文的主要目的是重写NGUI的UIInput,也就是TouchScreenKeyboard.Open函数,调用自己写的Android原生的键盘
三 准备
需要Android的环境 jdk sdk 还有eclise,不会安装的请百度自行安装,unity 和 ngui插件
四 实现原理
首先需要建立一个Android的工程,里面内容很简单,只要一个Activity和一个EditText,并且当打开这个工程Activity的时候,自动弹出一个软键盘,这很容易实现,当unity中有需要打开软键盘的时候,unity跳转到这个Activity,按下软键盘的完成键把数据传回到Unity.
五 Android库的实现
- public class SDKActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- Resources resources = this.getResources();//加载res资源
- String packageName = this.getPackageName();//包名
- int id = resources.getIdentifier("activity_sdk", "layout", packageName);//获取Activity的layout
- super.onCreate(savedInstanceState);
- setContentView(id);
- //设置Activity中的EditText,为了打开软键盘
- final EditText 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 = new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- };
- textArea.addTextChangedListener(textWatcher);
- //点击了软键盘的完成
- textArea.setOnEditorActionListener(new OnEditorActionListener()
- {
- @Override
- public boolean onEditorAction(TextView arg0, int arg1,KeyEvent arg2)
- {
- Log.e("点击", "完成");
- SendData(0,arg0.getText().toString());
- finish();
- return true;
- }
- });
- }
- // 向unity返回数据
- void SendData(int code, String info)
- {
- UnityPlayer.UnitySendMessage("Plugins", "OnCustomInputAction",info);
- }
- }
代码很简单,就是在OnCreate中设置一下EditText的属性,在点击的软键盘的完成键后SendData向Unity返回数据
还需要一个类用来供Unity调用,用来打开这个Activity,也就是打开这个软键盘
- public class AndroidKeyboard
- {
- Activity context = null;
- InputMethodManager inputMethodManager = null;
- TextWatcher textWatcher = null;
- boolean mode = false;
- public AndroidKeyboard()
- {
- context = UnityPlayer.currentActivity;
- inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
- }
- //打开Activiy,并且显示输入法
- public void Open(final String text,final boolean mode)
- {
- if(context == null)
- {
- Log.e("unity", "context null when open keyboard");
- return ;
- }
- context.runOnUiThread(new Runnable()
- {
- @Override
- public void run()
- {
- Log.e("unity", "1111111111111");
- Intent intent = new Intent();
- intent.setClassName(context, "com.android.keyboardsdk.SDKActivity");
- context.startActivity(intent);
- }
- });
- }
- }
代码没什么好说的,就是Unity调用Open函数的时候打开这个Activity,并且显示软键盘
六 unity资源设置和AndroidManifest.xml设置
主要是设置上面的Activity的layout和软键盘的属性
附上Acitvity的xml,里面就放了一个EditText
- <RelativeLayout xmlns: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" >
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_centerHorizontal="true"
- android:inputType="textNoSuggestions|textMultiLine"
- android:ems="10"
- 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>
然后需要在Unity的AndroidManifest.xml中设置
- <activity
- android:configChanges="keyboard|keyboardHidden|orientation"
- android:screenOrientation="landscape"
- android:windowSoftInputMode="adjustResize|stateVisible"
- android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
- android:name="com.android.keyboardsdk.SDKActivity">
- </activity>
主要是注册一下上面创建的Activity,不然在unity调用后打不开
七 unity中调用这个库,并打开软键盘
把上面创建的lib导出后放到unity中就可以提供给unity使用了
- AndroidJavaObject _input= new AndroidJavaObject("com.android.keyboardsdk.AndroidKeyboard");
_input.Call("Open", text, multiLines);
这两行代码很好理解,就是new了一个AndroidKeyboard类的实例(上面你自己创建的),并且调用了Open函数
最后还要接收一下Android软键盘传回来的消息,就是SendData函数传递回来的
UnityPlayer.UnitySendMessage(“Plugins”, “OnCustomInputAction”,info);
需要在unity创建一个名字为Plugins的GameObject,然后挂上一个脚本,实现函数OnCustomInputAction即可
- void OnCustomInputAction(string data)
- {
- //data就是软键盘传回来的数据
- }
八 替换掉NGUI UIInput的TouchScreenKeyboard.Open,并实现相关的逻辑,因为ngui版本不同,我就不多废话