Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 e-cobra · Jul 01, 2019 at 06:55 PM · androidpluginjava to c#

Overhead of calling a method from .aar plugin?

I have created a native android plugin aar and have imported it into Unity. I'm going to use it for Bluetooth communication to control a real RC car using Arduino. Doing so will require me to call a java a method every time a UI control like a joystick in Unity is moved.

Like for example,

  AndroidJavaClass javaOb = new AndroidJavaClass("com.eg.comp.myApp.ExampleClass");
  javaObj.CallStatic("inputX", "200");  // Here Im calling the method InputX with the joystick value

The above call will happen multiple times when the user moves the UI controls. I want to know what is the overhead of calling native methods like this? Will it cause FPS drop in my Unity app with nothing but just some small UI controls?

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 01, 2019 at 09:59 PM

This shouldn't be too expensive. However it's not the cheapest thing. Obviously you should cache your AndroidJavaClass and not create a new one each frame. Apart from that the AndroidJavaClass is just a wrapper of the more low level JNI interface wrapper. To avoid any memory allocation and overhead, it would probably be better to grab the actual method reference manually in Start. Also you really shouldn't use string parameters.


So you could use the AndroidJavaClass to get the class reference. Then you can use the GetRawClass method to get the actual class reference. Now you can use AndroidJNI.GetStaticMethodID with the class reference we just got, the method name and the "parameter definition string". With the returned reference you can directly call AndroidJNI.CallStaticVoidMethod. You should also cache the jvalue array that you need to pass the parameter(s).


As i said previously you shouldn't use a string as parameter. Strings are objects and requires an actual string object to be allocated on the java side. You also would need to allocate a new string each time on the C# side. Declare the method on the Java side with a float, double or int parameter instead. This allows the most efficient data transfer.


To sum up it should look something like this:

using System;

 // [...]
 
 private AndroidJavaClass exampleClass;
 private IntPtr exampleClassPtr;
 private IntPtr inputXMethod;
 private jvalue[] argumentList;
 
 void Start()
 {
     exampleClass = new AndroidJavaClass("com.eg.comp.myApp.ExampleClass");
     if (exampleClass == null)
         throw new Exception("Can't find native Java class 'com.eg.comp.myApp.ExampleClass'");
 
     exampleClassPtr = exampleClass.GetRawClass();
     if (exampleClassPtr == IntPtr.Zero)
         throw new Exception("Native Java class 'com.eg.comp.myApp.ExampleClass' not initialized / not found");
 
     inputXMethod = AndroidJNI.GetStaticMethodID(exampleClassPtr, "inputX", "(F)V");
     if (inputXMethod == IntPtr.Zero)
         throw new Exception("Can't locate static method 'inputX' in native java class");
     argumentList = new jvalue[1];
 }
 
 void CallInputX(float aValue)
 {
     if (inputMethod == IntPtr.Zero)
         return;
     argumentList[0].f = aValue;
     AndroidJNI.CallStaticVoidMethod(exampleClassPtr, inputXMethod, argumentList);
 }

Note that this assumes that your inputX method now takes a float argument. So it's defined in Java as

 void inputX(float someName)
 {
     // [ ... ]
 }

If you want to use a different argument type, you need to change a few things. First of all the method signature string need to be changed to the type you want to use. F = float, D = double, J = long, I = int. Likewise you have to use the correct "member" of tje jvalue when you assign your parameter. Unity named them just like the signature letter, but with a lower case letter. So argumentList[0].d for double or argumentList[0].i for an int value.


Of course I haven't tested this code example. Though you may want to look up some general JNI documentation like the method signature format table. Be warned if you want to actually use a string parameter there's no way around a per frame memory allocation. Also you have to be super careful with native string allocation and deallocation. Messing something up with JNI can instantly crash your application and the errors are in most cases pretty useless.


Since the AndroidJavaObject / AndroidJavaClass class is not very well documented you may want to have a look at the implementation of those to understand what they actually do under the hood.


edit
Just realised that you have to use "GetStaticMethodID" instead of GetMethodID since we have a static method and not an instance method. I've fixed the answer.

Comment
Add comment · Show 2 · Share
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
avatar image Optrix · Jul 01, 2019 at 10:20 PM 0
Share

Very nice answer!

avatar image Bunny83 · Jul 01, 2019 at 10:23 PM 0
Share

Note ins$$anonymous$$d of GetStatic$$anonymous$$ethodID you may want to use

 AndroidJNIHelper.Get$$anonymous$$ethodID(exampleClassPtr, "inputX", "(F)V", true);

which see$$anonymous$$gly is more reliable as it uses Java-side reflection to get the method id. It will use "GetStatic$$anonymous$$ethodID" as fallback automatically if it fails for some reason.

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.

Follow this Question

Answers Answers and Comments

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Phone number of android mobile device - Android native code to Unity c# 3 Answers

How to convert Dictionary to Java HashMap 0 Answers

Android Multi-Plugins 0 Answers

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

Update Notification 1 Answer


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