unity3d 获取剩余空间大小 android and ios

C#

    public static long GetFreeDiskSpace()
    {

#if  ( UNITY_ANDROID )
        try
        { 
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaClass unityPluginLoader = new AndroidJavaClass("java类全名");
            return unityPluginLoader.CallStatic<long>("GetFreeDiskSpace");
        }
        catch (Exception e)
        {
            ILog.Info(e.ToString());
        }
#elif UNITY_IPHONE && !UNITY_EDITOR
        return (long)_IOS_GetFreeDiskSpace();
#endif
        return 1024;
    }

java

    public static long GetFreeDiskSpace() {
        try {
            File file = Environment.getDataDirectory();
            StatFs sf = new StatFs(file.getPath());
            return sf.getAvailableBytes() / (1024 * 1024);
        } catch (Throwable e) {
            if (Utile.isDebug())
                Utile.LogError(e.getLocalizedMessage());
        }
        return 1024;
    }

IOS

    long _IOS_GetFreeDiskSpace()
    {
        struct statfs buf;  
        long long freespace = -1;  
        if(statfs("/var", &buf) >= 0){  
            freespace = (long long)(buf.f_bsize * buf.f_bfree);  
        }  
        return freespace/(1024 * 1024);
    }

你可能感兴趣的:(unity3d,android,ios)