Unity与java相互调用

新建Android工程,将Unity中关于编译Android程序的Jar包导入我们的项目中,

project - Properties - Java Build Path - Add External JARs...

Jar包路径一般为:%Unity安装目录%\Editor\DataPlaybackEngines\androidplayer\development\bin\classes.jar

让MainActivity继承UnityPlayerNativeActivity

温馨提示:在Eclipes中自动import的快捷键为ctrl+shift+o

MainActivity最基本的只需保留onCreate函数,

@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

接下来开始写接口,一般接口分为两种,静态和非静态,


静态函数调用

首先说一下静态函数接口的调用,假设我们的java类如下:

// MainActivity.java

package com.Sealy.FingerTest;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;

import com.unity3d.player.UnityPlayer;
import com.Sealy.FingerTest.UnityPlayerNativeActivity;

public class MainActivity extends UnityPlayerNativeActivity
{

    public static int myAdd(int a,int b)
    {
        return a+b;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }
}

然后打包成jar包,放到Unity工程中的Assets\Plugins\Android\libs目录下,注意,如果只需打包java代码,则只需点击src打包

在Unity中,构造一个AndroidJavaClass对象,然后通过CallStatic接口进行调用

private AndroidJavaClass jc;
jc = new AndroidJavaClass("com.Sealy.FingerTest.MainActivity");
int sum = jc.CallStatic<int>("myAdd",1,3);

这是最简单的调用类的静态方法,当然我们可以写成非静态方法,将java类写成单例,供外部调用

 

非静态函数调用

// MainActivity.java

package com.Sealy.FingerTest;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;

import com.unity3d.player.UnityPlayer;
import com.Sealy.FingerTest.UnityPlayerNativeActivity;

public class MainActivity extends UnityPlayerNativeActivity
{

    public static MainActivity currentActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        jarTestFunc("call onCreate");
        currentActivity=this;
    }
    
    public static MainActivity getInstance()
    {
        return currentActivity;
    }
    
    public int myAdd(int a,int b)
    {
        return a+b;
    }
}

因为用了单例,单例的实例化在onCreate中,所以必须让类调用onCreate,所以需要在AndroidManifest.xml中配置相应的activity,

例子:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Sealy.FingerTest" android:theme="@android:style/Theme.NoTitleBar" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
    <activity android:name="com.Sealy.FingerTest.MainActivity" 
	android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask"
	android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
      <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    </activity>

  </application>
  <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

然后把AndroidManifest.xml放到Unity的Assets\Plugins\Android目录下

 

Unity调用如下:

private AndroidJavaClass jc;

jc = new AndroidJavaClass("com.Sealy.FingerTest.MainActivity");
AndroidJavaObject ao = jc.CallStatic<AndroidJavaObject>("getInstance");
int sum = ao .Call<int>("myAdd",1,3);

如果不是立即返回的,则可以用UnityPlayer.UnitySendMessage将结果返回给Unity

UnityPlayer.UnitySendMessage("GameObjectName", "CallBackFuncName", "param");

 


 


 

©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页