Unity and Android Communications

Source: Internet
Author: User

Objective:

Recently do unity access to each Platform SDK, see the Rain Pine Momo Blog (http://www.xuanyusong.com/archives/667), Update SDK package to the latest 4.4, do a demo, there are various problems, in order to solve these problems,

It took the whole 2 days to run through. It is now recorded and viewed later.

1:eclipse Creating an Android project Unitysdk


Note :

The Minimum Required SDK chooses the lowest API 9, because the latest SDK package uses the Setcontentview (R.layout.activity_main); StartActivity (Intent);

API9 is required, otherwise the error will be indicated: call requires API Level 9 (current min is 8): Android.app.nativeactivity#setcontentview.

And eclipse is very difficult to detect this error, even if android:minsdkversion= "8", Eclipse Packaging does not modify the SDK package apk still normally run on the real machine display activity.

However, once the APK is created in the Unity project, it will not show the activity to start, loading Setcontentview (r.layout.activity_main) Error:

Android.content.res.resoureces$notfoundexception prompt could not find ID for activity_main.

for this problem, the pain for 1 days, do not know where the problem. Therefore, after creating the Android project, it is best to manually detect the following:

Right-click on the run Lints:check common errors, Project->android tools, to display errors on the current project. If the android:minsdkversion= is below the API version, it will be prompted.


2: Create the main activity

The Unity program will call this Mainactivity.java, which is configured in Androidmanifest.xml. It needs to inherit Unity's Unityplayeractivity class.

So you need the Android Classes.jarr file that unity provides,

Path: E:\unity4.5\Data\PlaybackEngines\androidplayer\development\bin can be found below.

Need to import into Android project inside, see if Android Project exists Libs folder, after SDK update,

Every time you create an Android project, Libs is created automatically. We need to put the Classes.jar file inside the libs.


When the project is F5 refreshed, it is automatically imported into the private libraries:



Mainactivity.java:

Package Com.example.unitysdk;import Android.content.context;import Android.content.intent;import android.os.Bundle Import Com.unity3d.player.unityplayeractivity;public class Mainactivity extends unityplayeractivity  {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);    }}


Testactivity0.java:

Package Com.example.unitysdk;import Android.app.activity;import Android.os.Bundle; public class TestActivity0 extends Activity {     @Override public    void OnCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }}

TestActivity0 belongs to the Unity program's child activity so it does not need to inherit unityplayeractivity, directly inherit the activity.
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "HTML" ><relativelayout xmlns:android= "http://schemas.android.com /apk/res/android "    xmlns:tools=" Http://schemas.android.com/tools "    android:layout_width=" Match_parent "    android:layout_height= "match_parent"    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_horizontal_margin"    android:paddingright= "@dimen/activity_horizontal_ Margin "    android:paddingtop=" @dimen/activity_vertical_margin "    tools:context=" Com.example.unitysdk.MainActivity ">    <textview        android:layout_width=" Wrap_content "        android: layout_height= "Wrap_content"        android:text= "@string/hello_world"/></relativelayout>


3: Modify the next Androidmanifest.xml file to add TestActivity0 activity
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.example.unitysdk "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "9" android:targetsdkversion= "/> <application android:allowbackup=" t Rue "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appt Heme "> <activity android:name=" com.example.unitysdk.MainActivity "android:label=" @stri Ng/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G                T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity&        Gt <activity android:name= "com.example.unitysdk.TestActivity0" Android:theme= "@android: STYLE/THEME.N Otitlebar.Fullscreen "> </activity> </application></manifest> 


4: Modify the res/values,values-xx inside the Styles.xml, delete the menu inside the file main.xml
</pre><pre code_snippet_id= "482409" snippet_file_name= "blog_20141011_14_7145067" name= "code" class= " CSharp "style=" Font-family:verdana, ' Microsoft Yahei '; ><pre name= "code" class= "HTML" ><style name= "Appbasetheme" parent= "Theme.AppCompat.Light" > Changed to < Style name= "Appbasetheme" parent= "Android:Theme.Light" > Similarly, <style name= "Appbasetheme" parent= " Theme.AppCompat.Light.DarkActionBar "> Changed to <style name=" Appbasetheme "parent=" Android: Theme.Holo.Light.DarkActionBar "> Otherwise error when unity generates APK: Error building Player:CommandInvokationFailure:Failed to Re-package Resources. See the Console for details. E:\android\android-sdks\build-tools\19.1.0\aapt.exe Package--auto-add-overlay-v-f-m-j gen-m androidmanifest.xml-s "Res"-i "E:/android/android-sdks/platforms/android-20\android.jar"-F bin/resources.ap_stderr[res\values\ Styles.xml:7: Error:error Retrieving parent for item:no Resource found that matches the given name ' Theme.AppCompat.Ligh T '. Res\values-v11\styles.Xml:7: Error:error Retrieving parent for item:no Resource found, that matches the given name ' Theme.AppCompat.Light '. res\ Values-v14\styles.xml:8: Error:error Retrieving parent for item:no Resource found that matches the given name ' Theme.app Compat.Light.DarkActionBar '.


5: Export the jar file for this project
Do not manually use JAR command to export, right-click Project->export->java->jar file
Always by default, click Finish to export the Sdk.jar file.
6: Create a Unity project and put in the Sdk.jar file.
The structure of the folder in the Unity project is as follows, and the plugins->android name cannot be modified. Put Android Project file: Res folder, Androidmanifest.xml
Copy to Unity's Assets/plugins/android/directory. Sdk.jar is also placed in this directory.
</pre><pre code_snippet_id= "482409" snippet_file_name= "blog_20141011_24_6056199" name= "code" class= " CSharp "style=" Font-family:verdana, ' Microsoft Yahei '; >using System.collections;public class Test:monobehaviour {    void Ongui ()    {        if (Guilayout.button ("OPEN Activity01 ", Guilayout.height (+)))        {            Androidjavaclass JC = new Androidjavaclass (" Com.unity3d.player.UnityPlayer ");            Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity");            Jo. Call ("StartActivity0", "first Activity");}}}    
8: Finally pay attention to the package name in unity, to be consistent with the Android project, otherwise it cannot be called. As shown, the Bundle identifier* current project is COM.EXAMPLE.UNITYSDK.
</pre><pre code_snippet_id= "482409" snippet_file_name= "blog_20141011_27_3799115" name= "code" class= " CSharp "style=" Font-family:verdana, ' Microsoft Yahei '; >9:build Package Build apk.
</pre><pre name= "code" class= "CSharp" style= "Font-family:verdana, ' Microsoft yahei '" >


Unity and Android Communications

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.