参考链接:Android Studio打包arr包并导入Unity中
参考链接:Unity中调用Android方法
根据包名获取本地Icon
Android java代码
package com.android.util;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
public class UseForUnity {
/// 设置一个 Activity 参数
private static Activity _unityActivity;
// 通过反射获取 Unity 的 Activity 的上下文
public static Activity getActivity() {
if (null == _unityActivity) {
try {
Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
_unityActivity = activity;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
return _unityActivity;
}
// 通过包名获取应用的 Drawable icon 并转为 byte[] 数据
public byte[] getDrawableIconByPackageName(String packageName) {
Drawable icon = getAppIcon(packageName);
Bitmap bitmap = FileTool.DrawableToBitmap(icon);
return FileTool.BitmapToByte(bitmap);
}
// 通过包名获取对应的 Drawable 数据
private Drawable getAppIcon(String packageName) {
try {
PackageManager pm = getActivity().getPackageManager();
ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
return info.loadIcon(pm);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Unity中调用获取:
/// <summary>
/// 加载本地app图标
/// </summary>
/// <param name="packageName"></param>
/// <returns></returns>
public static Texture2D LoadAPPIcon(string packageName)
{
Texture2D texture = new Texture2D(100, 100);
#if UNITY_ANDROID
try
{
// 调用aar包
AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.android.util.UseForUnity");
// 通过安卓类的接口类获取Drawable的byte[]
// 第一个参数:对应函数接口的名称
// 第二个参数:对应函数接口的包名参数
byte[] drawableBytes = androidJavaObject.Call<byte[]>("getDrawableIconByPackageName", packageName);
texture.LoadImage(drawableBytes);
}
catch (System.Exception e)
{
Debug.LogError("加载app图标报错!!!" + packageName);
Debug.LogError(e);
}
#endif
return texture;
}
但是有时需要,显示本地所有应用的Icon,如果通过遍历本地应用,每个本地应用挨个使用上述单个获取应用Icon方法,则会有1s左右的卡顿。解决方法:Android使用线程去加载所有本地应用Icon,加载完毕,调用回调,通知Unity。
Android java代码:
回调:
package com.android.util;
//获取icon回调
public interface GetIconCallback {
public void onSuccess(int byteLenth);
public void onError(String errorMessage);
}
开启线程,加载图标:
package com.android.util;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
public class UseForUnity {
private byte[][] imageList;
/// 设置一个 Activity 参数
private static Activity _unityActivity;
// 通过反射获取 Unity 的 Activity 的上下文
public static Activity getActivity() {
if (null == _unityActivity) {
try {
Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
_unityActivity = activity;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
return _unityActivity;
}
//根据index获取应用图标
public byte[] getIconByIndex(int index) {
if (imageList == null || imageList.length <= 0) {
// 说明此时未加载完毕
return null;
}
return imageList[index];
}
// 开启线程,获取应用图标
public void getIconByPackageName(final String[] packageNames, final GetIconCallback callback) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
imageList = new byte[packageNames.length][];
for (int i = 0; i < packageNames.length; i++) {
imageList[i] = getDrawableIconByPackageName(packageNames[i]);
}
// 调用回调,通知Unity加载完毕
callback.onSuccess(imageList.length);
} catch (Exception e) {
callback.onError("get icon error");
}
}
});
t.start();
}
// 通过包名获取应用的 Drawable icon 并转为 byte[] 数据
private byte[] getDrawableIconByPackageName(String packageName) {
Drawable icon = getAppIcon(packageName);
Bitmap bitmap = FileTool.DrawableToBitmap(icon);
return FileTool.BitmapToByte(bitmap);
}
// 通过包名获取对应的 Drawable 数据
private Drawable getAppIcon(String packageName) {
try {
PackageManager pm = getActivity().getPackageManager();
ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
return info.loadIcon(pm);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Unity收到onSuccess回调后,通过getIconByIndex,获得Icon:
/// <summary>
/// 安卓回调
/// </summary>
class AndroidPluginCallback : AndroidJavaProxy
{
public AndroidPluginCallback() : base("com.android.util.GetIconCallback") { }
public void onSuccess(int byteLenth)
{
// 图标加载成功 可以获取图标
}
public void onError(string error)
{
Debug.Log("本地app icon读取失败!");
Debug.Log(error);
}
}
public static AndroidJavaObject _androidJavaObject;
/// <summary>
/// 开启加载应用图标线程
/// </summary>
/// <param name="packageName"></param>
public static void StartLoadAppIcon(string[] packageNames)
{
#if UNITY_ANDROID
try
{
_androidJavaObject = new AndroidJavaObject("com.android.util.UseForUnity");
_androidJavaObject.Call("getIconByPackageName", packageNames, new AndroidPluginCallback());
}
catch (System.Exception e)
{
LWDebug.LogError("开启加载应用图标线程报错!!!");
LWDebug.LogError(e);
}
#endif
}
/// <summary>
/// 根据index加载图标
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public static Texture2D LoadIconByIndex(int index)
{
Texture2D texture = new Texture2D(100, 100);
#if UNITY_ANDROID
try
{
// 调用getIconByIndex获取Icon,注意此处用的还需原来的androidJavaObject,不能new一个,否则获取不到
byte[] drawableBytes = _androidJavaObject.Call<byte[]>("getIconByIndex", index);
texture.LoadImage(drawableBytes);
}
catch (System.Exception e)
{
LWDebug.LogError("根据index加载图标!!!");
LWDebug.LogError(e);
}
#endif
return texture;
}
网友评论