• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheCoolestTaco · Nov 04, 2015 at 03:35 PM · androidpluginpluginsandroidpluginandroid-manifest

How do I make an Android plugin for unity?

So i've been working on t$$anonymous$$s 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 t$$anonymous$$s...

 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(t$$anonymous$$s, 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 t$$anonymous$$s 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(), everyt$$anonymous$$ng 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. T$$anonymous$$s 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 t$$anonymous$$s 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 anyt$$anonymous$$ng, just explain how to make a plugin from scratch. I'm completely new to plugins so please be detailed, thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

android time cheat plugin problem with broadcastreceiver 1 Answer

Web plugin like uWebKit for Android? 0 Answers

How to alter the Android Manifest to use my ad-displaying activity in Unity? 0 Answers

Android plugin dependency 1 Answer

Using the MPFR number library on Android 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges