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
1
Question by borutkolar · Apr 22, 2018 at 11:39 AM · androidsendmessage

Send data from Android to Unity

I am developing a project which includes Android app and Unity app. The main app is Android which at some point opens the Unity app. I exported the Unity app as Android plugin and integrated it in the Android studio as AAR module. When I open Unity app from Android I have to send a variable (integer) and retrieve it in Unity. Depending on this integer then I activate different objects. My question is how to send this integer in Android and then retrieve it in Unity? I read and tried a lot, used "UnityPlayer.UnitySendMessage" in Android and "AndroidJavaObject" in Unity but I didn't manage to achieve what I want.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kudorado · Apr 22, 2018 at 11:47 AM

Java public class SomeJNIClass{ public static int GetInterger(){ return 0;//or something you want. } } Unity public class SomeClass : MonoBehaviour{ void Start(){ var someJNI = new AndroidJavaObject("com.package.SomeJNIClass"); int value = someJNI.GetStatic("GetInterger"); } }

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 borutkolar · Apr 22, 2018 at 01:42 PM 0
Share

I tried this already and now again but it didn't work. I will paste the code because probably I am doing something wrong. Package name of Android app is "android.unityembed" and of Unity is "com.borut.multiple".

Java code:

 import com.borut.multiple.UnityPlayerActivity;
 
 public class Start extends AppCompatActivity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.start);
 
         Button button = (Button) findViewById(R.id.button1);
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Intent intent = new Intent(getApplicationContext(), UnityPlayerActivity.class);
                 startActivity(intent);
             }
         });
     }
 
     public static int getInteger() {
         return 1;
     }
 }

Unity code:

 public class NormalStart : $$anonymous$$onoBehaviour {
     public GameObject space1;
     public GameObject space2;
 
     // Use this for initialization
     void Start () {
         StartCoroutine (LoadDeviceInit ("", false));
         PlayerPrefs.SetInt ("VR$$anonymous$$ode", 2);
         var someJNI = new AndroidJavaObject ("android.unityembed.Start");
         int value = someJNI.GetStatic<int> ("getInteger");
         if (value == 1) {
             space1.SetActive (true);
         } else {
             space2.SetActive (true);
         }
     }
     
     // Update is called once per frame
     void Update () {
     }
 
     IEnumerator LoadDeviceInit(string newDevice, bool enable)
     {
         Debug.Log ("LoadDevice: " + newDevice + " enabled: " + enable);
         VRSettings.LoadDeviceByName(newDevice);
         yield return null;
         VRSettings.enabled = enable;
         yield return null;
         Camera.main.ResetAspect();
 
     }
 }

When I open Unity app (on button click) none of the objects "space1" and "space2" become active.

avatar image
0

Answer by joshuaGnol · Feb 18, 2020 at 06:17 AM

https://www.youtube.com/watch?v=EElBAGkjPt4

you might want to check this out. it helps me perfectly.

Comment
Add comment · 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
0

Answer by degee147 · Dec 01, 2020 at 02:18 PM

Please see an answer here, It took me a while as well but I figured it out: https://stackoverflow.com/a/65092095/1828990

Comment
Add comment · 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

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

175 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 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

C# Script from Native Android Service 1 Answer

SendMail from Android simply doesn't work 0 Answers

UnitySendMessage on Android 1 Answer

Android SendMessage 1 Answer

App in background still receive UnityPlayer.UnitySendMessage from Android plugin, normal ? 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