Integration Guide for using the S Pen SDK in Unity Apps

[S Pen 2.x] Nov 26, 2012

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.

  1. Import the Unity classes.jar file
  2. Import S Pen SDK 2.2
  3. Create a MainActivity that inherits from UnityPlayerActivity
  4. Modify AndroidManifest.xml
  5. Export to a JAR file
  6. 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();
    
//--------------------------------------------
// Set S pen Touch Listener
//--------------------------------------------
    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();

        
// Generate a keycode for this event for your unity app. 
// Usage to generate KeyEvent for Unity:
// e.g.) sendKeyEvent( KeyEvent.KEYCODE_MENU);

      }

    });
    
  

// Key event generation and dispatch.
private void sendKeyDownEvent(final int keyCode) {
    // Send the down event.
    final KeyEvent keyDownEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
    this.dispatchKeyEvent(keyDownEvent);

  }

  private void sendKeyUpEvent(final int keyCode) {  
    // Send the up event.
    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) {
        // TODO Auto-generated method stub.
        Toast.makeText(MainActivity.this, "SideButtonDown", Toast.LENGTH_SHORT).show();
        sendKeyDownEvent( KeyEvent.KEYCODE_MENU);
        
      }

      @Override
      public void onTouchButtonUp(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub.
        Toast.makeText(MainActivity.this, "SideButtonUp", Toast.LENGTH_SHORT).show();
        sendKeyUpEvent( KeyEvent.KEYCODE_MENU);
        
      }

      @Override
      public boolean onTouchFinger(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub.
        return false;
      }

      @Override
      public boolean onTouchPen(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub.
        return false;
      }

      @Override
      public boolean onTouchPenEraser(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub.
        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) {


  
// Generate a keycode for this event for your unity app. 
// Usage to generate KeyEvent for Unity:
// e.g.) sendKeyEvent( KeyEvent.KEYCODE_MENU);
  
  ev.setSource(InputDevice.SOURCE_TOUCHSCREEN);
  
  
  /* To use a touch event as xxx.xyyyy -> x is touch, y is pressure. */
  // float x = FloatMath.floor(ev.getX() * 10) / 10;
  // float y = FloatMath.floor(ev.getX() * 10) / 10;
  // ev.setLocation(x + (ev.getPressure() / 100), y + (ev.getPressure()/100));
  /* In Unity script, detach the touch and pressure events from the above code. */
  // text = "pen ended: x:" +  Input.GetTouch(i).position.x + " y:" + Input.GetTouch(i).position.y;
  // text2 = "pressure:" + ((Input.GetTouch (i).position.x - (Mathf.Floor(Input.GetTouch (i).position.x * 10) /10    )) * 100);
 
  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]

[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]

[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

COPY URL twitter facebook