使用SystemInfo类获取Unity3D运行设备的各类信息(CPU类型,显卡类型等)

发表于2018-01-09
评论0 3.2k浏览
1.概述
研究完Unity3D的Application类后,就该看了一下SystemInfo类。这两个类都在UnityEngine类中。SystemInfo类中的属性都是只读属性,存储着运行平台的一些信息,主要是显卡和设备信息,如:设备的名称、设备的类型、显卡的类型,显卡的名称、显卡供应商(制造商)、系统内存大小、显存大小、支持的渲染目标数量等等。这篇文章主要介绍的也是如何如何使用SystemInfo类获取Unity3D运行设备的各类信息。

2.创建演示项目
(1)打开Unity3D,新建一个项目;
(2)新建一个C#脚本,并将脚本绑定到主相机(MianCamera)上;
(3)单击菜单栏上的GameObject,选择UI,添加一个Text。

3.编写示例代码
(1)双击打开刚才新建的脚本,开始编辑代码(其实SystemInfo类很简单)。

编写的代码如下所示:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class GameControllerScript: MonoBehaviour
 {
     //指定输出文本框
     public UnityEngine.UI.Text messageText;
     //存储临时字符串
     System.Text.StringBuilder info = new System.Text.StringBuilder();
     // Use this for initialization
     void Start()
     {
         //将输出文本框置空
         messageText.text = "";
         info.AppendLine("设备与系统信息:");
         //设备的模型
         GetMessage("设备模型",SystemInfo.deviceModel);
         //设备的名称
         GetMessage("设备名称",SystemInfo.deviceName);
         //设备的类型
         GetMessage("设备类型(PC电脑,掌上型)",SystemInfo.deviceType.ToString());
         //系统内存大小
         GetMessage("系统内存大小MB",SystemInfo.systemMemorySize.ToString());
         //操作系统
         GetMessage("操作系统",SystemInfo.operatingSystem);
         //设备的唯一标识符
         GetMessage("设备唯一标识符",SystemInfo.deviceUniqueIdentifier);
         //显卡设备标识ID
         GetMessage("显卡ID",SystemInfo.graphicsDeviceID.ToString());
         //显卡名称
         GetMessage("显卡名称", SystemInfo.graphicsDeviceName);
         //显卡类型
         GetMessage("显卡类型",SystemInfo.graphicsDeviceType.ToString());
         //显卡供应商
         GetMessage("显卡供应商", SystemInfo.graphicsDeviceVendor);
         //显卡供应唯一ID
         GetMessage("显卡供应唯一ID", SystemInfo.graphicsDeviceVendorID.ToString());
         //显卡版本号
         GetMessage("显卡版本号",SystemInfo.graphicsDeviceVersion);
         //显卡内存大小
         GetMessage("显存大小MB",SystemInfo.graphicsMemorySize.ToString());
         //显卡是否支持多线程渲染
         GetMessage("显卡是否支持多线程渲染",SystemInfo.graphicsMultiThreaded.ToString());
         //支持的渲染目标数量
         GetMessage("支持的渲染目标数量", SystemInfo.supportedRenderTargetCount.ToString());
         //输出
         messageText.text = info.ToString();
     }
     // Update is called once per frame
     void Update()
     {
         //退出
         if (Input.GetKeyUp("escape"))
         {
             if (Input.GetKeyUp("escape"))
             {
                 Application.Quit();
             }
         }
     }
     void GetMessage(params string[] str)
     {
         if(str.Length==2)
         {
             info.AppendLine(str[0]+":"+str[1]);
         }
     }  
 }

(2)保存代码,转到Unity3D编辑器中,将Text绑定到脚本。

4.执行代码
执行代码后可以显示运行设备的一些信息。

(1)在Unity3D编辑器中运行的结果:

(1)在Windows中的运行的结果:

(1)在Android中的运行的结果:

5.其他
有兴有趣的话,可以查看官方的文档了解SystemInfo的其他属性和方法,链接是:https://docs.unity3d.com/ScriptReference/SystemInfo.html

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论