IOS :
1 //获取磁盘空间总大小 2 -(float)GetTotalDiskSpace { 3 float totalSpace; 4 NSError *error = nil; 5 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 6 NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error]; 7 8 if (dictionary) { 9 NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize]; 10 NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize]; 11 totalSpace = ([fileSystemSizeInBytes floatValue])/1000.0f/1000.0f; 12 13 NSLog(@"iphone磁盘总大小 =%@", [[NSString stringWithFormat:@"%.2f",totalSpace] stringByAppendingString:@"MB"]); 14 } else { 15 NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]); 16 } 17 return totalSpace; 18 } 19 20 //获取磁盘剩余空间 21 -(float)GetFreeDiskSpace { 22 float totalFreeSpace; 23 NSError *error = nil; 24 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 25 NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error]; 26 27 if (dictionary) { 28 NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize]; 29 NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize]; 30 totalFreeSpace = ([freeFileSystemSizeInBytes floatValue])/1000.0f/1000.0f; 31 32 NSLog(@"iphone磁盘剩余大小 =%@", [[NSString stringWithFormat:@"%.2f",totalFreeSpace] stringByAppendingString:@"MB"]); 33 } else { 34 NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %@", [error domain], [error code]); 35 } 36 return totalFreeSpace; 37 }
Android:
unity - C#
1 using UnityEngine; 2 using System.Runtime.InteropServices; 3 4 5 /// <summary> 6 /// 设备机型类 7 /// </summary> 8 public static class DeviceModelEx 9 { 10 //获取磁盘空间总大小 11 public static int GetTotalMemory() 12 { 13 #if UNITY_ANDROID && !UNITY_EDITOR 14 try 15 { 16 AndroidJavaObject fileReader = new AndroidJavaObject("java.io.FileReader", "/proc/meminfo"); 17 AndroidJavaObject br = new AndroidJavaObject("java.io.BufferedReader", fileReader, 2048); 18 string mline = br.Call<String>("readLine"); 19 br.Call("close"); 20 mline = mline.Substring(mline.IndexOf("MemTotal:")); 21 mline = Regex.Match(mline, "(\\d+)").Groups[1].Value; 22 return (int.Parse(mline) / 1024); 23 } 24 catch (Exception e) 25 { 26 if (!Application.isEditor) Debug.LogError("GetTotalMemory 获取内存失败:" + e); 27 return SystemInfo.systemMemorySize; 28 } 29 #elif UNITY_IPHONE && !UNITY_EDITOR 30 int diskSpace = int.MaxValue; 31 long value = (long)GetTotalDiskSpace() 32 int.TryParse("" + value , out diskSpace); 33 return diskSpace; 34 #endif 35 return SystemInfo.systemMemorySize; 36 } 37 38 39 [DllImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceEx")] 40 public static extern int GetDiskFreeSpaceEx(string lpDirectoryName, out long lpFreeBytesAvailable, out long lpTotalNumberOfBytes, out long lpTotalNumberOfFreeBytes); 41 42 /// <summary> 43 /// 获取磁盘剩余空间 44 /// </summary> 45 /// <returns></returns> 46 public static int GetFreeDiskSpace() 47 { 48 int diskSpace = int.MaxValue; 49 #if UNITY_ANDROID && !UNITY_EDITOR 50 AndroidJavaClass unityPluginLoader = new AndroidJavaClass("com.woogiworld.hlextends.DiskSpace"); 51 long value = unityPluginLoader.CallStatic<long>("GetFreeDiskSpace"); 52 int.TryParse("" + value , out diskSpace); 53 return diskSpace; 54 55 #elif UNITY_IPHONE && !UNITY_EDITOR 56 long value = (long)GetFreeDiskSpace() 57 int.TryParse("" + value , out diskSpace); 58 return diskSpace; 59 #endif 60 return SystemInfo.systemMemorySize; 61 } 62 63 64 65 //[CSharpCallLua] 66 public delegate bool LuaReadAndroidInfCallBack(string target); 67 public static void CallReadAndroidInfoFun(string infoPath, LuaReadAndroidInfCallBack luaCallfun) 68 { 69 #if UNITY_EDITOR 70 try 71 { 72 if (string.IsNullOrEmpty(infoPath)) 73 { 74 if (luaCallfun != null) luaCallfun(null); 75 return; 76 } 77 var fileReader = new AndroidJavaObject("java.io.FileReader", infoPath); 78 if (fileReader == null) 79 { 80 if (luaCallfun != null) luaCallfun(null); 81 return; 82 } 83 var br = new AndroidJavaObject("java.io.BufferedReader", fileReader, 2048); 84 if (br == null) 85 { 86 if (luaCallfun != null) luaCallfun(null); 87 return; 88 } 89 string mline = ""; 90 while ((mline = br.Call<string>("readLine")) != null) 91 { 92 if (luaCallfun == null) break; 93 if (!luaCallfun(mline)) break; 94 } 95 br.Call("close"); 96 } 97 catch (System.Exception e) 98 { 99 Debug.LogError(e.ToString()); 100 } 101 #endif 102 if (luaCallfun != null) luaCallfun(null); 103 } 104 105 106 /// <summary> 107 /// 0 运行在非 AndroidSimulator 108 /// 1 运行在 AndroidSimulator 109 /// -1 表示未进行初始化 110 /// </summary> 111 private static int _isRunAndroidSimulator = -1; 112 113 /// <summary> 114 /// 判断是否运行在安卓模拟器上面 115 /// 判断依据是 通过模拟器无法模拟 Mac地址的属性 通过mac地址是否存在来判断 116 /// 注意的是可以以这样来判断 但是此处mac地址不能使用 117 /// 这个方法Android 7.0是获取不到的,都会返回“02:00:00:00:00:00” 118 /// </summary> 119 /// <returns></returns> 120 public static bool IsRunAndroidSimulator() 121 { 122 if (_isRunAndroidSimulator == -1) 123 { 124 #if UNITY_ANDROID && !UNITY_EDITOR 125 _isRunAndroidSimulator = 0; 126 try 127 { 128 AndroidJavaObject fileReader = new AndroidJavaObject("java.io.FileReader", "/proc/diskstats"); 129 AndroidJavaObject br = new AndroidJavaObject("java.io.BufferedReader", fileReader, 2048); 130 bool isMmcblk0 = false; 131 string mline = ""; 132 while ((mline = br.Call<String>("readLine")) != null) 133 { 134 if (mline.IndexOf("mmcblk0") == -1) continue; 135 isMmcblk0 = true; 136 break; 137 } 138 br.Call("close"); 139 140 if (!isMmcblk0) 141 { 142 //如果芯片是 x86 则认为是模拟器 143 if (SystemInfo.processorType.ToLower().IndexOf("intel x86") != -1) 144 { 145 _isRunAndroidSimulator = 1; 146 return true; 147 } 148 AndroidJavaClass roSecureObj = new AndroidJavaClass("android.os.SystemProperties"); 149 AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 150 AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity"); 151 AndroidJavaObject unityContext = unityActivity.Call<AndroidJavaObject>("getPackageManager"); 152 //闪光灯检测 153 var isflash = unityContext.Call<Boolean>("hasSystemFeature", "android.hardware.camera.flash"); 154 if (!isflash) 155 { 156 _isRunAndroidSimulator = 1; 157 return true; 158 } 159 //ro.hardware 160 var hardware = roSecureObj.CallStatic<String>("get", "ro.hardware"); 161 if (!string.IsNullOrEmpty(hardware)) 162 { 163 hardware = hardware.ToLower(); 164 if (hardware.Contains("ttvm") || hardware.Contains("nox") || hardware.Contains("_x86") || hardware.EndsWith("x86")) 165 { 166 _isRunAndroidSimulator = 1; 167 return true; 168 } 169 } 170 } 171 } 172 catch 173 { 174 if (!SystemInfo.supportsGyroscope || !SystemInfo.supportsVibration) return true; 175 } 176 #else 177 _isRunAndroidSimulator = 0; 178 #endif 179 } 180 return _isRunAndroidSimulator == 1; 181 } 182 183 184 }
Android - java:
1 package com.woogiworld.hlextends; 2 3 import android.os.Environment; 4 import android.os.StatFs; 5 6 import java.io.File; 7 8 public class DiskSpace { 9 10 public static long GetFreeDiskSpace() { 11 try { 12 File file = Environment.getDataDirectory(); 13 StatFs sf = new StatFs(file.getPath()); 14 return sf.getAvailableBytes() / (1024 * 1024); 15 } catch (Throwable e) { 16 return -1; 17 } 18 } 19 }