Android提供aar包給Unity3d呼叫
阿新 • • 發佈:2018-11-12
1.aar包製作
下面通過手機振動為例:PhoneVibrate
package com.gamestar.nativesoundpool; import android.Manifest; import android.app.Activity; import android.app.Service; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.os.VibrationEffect; import android.os.Vibrator; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; /** * Created by jiamilin on 2018/9/10. */ public class PhoneVibrate { /** * 申請振動許可權 * @param activity */ public static boolean requestVibratePermission(Activity activity){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.VIBRATE}, 1001); Log.e("phonevibrate","許可權未申請"); return false; }else { Log.e("phonevibrate","許可權已經申請"); return true; } } return true; } public static void vibrator(Context context){ Vibrator vibrator=(Vibrator)context.getSystemService(Service.VIBRATOR_SERVICE); if (Build.VERSION.SDK_INT>25){ /** * 建立一次性振動 * * @param milliseconds 震動時長(ms) * @param amplitude 振動強度。這必須是1到255之間的值,或者DEFAULT_AMPLITUDE */ VibrationEffect effect = VibrationEffect.createOneShot(20,1); vibrator.vibrate(effect); }else { vibrator.vibrate(20); } } public static void vibrator(Context context, int milliseconds){ Vibrator vibrator=(Vibrator)context.getSystemService(Service.VIBRATOR_SERVICE); if (Build.VERSION.SDK_INT>25){ /** * 建立一次性振動 * * @param milliseconds 震動時長(ms) * @param amplitude 振動強度。這必須是1到255之間的值,或者DEFAULT_AMPLITUDE */ VibrationEffect effect = VibrationEffect.createOneShot(milliseconds,1); vibrator.vibrate(effect); }else { vibrator.vibrate(milliseconds); } } public static void vibrator(Context context, int milliseconds, int amplitude){ Vibrator vibrator=(Vibrator)context.getSystemService(Service.VIBRATOR_SERVICE); if (Build.VERSION.SDK_INT>25){ /** * 建立一次性振動 * * @param milliseconds 震動時長(ms) * @param amplitude 振動強度。這必須是1到255之間的值,或者DEFAULT_AMPLITUDE */ VibrationEffect effect =null; if (amplitude>0 && amplitude <256){ effect = VibrationEffect.createOneShot(milliseconds,amplitude); }else { effect = VibrationEffect.createOneShot(milliseconds,1); } vibrator.vibrate(effect); }else { vibrator.vibrate(milliseconds); } } }
2.將當前專案修改為module,在APP下的build.gradle中將
1)apply plugin: 'com.android.application'修改為:apply plugin: 'com.android.library'
2)defaultConfig下面有個applicationId刪除,案例如下:
將上面兩個紅色箭頭表示的地方修改後繼續執行下面的操作
3.build專案以後會自動生成app-debug.aar的檔案,具體在專案下找到你的app/build/outputs/aar/app-debug.aar
2.aar包匯入
unity呼叫Android的sdk的兩種辦法:1.AAR包,2.jar包+assets資源包+AndroidMainfest,但是目前已經不提倡後者,多說採用aar,aar中包括了jar包+assets資源包+AndroidMainfest等所有的資源,我們只要將aar放到unity中就可以,具體存放位置Plugins/Android/****(自定義的資料夾名稱,只要能代表你aar的功能即可)/**.aar
3.aar包的呼叫