Unity和android通訊

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   ar   

前言:

最近做Unity接入各平台SDK,看了雨松MOMO部落格(http://www.xuanyusong.com/archives/667),更新SDK包到最新的4.4,做了個demo,出現各種問題,為瞭解決這些問題,

整了整整2天才跑通。現記錄下來以後查看。

1:eclipse建立android工程項目UnitySDK


注意:

Minimum Required SDK 選擇最低API 9,因為最新的SDK包 使用的 setContentView(R.layout.activity_main); startActivity(intent); 

需要API9,不然就會提示錯誤:Call requires API level 9 (current min is 8): android.app.NativeActivity#setContentView。

而eclipse又很難檢測出此錯誤,即使 android:minSdkVersion="8" ,eclipse打包沒有修改的sdk包apk仍正常在真機上運行顯示activity。

但是,一旦匯入到unity工程中產生apk後,就會顯示不出要啟動的activity,載入setContentView(R.layout.activity_main) 報錯:

android.content.res.resoureces$NotFoundException  提示無法找到 activity_main 的ID。

  為了這個問題,痛苦了1天,不知道哪裡出問題。因此在建立android工程後,最好手動檢測下:

右鍵工程項目->android tools -> 點擊 run lints: check common errors ,來顯示目前項目上的錯誤。如果 android:minSdkVersion= 低於api對應的版本就會提示出來。


2:建立主Activity

Unity程式一起動就會調用這個MainActivity.java,它是在AndroidManifest.xml中配置的。它需要繼承unity的UnityPlayerActivity類。

因此需要unity提供的android的classes.jarr檔案,

路徑:E:\unity4.5\Data\PlaybackEngines\androidplayer\development\bin 下可以找到。

需要匯入到android工程裡面,看android工程是否存在libs檔案夾,sdk更新後,

每次建立android工程都會自動建立libs的。我們需要把classes.jar 檔案放入libs裡面。


對工程按F5重新整理,則會自動匯入到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屬於Unity程式的子Activity所以它不需要繼承UnityPlayerActivity,直接繼承Activity即可。
<span style="font-family: Arial, Helvetica, sans-serif;">Activity_main.xml: 預設的</span>
<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:修改下AndroidManifest.xml檔案,添加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="16" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.unitysdk.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name="com.example.unitysdk.TestActivity0"            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"            >        </activity>    </application></manifest>



4:修改res/values,values-xx裡面的styles.xml,刪掉menu裡面的檔案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">改為<style name="AppBaseTheme" parent="android:Theme.Light"> 同理,將<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">改為<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">不然在unity產生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.Light'.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.AppCompat.Light.DarkActionBar'.



5:匯出此工程的jar檔案
不要手動用jar命令去匯出,直接右鍵工程項目->Export->Java->JAR file
一直預設,點擊Finish就匯出了sdk.jar檔案了。
6:建立unity工程,放入sdk.jar檔案。
Unity工程中檔案夾的結構如下,Plugins->Android的名稱不能修改。把android工程檔案:res檔案夾,AndroidManifest.xml
拷貝到unity的Assets/Plugins/Android/ 目錄下。Sdk.jar也放到此目錄下。
</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(100)))        {            AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");            AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");            jo.Call("StartActivity0", "第一個Activity");        }    }}

8:最後要注意Unity中的包名,要和Android工程保持一致,否則無法調用。如所示,Bundle Identifier* 當前項目為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打包產生APK。
</pre><pre name="code" class="csharp" style="font-family:Verdana,'Microsoft YaHei'">


Unity和android通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.