Unity3d Research Institute and Android exchange messages (19th)

Source: Internet
Author: User

Http://blog.csdn.net/xys289187120/article/details/7687319

In the previous article, we learned how to send messages from unity to Android. If android can give back messages to unity again, this is a wonderful thing. Just like unity for andoid and iOS, they can send and receive messages to and from unity. In this article, Momo will share a good chat with you about how Android sends messages to unity. Before reading this blog post, I suggest you read my previous article.Unity3d Research Institute: opening activity and calling Java code transfer parameters (18 ),We have already made it clear about data packaging in the previous article, so we will not repeat it here.

We create two activities in the project. One is the main activity, and the other is the newly opened activity and content, which is similar to the previous article.

Unitytestactivity. Java main activity, provides an interface method startactivity (), which is called by unity

 

 

[Java]View plaincopy

  1. Package com. xys;
  2. Import Android. content. context;
  3. Import Android. content. intent;
  4. Import Android. OS. Bundle;
  5. Import com. unity3d. Player. unityplayeractivity;
  6. Public class unitytestactivity extends unityplayeractivity {
  7. /** Called when the activity is first created .*/
  8. Context mcontext = NULL;
  9. @ Override
  10. Public void oncreate (bundle savedinstancestate ){
  11. Super. oncreate (savedinstancestate );
  12. Mcontext = this;
  13. }
  14. Public void startactivity0 (string name)
  15. {
  16. Intent intent = new intent (mcontext, testactivity0.class );
  17. Intent. putextra ("name", name );
  18. This. startactivity (intent );
  19. }
  20. }

 

 

Next, after testactivity. Java opens this interface, the program will send a message to unity.

 

 

[Java]View plaincopy

  1. Package com. xys;
  2. Import com. unity3d. Player. unityplayer;
  3. Import Android. App. activity;
  4. Import Android. OS. Bundle;
  5. Import Android. View. view;
  6. Import Android. View. View. onclicklistener;
  7. Import Android. widget. Button;
  8. Import Android. widget. edittext;
  9. Import Android. widget. textview;
  10. Public class testactivity0 extends activity {
  11. @ Override
  12. Public void oncreate (bundle savedinstancestate ){
  13. Super. oncreate (savedinstancestate );
  14. Setcontentview (R. layout. Main );
  15. Textview text = (textview) This. findviewbyid (R. Id. textview1 );
  16. Text. settext (this. getintent (). getstringextra ("name "));
  17. Final edittext edit = (edittext) This. findviewbyid (R. Id. Edit );
  18. Button close = (button) This. findviewbyid (R. Id. button0 );
  19. Close. setonclicklistener (New onclicklistener (){
  20. @ Override
  21. Public void onclick (view v ){
  22. // Note 1
  23. Unityplayer. unitysendmessage ("main camera", "messgae", Edit. gettext (). tostring ());
  24. Testactivity0.this. Finish ();
  25. }
  26. });
  27. }
  28. }

 

 

Import com. unity3d. Player. unityplayer in the program header; because the static method for sending messages to unity is written here.

Note 1: After clicking the send button, the program will send a message to unity. Unityplayer. unitysendmessage () parameter 1 indicates the name of the game object to be sent, parameter 2 indicates the method by which the script bound to the object receives the message, and parameter 3 indicates the string information sent by the message, this method is very similar to the message sending method in IOS.

Next, we open the unity project to package and copy the android plug-in into the unity project. For details about the packaging process, see the previous chapter and bind the Script test. CS to the main camera object.The unityplayer. unitysendmessage () method sends a message to the maincamera object. Therefore, the message must be received in the script bound to the main camera object, that is, in the test. CS script.

 

 

 


 

 

Next let's take a look at the test. CS script

 

 

[CSHARP]View plaincopy

  1. Using unityengine;
  2. Using system. collections;
  3. Public class test: monobehaviour
  4. {
  5. // Enter a string
  6. Private string stringtoedit = "Please enter a string ";
  7. Void Update ()
  8. {
  9. // Click the return key to close the application
  10. If (input. getkeydown (keycode. Escape) contains multiple input. getkeydown (keycode. Home ))
  11. {
  12. Application. Quit ();
  13. }
  14. }
  15. Void ongui ()
  16. {
  17. // Draw an input box to receive user input
  18. Stringtoedit = guilayout. textfield (stringtoedit, guilayout. Width (300), guilayout. Height (100 ));
  19. // A submit button
  20. If (guilayout. Button ("commit", guilayout. Height (50 )))
  21. {
  22. // Annotation 1
  23. Using (androidjavaclass JC = new androidjavaclass ("com. unity3d. Player. unityplayer "))
  24. {
  25. Using (androidjavaobject Jo = JC. getstatic <androidjavaobject> ("currentactivity "))
  26. {
  27. // Call the startactivity0 method in unitytestactivity of the android plug-in. stringtoedit indicates its parameters.
  28. Jo. Call ("startactivity0", stringtoedit );
  29. }
  30. }
  31. }
  32. }
  33. // Annotation 2
  34. Void messgae (string Str)
  35. {
  36. Stringtoedit = STR;
  37. }
  38. }

 

 

Note 1: place the code in using to tell the recycle bin to recycle the garbage in time. We recommend that you write the code in this way. Androidjavaclass represents a Java class, And androidjavaobject represents a Java object, which has been explicitly stated in the previous chapter. There are also some other methods, such as calling static methods, which are clearly written in the API. You can refer to them.

Note 2: The called method unityplayer. the unitysendmessage () parameter 2 is "message", so the program will call the test. the message method of the CS script. The parameter is passed by Android. Finally, the stringtoedit variable is modified here to refresh the UI of the screen.

 


 

 

After opening the program, the Unity screen will be started. For example, we have written an input box to receive users' keyboard input. Finally, the user clicks the commit button to open the android activity, the string "Please enter a string" is passed to the new activity as a parameter.

 


 

As shown in, in the newly opened activity, you can see that the please enter a string has been displayed on the interface. In the layout file, the android advanced control input box and button are also written, enter the string "xuanyusong" (any input) in the input box and click "save" to pass the string "xuanyusong" just entered to unity.

 


 

 

As shown in, the string "xuanyusong" in unity is displayed in the input box, which perfectly achieves message sending and receiving. We hope you will continue to support the Momo program Research Institute.

 

Source code: http://www.xuanyusong.com/archives/676

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.