Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Pleng · Aug 30, 2014 at 06:45 AM · androidpluginjava

Send message to Java Code (without a plugin)?

I'm very confused when it comes to communicating between Unity (C#) scripts and my Java android activity.

To send a message from Java to Unity I just call the static method UnityPlayer.UnitySendMessage and this works fine.

Is there not some easy way to do the reverse? All the 'tutorials' I've seen talk about having to build a plugin first (why???), and they aren't exactly clear on the process of doing so.

So I have two questions:

  1. Is there any equivalent unity/c# of UnitySendMessage, that will deliver a message to UnityNativePlayerActivity to handle?

  2. If not, what is the process for building a plugin? Do I need to define all methods to be called in that plugin, or is it some kind of blank skeleton project? (all the documentation I've seen seem to involve creating pretty empty java projects)

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
2
Best Answer Wiki

Answer by liortal · Aug 31, 2014 at 06:25 PM

In order to call from Unity (C# code) to the Java VM (UnityNativeActivity, etc), you use the AndroidJavaObject and AndroidJavaClass classes.

The reason the documentation mentions that you have to build a Java plugin, is that the code you'd like to call probably doesn't exist yet (they assume it is some custom functionality that you're going to add and call to from your C# code).

If, however, you'd like to call some existing Java methods that exist on the Activity class, you can do that without building a plugin:

For example, the Activity class in Android exposes a public getTaskId() method. If you want to call it, you don't need to build a plugin:

 using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
 { 
     using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
     {
         // Call the getTaskId() method
         int result = jo.Call<int>("getTaskId");
     }
 } 

The UnityPlayer class exposes a static currentActivity field with the game's main activity. Once you get a reference to it, you can use it to call a method on it.

See more examples here: http://docs.unity3d.com/Manual/PluginsForAndroid.html

Comment
Add comment · Show 1 · 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 Pleng · Sep 02, 2014 at 07:27 AM 0
Share

Thank you! If only the documentation could be as clear as your answer :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I know if I'm calling a plugin correctly? 0 Answers

How do you access a custom Java class from Unity? 1 Answer

Problem in android java plugin 0 Answers

Compass java plugin for Unity Android 4 Answers

Calling static jar function from Unity3D 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