主要参考了这篇文章:
Unity与Android交互方案优化版
链接:https://www.jianshu.com/p/86b275da600e
自己的实现(unity获取内存和温度):
android端:
1 package com.jing.unity; 2 import android.app.Activity; 3 import android.app.ActivityManager; 4 import android.content.BroadcastReceiver; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.content.IntentFilter; 8 import android.hardware.Sensor; 9 import android.hardware.SensorEvent; 10 import android.hardware.SensorEventListener; 11 import android.hardware.SensorManager; 12 import android.os.Debug; 13 // 当前电池温度 14 import static android.os.BatteryManager.EXTRA_TEMPERATURE; 15 16 /** 17 * Created by Jing on 2018-1-18. 18 */ 19 public class Unity2Android { 20 21 /** 22 * unity项目启动时的的上下文 23 */ 24 private Activity _unityActivity=null; 25 private SensorManager mSensorManager = null; 26 private Sensor mSensor = null; 27 ActivityManager mActivityManager=null; 28 private float temperature = 0; 29 private float memory=0; 30 31 /** 32 * 获取unity项目的上下文 33 * 34 * @return 35 */ 36 37 public void Init() { 38 if (_unityActivity == null) { 39 try { 40 Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer"); 41 Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype); 42 _unityActivity = activity; 43 } catch (ClassNotFoundException e) { 44 45 } catch (IllegalAccessException e) { 46 47 } catch (NoSuchFieldException e) { 48 49 } 50 } 51 mActivityManager = (ActivityManager) _unityActivity.getSystemService(Context.ACTIVITY_SERVICE); 52 /*获取系统服务(SENSOR_SERVICE)返回一个SensorManager对象*/ 53 mSensorManager = (SensorManager) _unityActivity.getSystemService(Context.SENSOR_SERVICE); 54 /*通过SensorManager获取相应的(温度传感器)Sensor类型对象*/ 55 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); 56 if (mSensor != null) { 57 mSensorManager.registerListener(mSensorEventListener, mSensor 58 , SensorManager.SENSOR_DELAY_NORMAL); 59 } 60 else 61 { 62 startBroadCast(); 63 } 64 } 65 66 public void Destroy() { 67 if (mSensor != null) { 68 mSensorManager.unregisterListener(mSensorEventListener, mSensor); 69 } 70 else 71 { 72 stopBroadCast(); 73 } 74 } 75 76 private void startBroadCast() { 77 IntentFilter filter = new IntentFilter(); 78 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 79 _unityActivity.registerReceiver(mBroadcastReceiver, filter); 80 } 81 82 private void stopBroadCast() { 83 _unityActivity.unregisterReceiver(mBroadcastReceiver); 84 } 85 86 /*声明一个SensorEventListener对象用于侦听Sensor事件,并重载onSensorChanged方法*/ 87 private final SensorEventListener mSensorEventListener = new SensorEventListener() { 88 89 @Override 90 public void onSensorChanged(SensorEvent event) { 91 if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) { 92 /*温度传感器返回当前的温度,单位是摄氏度(°C)。*/ 93 temperature = event.values[0]; 94 } 95 } 96 97 @Override 98 public void onAccuracyChanged(Sensor sensor, int accuracy) { 99 // TODO Auto-generated method stub 100 101 } 102 }; 103 104 public float GetTemperature() { 105 return temperature; 106 } 107 108 public float GetMemory() { 109 try { 110 int pid = android.os.Process.myPid(); 111 Debug.MemoryInfo[] memoryInfoArray = mActivityManager.getProcessMemoryInfo(new int[]{pid}); 112 memory = (float) memoryInfoArray[0].getTotalPss()/ 1024.0f; 113 } catch (Exception e) { 114 } 115 return memory; 116 } 117 118 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 119 120 @Override 121 public void onReceive(Context context, Intent intent) { 122 if (null == intent) { 123 return; 124 } 125 String action = intent.getAction(); 126 127 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 128 temperature = intent.getIntExtra(EXTRA_TEMPERATURE, -1)/10.0f; 129 } 130 } 131 }; 132 }
unity端:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class ShowGUI : MonoBehaviour { 6 private float memory=0; 7 private float temperature=0; 8 private AndroidJavaObject jo; 9 // Use this for initialization 10 void Start () { 11 jo = new AndroidJavaObject("com.jing.unity.Unity2Android"); 12 jo.Call("Init");//初始化监听 13 } 14 15 float GetMemory() 16 { 17 float m=jo.Call<float>("GetMemory"); 18 return m; 19 } 20 21 float GetTemperature() 22 { 23 float m=jo.Call<float>("GetTemperature"); 24 return m; 25 } 26 27 void OnDestroy() 28 { 29 jo.Call("Destroy");//结束监听 30 } 31 32 void OnGUI() 33 { 34 GUIStyle labStyle=new GUIStyle(); 35 labStyle.fontSize=100; 36 labStyle.normal.textColor=Color.red; 37 38 if(GUILayout.Button("获取当前值",GUILayout.MaxWidth(200),GUILayout.MaxHeight(100))) 39 { 40 memory=GetMemory(); 41 temperature=GetTemperature(); 42 } 43 44 GUILayout.Label("memory="+memory.ToString(),labStyle); 45 GUILayout.Label("temperature="+temperature.ToString(),labStyle); 46 47 } 48 }
值得注意的一点,导出的arr包得改一下sdk的 min version和target version,与unity统一,不然会报错。
如果想导出jar包,打开该类库下的build.gradle,在dependencies后添加以下代码
task makeJar(type:Copy) { delete 'build/outputs/test.jar' from('build/intermediates/intermediate-jars/release/')//jar文件来源,这个吧我坑的,早期版本是'build/intermediates/bundles/release/' into('build/libs/') //生成路径 include('classes.jar') rename('classes.jar','test.jar') //命名为test.jar } makeJar.dependsOn(build)
然后View-ToolWindow-Gradle,点左上同步,然后拉到other->makeJar即可