ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Unity_如何判断应用设备内存小于1G(内容可定制为根据机器配置进行不同LOD)

2019-11-13 17:57:49  阅读:279  来源: 互联网

标签:LOD int minf UNITY 1G 机器配置 line public mVal


直接上脚本,需要用的时候在需要的地方调用就好。

如:

//获取设别的最大内存,作为判断LOD等级和决定1G以下设备不能进游戏
#if UNITY_ANDROID && !UNITY_EDITOR
        meminfo.gc_Collect();
#endif

meminfo脚本

using UnityEngine;
using System;
using System.Collections;

#if UNITY_ANDROID
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
#endif

#if UNITY_IPHONE || UNITY_IOS
    using System.Runtime.InteropServices;
#endif


public class meminfo  {
#if !UNITY_EDITOR && !UNITY_WEBPLAYER

    #if UNITY_ANDROID
        public struct meminf{
            //all numbers are in kiloBytes
            public int memtotal;
            public int memfree;
            public int active;
            public int inactive;
            public int cached;
            public int swapcached;
            public int swaptotal;
            public int swapfree;
        }
        
        public static meminf minf = new meminf();
        
        private static Regex re = new Regex(@"\d+");
        
        public static bool getMemInfo(){
            
            if(!File.Exists("/proc/meminfo")) return false;
        
            FileStream fs = new FileStream("/proc/meminfo", FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader sr = new StreamReader(fs);
            
            string line;
            while((line = sr.ReadLine())!=null){
                line = line.ToLower().Replace(" ","");
                if(line.Contains("memtotal")){ minf.memtotal = mVal(line); }
                if(line.Contains("memfree")){ minf.memfree = mVal(line); }
                if(line.Contains("active")){ minf.active = mVal(line); }
                if(line.Contains("inactive")){ minf.inactive = mVal(line); }
                if(line.Contains("cached") && !line.Contains("swapcached")){ minf.cached = mVal(line); }
                if(line.Contains("swapcached")){ minf.swapcached = mVal(line); }
                if(line.Contains("swaptotal")){ minf.swaptotal = mVal(line); }
                if(line.Contains("swapfree")){ minf.swapfree = mVal(line); }
            }
            
            sr.Close(); fs.Close(); fs.Dispose();
            return true;
        }
        
        private static int mVal(string s){
            Match m = re.Match(s); return int.Parse(m.Value);
        }
    
        public static void gc_Collect() {
            var jc = new AndroidJavaClass("java.lang.System");
            jc.CallStatic("gc");
            jc.Dispose();
        }

    #endif
    
    #if UNITY_IPHONE || UNITY_IOS
    
        public struct meminf{
            //all numbers are in bytes
            public int memtotal;
            public int memfree;
            public int memused;
        }
    
        public static meminf minf = new meminf();
        
        [DllImport("__Internal")]
        private static extern int igetRam(bool what);
    
    
    
        public static bool getMemInfo(){
        
            int rt;

            rt = minf.memfree = igetRam(true);//free
            rt = minf.memused = igetRam(false);//used
            if(rt==-1) return false;
            
            minf.memtotal = minf.memfree + minf.memused;
            
            return true;
            
        }
    
    #endif

#endif
}

 

标签:LOD,int,minf,UNITY,1G,机器配置,line,public,mVal
来源: https://www.cnblogs.com/zangjiapei/p/11851376.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有