Unity接入安卓sdk查看应用内存占用
注:若不清楚如何在unity中接入android sdk可先了解下相关流程。项目地址:http://download.csdn.net/download/yhuangher/9976564
在项目后期进行内存优化,在android端进行内存优化时做了若干辅助工具,比如此款,查看系统总内存,当前App占用内存,系统剩余内存及fps,如图示:
android端代码如下:
package com.JohnRey.ShowAppBaseInfo; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.List; import android.annotation.SuppressLint; import android.app.ActivityManager; import android.content.Context; import android.os.Bundle; import android.os.Debug; import android.text.format.Formatter; import com.unity3d.player.UnityPlayerActivity; public class MainActivity extends UnityPlayerActivity { private ActivityManager mActivityManager = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); } public String getMemoryInfo() { // 系统总内存 String message = getTotalMemory(); // 系统可用内存 message += "-" + getAvailMemory(); // 当前App占用的内存 message += "-" + getCurrentProcessMemory(); return message; } /** * 获取当前app占用内存 * * @return */ @SuppressLint("NewApi") private String getCurrentProcessMemory() { String pkgName = this.getPackageName(); List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager .getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appInfo : appList) { if (appInfo.processName.equals(pkgName)) { int[] pidArray = new int[] { appInfo.pid }; Debug.MemoryInfo[] memoryInfo = mActivityManager .getProcessMemoryInfo(pidArray); float temp = (float) memoryInfo[0].getTotalPrivateDirty() / 1024.0f; return String.format("%.2f", temp)+"MB"; } } return "获取失败"; } /** * 获取手机总可用内存大小 * * @return */ private String getTotalMemory() { String str1 = "/proc/meminfo";// 系统内存信息文件 String str2; String[] arrayOfString; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader( localFileReader, 8192); str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小 arrayOfString = str2.split("\\s+"); localBufferedReader.close(); float temp = Integer.valueOf(arrayOfString[1])/1048576.0f; return String.format("%.2f", temp)+"GB"; } catch (IOException e) { return "获取失败"; } } /** * 获取系统可用内存信息 * * @return */ private String getAvailMemory() { ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); mActivityManager.getMemoryInfo(mi); // 转化为mb return Formatter.formatFileSize(this, mi.availMem); } }
C#端代码如下:
using UnityEngine; using System.Collections; public class FPS : MonoBehaviour { public float updateInterval = 0.5F; private float accum = 0; private int frames = 0; private float timeleft; void Start() { timeleft = updateInterval; #if UNITY_ANDROID _getJavaObject(); Invoke("callAndroid", 2); #endif } void Update() { _updateFPS(); } #region FPS显示 private float fps; [Range(0, 150)] public int MaxFPS; private string _curFPS; private void _updateFPS() { timeleft -= Time.deltaTime; //timescale是时间速度,为1时即为正常速度,为2时即为两倍速,为0时即为暂停 accum += Time.timeScale / Time.deltaTime; ++frames; if (timeleft <= 0.0) { fps = accum / frames; _curFPS = "FPS:" + fps; timeleft = updateInterval; accum = 0.0F; frames = 0; } } #endregion #region CallAndroid private string _memoryInfo; private AndroidJavaObject ajo; #if UNITY_ANDROID private void callAndroid() { _memoryInfo = string.Empty; string result = ajo.Call<string>("getMemoryInfo"); string[] strArray = result.Split('-'); //系统总内存 _memoryInfo += "系统内存:" + strArray[0] + "\n"; //系统可用内存 _memoryInfo += "可用内存:" + strArray[1] + "\n"; //App占用内存 _memoryInfo += "app占用内存:" + strArray[2] + "\n"; ////系统分配的最大占用内存 //_memoryInfo += "AllotMemory:" + strArray[3]; //加入代码之后,可能会每隔两秒钟出现卡顿现象 Invoke("callAndroid", 2); } private void _getJavaObject() { AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); ajo = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); } #endif #endregion void OnGUI() { GUIStyle style = new GUIStyle(); style.fontSize = 20; style.normal.textColor = Color.green; GUI.Label(new Rect(Screen.width - 300, 10, 250, 200), _memoryInfo, style); style.normal.textColor = fps > MaxFPS ? Color.red : Color.green; GUI.Label(new Rect(Screen.width - 200, Screen.height - 50, 100, 30), _curFPS, style); } }