Unity3d SDK Android development (1)

Source: Internet
Author: User

Unity3d SDK Android development (1)

Android project interaction and sdk addition Guide: (Full Version)

There are two solutions to connect u3d with android natvice:

 

. U3D project interaction solution:

A. using u3d with jni and using jar solution for direct processing, the principle is to directly package apk in u3d. In fact, the processing process is to build the directory of the relevant project, c #, Which is packaged in u3d, interacts with java. The underlying ndk is completely ignored. You can directly use the jni interface provided by u3d to interact with the class address in jar;

B. In the traditional method, the eclipse android project adopts the ndk jni communication method;

 

Next we will introduce solution:

Android lifecycle:

. Create sdk native idea: Jump to the new activity in the interactive activity to display the part;

 

 

Solution a Configuration Guide:

1. Android Project Creation Guide

At least configuration: (otherwise it may be pitted)

. Minimum Required SDK: API 14 Android 4.0

. Target SDK: API 19 Android 4.4

. Compile With: API 19 Android 5.1.1

Theme :......

 

Activity Name :......

Complete Project creation;

 

2. project structure:

 

. Src: the original file module. You must append a package name;

. Libs: add an sdk class library package corresponding to unity;

. Res/laytou: add the layout management module as needed;

. AndroidManifest. xml: added the xml android project layout;

 

 

3. Find the libs Library File class. jar supported by unity3d:

In the u3d installation directory

../Contents/PlaybackEngines/AndroidPlayer/release/bin/classes. jar

../Contents/PlaybackEngines/AndroidPlayer/development/bin/classes. jar

 

Copy to the libs directory under AndroidPlugin.

Reference once:

We recommend that you use the jar interactive Library Under release;

 

 

4. Edit the layout file to be tested:

Layout files under. res/layout can be written at will based on the android native construction principles;



 

 

5. Return to the src project file to implement some code:

. Modify MainActivity. java :( this file is used to interact with u3d, so do not implement the setContentView method)

Delete setContentView (R. layout. activity_main );

Delete onCreateOptionsMenu (Menu menu );

Delete onOptionsItemSelected (MenuItem item );

Added: Method for interaction (c # (u3d)/java );

 

Public void StartActivity1 (String name) {// u3d call android

 

/// TODO Auto-generated method stub

 

// Intent intent = new Intent (mContext, OpenActivity1.class );

 

// Intent. putExtra ("name", name );

 

// MActivity. startActivity (intent );

 

Log. e ("U3D:", "StartActivity1:" + name );

 

}

 

 

Public void StartActivity2 (String name )//

 

{

 

UnityPlayer. UnitySendMessage ("Main Camera", "messgae", "android call u3d sucess !!! "+ Name );

 

}

 

Public void StartActivity3 (String name) {// native jump sdk, etc.

 

// TODO Auto-generated method stub

 

Intent intent = new Intent (mContext, TestActivity0.class );

 

Intent. putExtra ("name", name );

 

MActivity. startActivity (intent );

 

}

 

Supplement: If you can use Activity during debugging

U3d access needs to be modified:

 

Import com. unity3d. player. UnityPlayerActivity;

 

Public class MainActivity extends UnityPlayerActivity {// UnityPlayerActivity

// After modification, you need to re-enter eclipse clean and build;

 

 

 

6. Start packaging the class directory in bin:

Open the console and go to the bin directory of the current android project:

. Execute the java command line: jar-cvf class. jar *;

. The packaged class. jar can be renamed at will;


 

 

7. Construct the AndroidManifest File

. Create activity Grinding

Android: theme = "@ android: style/Theme. NoTitleBar. Fullscreen"

Android: name = ". OpenActivity1">

 

Add a permission file:

 

. The android part of the initial state has been created;


 

 

 

8. Go to the unity3d Project Settings section;

. Create a u3d project;

. Create the Plugins folder, create the Android folder, create the bin folder, create the libs folder, and create the res folder.

. Copy AndroidManifest to Android;

. Copy the generated jar file to the bin;

. Copy a third-party library to libs;

. Copy the res content of the current project to res. Check layout;



 

 

9. Make the relevant code:

Void Start (){

# If UNITY_EDITOR

# Elif UNITY_IPHONE

# Elif UNITY_ANDROID

AndroidJavaClass jc = new AndroidJavaClass ("com. unity3d. player. UnityPlayer ");

M_activity = jc. GetStatic ("currentActivity ");

# Endif

}

 

Void OnGUI (){

// Call hello world

If (GUI. Button (new Rect (50,50, 200,40), "Open Activity download ")){

M_activity.Call ("StartActivity1", "the android method is called up in Unity ");

}

// Call hello world

If (GUI. Button (new Rect (50,250,200, 40), "Open Activity N2 ")){

M_activity.Call ("StartActivity2", "the android method in Unity is called 222 ");

}

}

Mount the script to the Main Camera object;

 

 

10. U3D BuildSetting:


 

11. generate an apk file after packaging;


 

 

12. Set up a tomcat web server:

Mac OS: http://blog.csdn.net/kaitiren/article/details/44871181 installation;

 

 

 

Extended Part: (jump to an activity to implement the layout or add part of the sdk)

1. Go back to the android project and create the relevant code process:

Android added the native test class OpenActivity1.java:

 

Package com. example. androidplugin;

 

Import android. app. Activity;

 

Import android. OS. Bundle;

 

Import android. view. View;

 

Import android. view. View. OnClickListener;

 

Import android. widget. Button;

 

Import android. widget. TextView;

 

 

 

Public class OpenActivity1 extends Activity {

 

 

 

@ Override

 

Public void onCreate (Bundle savedInstanceState ){

 

Super. onCreate (savedInstanceState );

 

SetContentView (R. layout. activity_main );

 

 

 

TextView text = (TextView) this. findViewById (R. id. textView1 );

 

Text. setText (this. getIntent (). getStringExtra ("name "));

 

 

 

Button close = (Button) this. findViewById (R. id. button0 );

 

Close. setOnClickListener (new OnClickListener (){

 

Public void onClick (View v ){

 

OpenActivity1.this. finish ();

 

}

 

});

 

}

 

}

 

 

 

In MainActivity. java:

 

Public Context mContext = null; // Add

 

Public Activity mActivity = null; // Add

 

Protected void onCreate (Bundle savedInstanceState) {// modify

 

Super. onCreate (savedInstanceState );

 

MContext = this; // Add

 

MActivity = this; // Add

 

// SetContentView (R. layout. activity_main );

 

}

 

 

Public void StartActivity3 (String name) // android call unity

 

{

 

Intent intent = new Intent (mContext, OpenActivity1.class );

 

Intent. putExtra ("name", name );

 

MActivity. startActivity (intent );

 

}

 

 

Xml configuration:

 

 

Xmlns: android = "http://schemas.android.com/apk/res/android"

 

Xmlns: tools = "http://schemas.android.com/tools"

 

Android: id = "@ + id/container"

 

Android: layout_width = "match_parent"

 

Android: layout_height = "match_parent"

 

Tools: context = "com. ITFFuture. testua. MainActivity"

 

Tools: ignore = "MergeRootFrame">

 

 

Android: orientation = "vertical"

 

Android: layout_width = "match_parent"

 

Android: layout_height = "wrap_content">

 

 

Android: id = "@ + id/textView0"

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "wrap_content"

 

Android: textColor = "#000000"

 

Android: textSize = "18sp"

 

Android: background = "#00FF00"

 

Android: text = "test the communication between Unity and Android"

 

Android: gravity = "center_vertical | center_horizontal"/>

 

 

Android: id = "@ + id/textView1"

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "wrap_content"

 

Android: textColor = "# FFFFFF"

 

Android: textSize = "18sp"

 

Android: background = "# 0000FF"

 

Android: text = "Hello everyone, I am OpenActivity1 of android"

 

Android: gravity = "center_vertical | center_horizontal"/>

 

 

Android: id = "@ + id/button0"

 

Android: layout_width = "fill_parent"

 

Android: layout_height = "wrap_content"

 

Android: text = "Disable OpenActivity1"/>

 

 

 

 

 

 

AndroidManifest. xml:

....

 

Android: theme = "@ android: style/Theme. NoTitleBar. Fullscreen"

 

Android: name = ". OpenActivity1"> (use the package name if it cannot be identified)

 

 

 

 

 

. Compile and package the file to the u3d android binfile path. (If you do not know how to check the preceding step 6th)

After completing the java part, remember to replace the resource and AndroidManifest. xml into u3d;

 

 

2. Return to the u3d project script:

// Call hello world

If (GUI. Button (new Rect (50,400,200, 40), "Open Activity N3 ")){

M_activity.Call ("StartActivity3", "333333 ");

}

. Check all parts of the project, package and generate a new apk, and provide the apk ota download part;

After the test is completed, enter the android native sdk module to add:

Tip: Once the development Interaction module inherits UnityPlayerActivity, it cannot be debugged. You can use Activity to develop android native.

The android project may fail to run after it is changed. Simply clean the project;

 

3. How to add the Android Native sdk:

. The android native sdk usually exists in the form of a jar package. You can import and reference it to the libs folder in eclipse;

. You also need to introduce the libs folder of u3d, and create a folder if there is no folder; (the sdk does not prompt, The add method is as follows)

After the above operations are completed, go to the following operations. The idea of adding native sdk is also done by redirecting to the new activity:

 

4. Export the android project from u3d after using the sdk or adding the c # Interaction module with java;


----------------------------------------

 

 

TIPS: reduce the size of external store export:

Modifying android playersetting settings in u3d

-- Scrpping Level *: Strip Byte Code;

-- Script Call Optimization: fast


 

 

 

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.