unity建立Android原生插件

1.首先打開https://developer.android.com/studio/projects/android-library學習如何建立一個Android Libraryjava

2.打開Android Studio 建立項目android

  - 輸入項目名稱app

  - 輸入Company domain或者用默認的dom

  - 點擊next學習

 

 

 

3.點擊nextgradle

 

 

4.由於是建立Android plugin 因此選擇Add No Activity點擊Finishui

 

 

 

5.建立的項目目錄以下this

 

 

 

6.lua

  +點擊File—》New—》New Module 彈出下面界面spa

         + 選擇Android Library

         + 點擊Next

 

 

7.

  + 輸入Application/Library name

    + 點擊Finish

    + 建立

 

 

 

8.建立後的目錄

 

 

9.建立JavaClass

 

 

 

10.

  + 輸入名稱

    + 點擊OK

 

 

11.  腳本內容

package com.wawagame.app.game.mediaplayerplugin;

import android.util.Log;

public class TutorialClass {
    public void LogNativeAndroidLogcatMessage(){
            Log.d("Unity", "Native LogCcat Message!!!");
    }
}
 

12.Make Moudule Library(jar 包)

   + BuildàMake Module ’mediaplayerplugin’

     

 

 

 

 

 

 

E:\work\androidStudio\mediaplayerplugin\build\intermediates\packaged-classes\debug

 

 

 



 

13.在Android Sudio

雙擊打開:build.gradle(Module:mediaplayerplugin)

 

 

14.將下面代碼複製到build.gradle建中

task exprotJar(type: Copy){

    from('build/intermediates/packaged-classes/debug/classes.jar')

    into('../../../../../../unity/PluginTurial/Assets/Plugins/Android')

    rename('classes.jar','MediaPlayerPlugin.jar')

}
task deleteOldJar(type:Delete){

    delete('../../../../../../unity/PluginTurial/Assets/Plugins/Android/MediaPlayerPlugin.jar')

}
exprotJar.dependsOn(deleteOldJar)

  

 

 

 

15.

afterEvaluate {

    assembleDebug.finalizedBy(exprotJar)

    //build.finalizedBy(exprotJar)

}

  

 

 

 

 

16.以下圖操做這樣就講classes.jar複製並重命名到Unity項目中了

 

 

17.在unity中調用javaClass

  有兩種方式:   

    1). AndroidJavaClass對象(用法見Unity Api)

    2).AndroidJavaObject 對象(用法見Unity Api)

 

  +在unity建立C#

 

    private AndroidJavaObject javaClass;

    public Text myText;

    // Use this for initialization

    void Start () {

        javaClass = new AndroidJavaObject("com.wawagame.app.game.mediaplayerplugin.TutorialClass");

        javaClass.Call("LogNativeAndroidLogcatMessage");

        string a = javaClass.Call<string>("LogNativeAndroidLogcatMessageReturn");

        Debug.Log("a=============" + a);

        myText.text = javaClass.Call<string>("LogNativeAndroidLogcatMessageReturn");
    }

    // Update is called once per frame
    void Update () {   
    }

  

 

 

二.Java 調用Unity Function

1. android Project 須要一個Unity classes.jar文件

2.複製classes.jar文件

    + 若是你的打包方式爲Mono

      複製文件是:C:\ProgramFiles\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\ classes.jar

  + 若是你的打包方式爲L2CPP

  複製文件是:C:\ProgramFiles\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes\ classes.jar

3.將classes.jar文件複製到以下圖所示的文件中

 

 

4.在build.gradle(Module:mediaplayerplugin)中加入下面代碼

include('clasess.jar')

 

 

 

 

 

 

5.調用unity使用的對象com.unity3d.player.UnityPlayer (在Unity Api 說明Building Plugins for Android能夠看到)

  使用方法:com.unity3d.player.UnityPlayer.UnitySendMessage

6.首次使用沒有自動導入怎們辦

  +  File—》Settings—》Anto Import

  +  勾上Add unambiguous imports on ths fly和 Auto import in completion

  +  點擊Apply而後點擊OK

7.在Android Sudio中的代碼:

public void CallAorB(String value){

    if("A".equals((value))){

        DoSomethingA();

    }else if("B".equals(value)){

        DoSomethingB();
    }
}

public void DoSomethingA() {

    UnityPlayer.UnitySendMessage("MyGameObject","ChangeTextA","1111111");
}
public void DoSomethingB()
{
    UnityPlayer.UnitySendMessage("MyGameObject","ChangeTextB","2222222");
}

  

8.在Unity中的代碼:

    public void CallJavaFunction(string value)
    {
        javaClass.Call("CallAorB", value);
    }
    public void OnABtnClick()
    {
        CallJavaFunction("A");
    }

    public void OnBBtnClick()
    {
        CallJavaFunction("B");
    }
相關文章
相關標籤/搜索