Unity3d調用Andriod

  最近在作一些前端的嘗試,須要用到unity3d調用android原生開發打包好的一些功能,因而就在網上搜索一些方法,發現網上的一些方法多多少少有一點錯誤!這裏本身寫一個隨筆記錄一下。前端

1、首先建立android開發好的arr包(android studio開發環境)android

1.建立android工程c#

這裏的sdk選擇比較重要,要與調試機以及等下unity工程裏面的minmun sdk 版本同樣才能夠(有些低版本的unity支持的sdk版本較低),否則會在unity編譯打包的時候報錯誤!app

二、修改android工程build.gradle(android studio環境須要),讓工程變成libraryide

三、將unity的開發包jar導入到android工程中測試

找到unity的開發包classes.jar,路徑在unity的安裝路徑中(D:\Program Files\Unity 2017.1.0b1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes\classes.jar)中,gradle

右鍵複製classes.jar,直接黏貼到android studio工程libs文件夾中ui

右鍵app,彈出下拉菜單,選擇Open module setting,在彈出的窗口中切換到Dependencies選項卡,將classes.jar包含到工程裏面this

 

4.修改class main ,Mainacity代碼讓他繼承與UnityPlayerActivity類spa

 下面是個人代碼,寫了兩個方法,一個是用來測試接口返回值,一個是用來測試調用彈窗的

package com.example.hsh.testun;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.unity3d.player.UnityPlayerActivity;

public class MainActivity extends UnityPlayerActivity {

    Context m_context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        m_context = this;
    }

    public String ShowDialog(final String _title, final String _content){
        return "Java return";
    }

    public void dialog2(){
        Log.d("dialog2","ffffffffffff");
    }

    public void dialog1(){
        AlertDialog.Builder mBuilder=new AlertDialog.Builder(m_context);
        mBuilder.setTitle("testtttt")
                .setMessage("xxxxxxxxxxxxxxxxx")
                .setPositiveButton("肯定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Whatever...
                    }
                });
        AlertDialog dialog=mBuilder.create();
        dialog.show();
    }
}

五、編譯成arr

2、建立unity工程

一、拉入Buttion跟Text組件用於測試

二、建立Plugins/Android路徑(必須是同樣名字的)

將剛纔android工程建立的arr包中classes.jar,放到unity工程的Plugins/Android路徑中,並建立AndroidManifest.xml文件

AndroidManifest.xml文件內容以下

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hsh.testun"
    xmlns:tools="http://schemas.android.com/tools"
    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"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:theme="@style/UnityThemeSelector"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
        <activity android:name=".MainActivity"
                  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>
    </application>
</manifest>

其中<activity android:name=".MainActivity">,屬性名要和本身android jar中的activity名字同樣,個人就是MainActivity,因此不用更改

三、建立c#測試腳本

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TestBtnScript: MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    public void MyShowDialog()
    {
        
        // Android的Java接口    
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        //AndroidJavaClass jc = new AndroidJavaClass("com.example.hsh.testun.MainActivity");
        //AndroidJavaObject jo = new AndroidJavaObject("com.example.hsh.testun.MainActivity");
       
        // 參數    
        string[] mObject = new string[2];
        mObject[0] = "Dialog title";
        mObject[1] = "Dialog text is here!!";
try
        {
    
            jo.Call("dialog1");
string ret = jo.Call<string>("ShowDialog", mObject); Text text4 = GameObject.Find("Canvas/Text").GetComponent<Text>(); text4.text = ret; } catch(System.Exception e) { Text text2 = GameObject.Find("Canvas/Text").GetComponent<Text>(); text2.text = e.ToString(); } // 調用方法 } }

將MyShowDialog方法綁定到button的點擊事件,就能夠了。

注意,C#代碼中,有註釋的//AndroidJavaObject jo = new AndroidJavaObject("com.example.hsh.testun.MainActivity");

若是用這個jo直接調用call去調用android中的ShowDialog方法,是能夠拿到string的返回值的,可是調用dialog1去彈窗的狀況,會出現安卓報錯nullpointer的錯誤。(具體空指針位置在代碼AlertDialog.Builder mBuilder=new AlertDialog.Builder(m_context);這一行中,具體緣由不太明白,但願有理解安卓機制的大牛告知)。

相關文章
相關標籤/搜索