Introduction
This guide demonstrates how to write Android Native codes to use S Pen events and how to use it within a Unity app by integrating the S Pen event in Unity.
It takes some work as Android Native code must be written and integrated as a plug-in. When you working with this integration, there is an advantage to being able to customize and use all S Pen events and the features of the S Pen Library in any way you wish.
The overall order of tasks is as follows.
- Import the Unity classes.jar file
- Import S Pen SDK 2.2
- Create a MainActivity that inherits from UnityPlayerActivity
- Modify AndroidManifest.xml
- Export to a JAR file
- Copy the JAR file and AndriodManifest.xml to your Unity project
1. Import the Unity classes.jar file
Refer to the manual at the link below to import the Unity classes.jar file.
http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html
2. Import S Pen SDK 2.2
Refer to the S Pen technical document at the link below and import the S Pen SDK 2.2.
http://developer.samsung.com/s-pen-sdk/technical-docs
3.Create a MainActivity that inherits from UnityPlayerActivity
The code below shows how to create a MainActivity that inherits from UnityPlayerActivity.
First, the SPenEventLibrary is initialized and each event is registered by using registerSPenDetachmentListener.
public class MainActivity extends UnityPlayerActivity {
SPenEventLibrary mSPenEventLibrary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSPenEventLibrary = new SPenEventLibrary();
mSPenEventLibrary.registerSPenDetachmentListener(this,new SPenDetachmentListener(){
@Override
public void onSPenDetached(boolean bDetached) {
if(bDetached) Toast.makeText(MainActivity.this, "S Pen Detached", Toast.LENGTH_SHORT).show();
else Toast.makeText(MainActivity.this, "S Pen Inserted", Toast.LENGTH_SHORT).show();
}
});
private void sendKeyDownEvent(final int keyCode) {
final KeyEvent keyDownEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
this.dispatchKeyEvent(keyDownEvent);
}
private void sendKeyUpEvent(final int keyCode) {
final KeyEvent keyUpEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
this.dispatchKeyEvent(keyUpEvent);
}
mSPenEventLibrary.setSPenTouchListener(this.getWindow().getDecorView().getRootView() ,new SPenTouchListener(){
@Override
public void onTouchButtonDown(View arg0, MotionEvent arg1) {
Toast.makeText(MainActivity.this, "SideButtonDown", Toast.LENGTH_SHORT).show();
sendKeyDownEvent( KeyEvent.KEYCODE_MENU);
}
@Override
public void onTouchButtonUp(View arg0, MotionEvent arg1) {
Toast.makeText(MainActivity.this, "SideButtonUp", Toast.LENGTH_SHORT).show();
sendKeyUpEvent( KeyEvent.KEYCODE_MENU);
}
@Override
public boolean onTouchFinger(View arg0, MotionEvent arg1) {
return false;
}
@Override
public boolean onTouchPen(View arg0, MotionEvent arg1) {
return false;
}
@Override
public boolean onTouchPenEraser(View arg0, MotionEvent arg1) {
return false;
}
});
}
If needed, dispatchTouchEvent(MotionEvent ev) can be overridden to modify the event before the event is transferred to Unity.
The code below is used to convert a Pen event to a Touch event and transfer it to Unity.
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
ev.setSource(InputDevice.SOURCE_TOUCHSCREEN);
return super.dispatchTouchEvent(ev);
}
4. Modify AndroidManifest.xml
Once you have written the code as shown above, you must launch the MainActivity in AndroidManifest.xml and modify the <intent-filter> of the MainActivity as shown below.
<activity
android:name="com.samsung.unityspeninput.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
5. Export to a JAR file
Now, create a JAR file. When exporting the JAR file, set the options as shown below.
[Figure 1: JAR Export screen]
6. Copy the JAR file and AndriodManifest.xml to your Unity project This is the final step. When you add the exported JAR file, AndroidManifest.xml and S Pen SDK’s jar file to Unity using the folder structure shown below, the integration is complete.
[Figure 2: Plugins folder in Unity Editor]
Refer to the Unity site at the link below for more details regarding writing Unity Plug-ins.
http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html