卜若的代码笔记系列-unity系列-第四章:android交互之unity调用android打的jar包api--5004

1.从上一章的学习中,我们学会了打包,接下来我们要讲怎么在unity里面使用你打的这个包的函数。

首先,我们先介绍一下几个知识点。

活动,是android开发的四大核心组件之一,也叫做Activty

而,unity的发布的安卓端的游戏本质就是一个android开发工程,我们尝试发布一个android工程看一下

主要是看一下它的AndroidManifest.xml文件

导出来后,我们观察一下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.xxx.xxxxx" 
xmlns:tools="http://schemas.android.com/tools" 
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:theme="@style/UnityThemeSelector" 
  android:icon="@drawable/app_icon" 
  android:label="@string/app_name" 
  android:isGame="true" 
  android:banner="@drawable/app_banner">
    <activity 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|layoutDirection" android:name="com.xxx.xxxxx.UnityPlayerActivity">
      <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" />
    </activity>
  </application>
  <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26" />
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-feature android:name="android.hardware.vulkan" android:required="false" />
  <uses-permission android:name="android.permission.INTERNET" />
  <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文件的内容,我们发现上面有一个pakage = "com.xxx.xxxx"

这个其实就是你发布的包名

我们又继续观测下面一个标签,这个描述你创建的活动默认继承自这个东西

 

也就是说,如果你没有重写这个 AndroidManifest.xml文件的话,那么,你的活动应该继承自这个

至于 

UnityPlayerActivity

这个父类,也是来自于unity写好的一个jar包,我们可以从

Unity2018\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes

获取到这个包,然后放到

下面,选中这个classes.jar,右键,and as library

就成功的添加到你的项目中去了。

 

然后,让我们的活动继承自这个玩意,就像

一样,随便写了一个api,然后我们来讲怎么将空项目打包成jar包,和上一章有点点区别

 

注释掉,并且将打包成app改成打包成类库,然后打包方式和上一章一模一样。

出来之后,我们得到了包

直接丢到unity的Plugins/Android文件夹下面

 

然后测试代码:

    public Text txt;
    void Start()
    {
        txt = this.GetComponent<Text>();
        txt.text = "end:";
        var javaClass = new AndroidJavaObject("com.unityjar.unityjar2.MainActivity");
        int value1=javaClass.Call<int>("add",2,2);

          txt.text += value1 + "";

    }

 至于这个东西

com.unityjar.unityjar2.MainActivity

 

懂了吗??

最后,这个东西只能在真机上测试

:这是一张真机的截屏

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值