- Home /
 
How do I make an Android plugin for unity?
So i've been working on this for about a week now and i haven't been able to get what i wanted. Basically i want to use the sensors on an android phone for more accuracy in unity. In the Android SDK i found some useful methods that i want to use. So i created this...
 package com.eduardo.accelerometertwo;
 
 import android.os.Bundle;
 import android.hardware.*;
 import com.unity3d.player.UnityPlayerActivity;
 
 public class MainActivity extends UnityPlayerActivity implements SensorEventListener
 {
     Sensor accelerometer;
     SensorManager manager;
     static float[] gravity;
     static float[] linear_acceleration;
     
     //test
     public static float number;
     
     @Override
     protected void onCreate(Bundle savedInstanceState) 
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         
         //setting up array
         gravity = new float[3]; 
         linear_acceleration = new float[3];
         number = 19.95f;
         //acceleration code
         manager = (SensorManager) getSystemService(SENSOR_SERVICE);
         accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
         manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
     }
 
     @Override
     public void onSensorChanged(SensorEvent event) 
     {
         
         final float alpha = 0.8f;
 
         gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
         gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
         gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
     
         linear_acceleration[0] = event.values[0] - gravity[0];
         linear_acceleration[1] = event.values[1] - gravity[1];
         linear_acceleration[2] = event.values[2] - gravity[2];
     }
 
     @Override
     public void onAccuracyChanged(Sensor sensor, int accuracy) 
     {}
 
     //these are the functions that will be used by unity
     public static float xValue ()
     {
         return linear_acceleration[0];
     }
     
     public static float yValue ()
     {
         return linear_acceleration[1];
     }
     
     public static float zValue ()
     {
         return linear_acceleration[2];
     }
     
     //Tests
     public static float test ()
     {
         return number;
     }
     public static float testS()
     {
         return 19.97f;
     }
 }
 
               I've tested this program and it gets me the result i want. But when i make it into a jar file and put it into unity, it doesn't get me the results a i want. When i place values OUTSIDE of onCreate(), everything works fine and unity is able to get the values, but for example the variable "number" comes up as 0 in unity since it initiated in onCreate(). I tried extending UnityPlayerActivity to see if it would fix it, but it doesn't. This is my unity code...
 using UnityEngine;
 using System.Collections;
 
 public class testingPlugin : MonoBehaviour {
 
     // Update is called once per frame
     void Update () 
     {
         Debug.Log("first float: " + testingPlugin.testGetter());
         Debug.Log("second float: " + testingPlugin.testSGetter());
         Debug.Log("INPUT WE WANT:::::: " + testingPlugin.getXAxis());
         Debug.Log("test output");
     }
     
     public static float testGetter()
     {
         AndroidJavaClass ajc = new AndroidJavaClass("com.eduardo.accelerometertwo.MainActivity");
         return ajc.CallStatic<float>("test");
     }
     
     public static float testSGetter()
     {
         AndroidJavaClass ajcS = new AndroidJavaClass("com.eduardo.accelerometertwo.MainActivity");
         return ajcS.CallStatic<float>("testS");
     }
     
     private static float getXAxis ()
     {
         AndroidJavaClass ajcX = new AndroidJavaClass ("com.eduardo.accelerometertwo.MainActivity");
         return ajcX.CallStatic<float>("xValue");
     }
 }
 
               It's able to output the first two values, and the last value as well but it cannot print out the x value we want. I also added this manifest file...
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.eduardo.accelerometertwo"
     android:versionCode="1"
     android:versionName="1.0" >
 
     <uses-sdk
         android:minSdkVersion="21"
         android:targetSdkVersion="21" />
 
     <application
         android:label="@string/app_name" 
         android:icon="@drawable/app_icon">
         <activity
             android:name=".MainActivity"
             android:label="@string/app_name" 
             android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>
 
 </manifest>
 
               The .jar and AndroidManifest.xml are but in unity under Assets/Plugin/Android and my program works fine when only the .jar file is in the android folder, but it doesn't give the correct results, but when i put the manifest file in the android folder, it won't start on the android phone.
Somebody please help, if anything, just explain how to make a plugin from scratch. I'm completely new to plugins so please be detailed, thanks in advance.
Your answer