上一篇章節 "Android Studio 匯出 JAR 檔 - 加入 Unity classes.jar" 已經說明了如何將 Android 打包成 JAR 檔了。
並且我們也在 Android 中建立了一個 Function,而該 Function 會去呼叫一個彈出視窗。

現在,我們要在 Unity 中去使用該 Function,詳細作法如下:

步驟一:
先在 Assets 中建立資料夾 Assets > Plugins > Android。
image01.jpg 


步驟二:
將剛剛匯出的 AndroidPlugin.jar 放入 Assets > Plugins > Android 資料夾中。
image02.jpg



步驟三:
找到 Unity 安裝目錄下的 Unity > Editor > Data > PlaybackEngines > androidplayer > AndroidManifest.xml
image10.jpg

 

補充說明,新版 Unity 的路徑為 Unity > Editor > Data > PlaybackEngines > AndroidPlayer > Apk > AndroidManifest.xml
image11.jpg


將 AndroidManifest.xml 複製貼到 Assets > Plugins > Android 資料夾中。
image03.jpg

 


步驟四:
開啟 AndroidManifest.xml,將我們在 Android Studio 中新增的 Activity 加進 application 裡面,完成後會跟下面一樣。

 
<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"          
    package="com.unity3d.player"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">
    
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>

        <application
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:icon="@drawable/app_icon"
            android:label="@string/app_name"
            android:debuggable="true">

            <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
            </activity>

            <!-- 添加我們剛剛建立的 Activity -->
            <activity android:name="com.test.tw.test.MyDialog" />
        </application>
</manifest>
 



步驟五:
建立一個 C# 腳本,命名隨意。

image04.jpg

image05.jpg

 


步驟六:
建立一個 GameObject,命名隨意。

image06.jpg

image07.jpg

 


步驟七:
將剛剛建立的 C# 腳本拖曳到剛剛建立的 GameObject 裡面。
image08.jpg 


步驟八:
開啟剛剛建立的 C# 腳本,並且修改如下。

 
using UnityEngine;
using System.Collections;


public class MyAndroid : MonoBehaviour
{
    private void OnGUI()
    {
        Rect rect = new Rect( 0.0f, 0.0f, 200.0f, 100.0f );
        if ( GUI.Button(rect, "呼叫 Android") )
        {
            #if UNITY_ANDROID && !UNITY_EDITOR

            using ( AndroidJavaClass unity = new AndroidJavaClass("com.test.tw.test.MyDialog") )
            {
                unity.CallStatic( "Show", "這是標題", "這是內文" );
            }

            #endif
        }
    }

}
 


上面是建立一個按鈕,在按鈕按下後會執行我們在 Android Studio 中建立的 Function,並且傳入參數。
new AndroidJavaClass("com.test.tw.test.MyDialog") 這是通知系統去開啟該 Class,所以你的 package 跟 Class 名稱要注意別打錯了。
並且要記得加到 AndroidManifest.xml 裡面 (步驟四),這步驟若錯誤,APP 就會報錯並退出。


步驟九:
完成!打包 APK 並在手機上執行吧!
image09.png 


若需要 Unity 詳細的打包 APK 步驟的話,請直接參考這篇文章 "Unity 導出 Android APK 檔"

 

範例下載:https://drive.google.com/file/d/0B0QPre3qvuw-b3ZCVzBOUHIzTVE/view?usp=sharing&resourcekey=0-FFY2UTAjw6amWY0AM0pKlw

 

該教學的一系列文章:
Android Studio 匯出 JAR 檔
Android Studio 匯出 JAR 檔 - 加入 Unity classes.jar
Android Studio 匯出 JAR 檔 - 在 Unity 中調用 Android Function
Android Studio 匯出 JAR 檔 - 在 Android 中呼叫 Unity Method

 

可能會遇到的問題:
Android Studio 匯出 JAR 檔 - 呼叫 Function 無反應
Android Studio 匯出 JAR 檔 - broken class file

 

 

arrow
arrow

    岳 發表在 痞客邦 留言(1) 人氣()