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
- Package com. xys;
- Import Android. content. context;
- Import Android. content. intent;
- Import Android. OS. Bundle;
- Import com. unity3d. Player. unityplayeractivity;
- Public class unitytestactivity extends unityplayeractivity {
- /** Called when the activity is first created .*/
- Context mcontext = NULL;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Mcontext = this;
- }
- Public void startactivity0 (string name)
- {
- Intent intent = new intent (mcontext, testactivity0.class );
- Intent. putextra ("name", name );
- This. startactivity (intent );
- }
- }
Next, after testactivity. Java opens this interface, the program will send a message to unity.
[Java]View plaincopy
- Package com. xys;
- Import com. unity3d. Player. unityplayer;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. View. view;
- Import Android. View. View. onclicklistener;
- Import Android. widget. Button;
- Import Android. widget. edittext;
- Import Android. widget. textview;
- Public class testactivity0 extends activity {
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Textview text = (textview) This. findviewbyid (R. Id. textview1 );
- Text. settext (this. getintent (). getstringextra ("name "));
- Final edittext edit = (edittext) This. findviewbyid (R. Id. Edit );
- Button close = (button) This. findviewbyid (R. Id. button0 );
- Close. setonclicklistener (New onclicklistener (){
- @ Override
- Public void onclick (view v ){
- // Note 1
- Unityplayer. unitysendmessage ("main camera", "messgae", Edit. gettext (). tostring ());
- Testactivity0.this. Finish ();
- }
- });
- }
- }
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
- Using unityengine;
- Using system. collections;
- Public class test: monobehaviour
- {
- // Enter a string
- Private string stringtoedit = "Please enter a string ";
- Void Update ()
- {
- // Click the return key to close the application
- If (input. getkeydown (keycode. Escape) contains multiple input. getkeydown (keycode. Home ))
- {
- Application. Quit ();
- }
- }
- Void ongui ()
- {
- // Draw an input box to receive user input
- Stringtoedit = guilayout. textfield (stringtoedit, guilayout. Width (300), guilayout. Height (100 ));
- // A submit button
- If (guilayout. Button ("commit", guilayout. Height (50 )))
- {
- // Annotation 1
- Using (androidjavaclass JC = new androidjavaclass ("com. unity3d. Player. unityplayer "))
- {
- Using (androidjavaobject Jo = JC. getstatic <androidjavaobject> ("currentactivity "))
- {
- // Call the startactivity0 method in unitytestactivity of the android plug-in. stringtoedit indicates its parameters.
- Jo. Call ("startactivity0", stringtoedit );
- }
- }
- }
- }
- // Annotation 2
- Void messgae (string Str)
- {
- Stringtoedit = STR;
- }
- }
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