使用SYSTEMINFO類獲取UNITY3D運行設備的各類信息(CPU類型,顯卡類型等)

1.概述

2.創建演示項目

3.編寫示例代碼

4.執行代碼

6.其他

 

1.概述

 

研究完Unity3D的Application類後,就該看了一下SystemInfo類。這兩個類都在UnityEngine類中。

SystemInfo類中的屬性都是隻讀屬性,存儲着運行平臺的一些信息,主要是顯卡和設備信息,如:設備的名稱、設備的類型、顯卡的類型,顯卡的名稱、顯卡供應商(製造商)、系統內存大小、顯存大小、支持的渲染目標數量等等。

2.創建演示項目

(1)打開Unity3D,新建一個項目;

(2)新建一個C#腳本,並將腳本綁定到主相機(MianCamera)上;

(3)單擊菜單欄上的GameObject,選擇UI,添加一個Text。

3.編寫示例代碼

(1)雙擊打開剛纔新建的腳本,開始編輯代碼(其實SystemInfo類很簡單)。

編寫的代碼如下所示:

複製代碼
  1 using UnityEngine;
  2 
  3 using System.Collections;
  4 
  5 using System.Collections.Generic;
  6 
  7 public class GameControllerScript: MonoBehaviour
  8 
  9 {
 10 
 11     //指定輸出文本框
 12 
 13     public UnityEngine.UI.Text messageText;
 14 
 15     //存儲臨時字符串
 16 
 17     System.Text.StringBuilder info = new System.Text.StringBuilder();
 18 
 19     // Use this for initialization
 20 
 21     void Start()
 22 
 23     {
 24 
 25        
 26 
 27         //將輸出文本框置空
 28 
 29         messageText.text = "";
 30 
 31         info.AppendLine("設備與系統信息:");
 32 
 33  
 34 
 35         //設備的模型
 36 
 37         GetMessage("設備模型",SystemInfo.deviceModel);
 38 
 39         //設備的名稱
 40 
 41         GetMessage("設備名稱",SystemInfo.deviceName);
 42 
 43         //設備的類型
 44 
 45         GetMessage("設備類型(PC電腦,掌上型)",SystemInfo.deviceType.ToString());
 46 
 47         //系統內存大小
 48 
 49         GetMessage("系統內存大小MB",SystemInfo.systemMemorySize.ToString());
 50 
 51         //操作系統
 52 
 53         GetMessage("操作系統",SystemInfo.operatingSystem);
 54 
 55         //設備的唯一標識符
 56 
 57         GetMessage("設備唯一標識符",SystemInfo.deviceUniqueIdentifier);
 58 
 59         //顯卡設備標識ID
 60 
 61         GetMessage("顯卡ID",SystemInfo.graphicsDeviceID.ToString());
 62 
 63         //顯卡名稱
 64 
 65         GetMessage("顯卡名稱", SystemInfo.graphicsDeviceName);
 66 
 67         //顯卡類型
 68 
 69         GetMessage("顯卡類型",SystemInfo.graphicsDeviceType.ToString());
 70 
 71         //顯卡供應商
 72 
 73         GetMessage("顯卡供應商", SystemInfo.graphicsDeviceVendor);
 74 
 75         //顯卡供應唯一ID
 76 
 77         GetMessage("顯卡供應唯一ID", SystemInfo.graphicsDeviceVendorID.ToString());
 78 
 79         //顯卡版本號
 80 
 81         GetMessage("顯卡版本號",SystemInfo.graphicsDeviceVersion);
 82 
 83         //顯卡內存大小
 84 
 85         GetMessage("顯存大小MB",SystemInfo.graphicsMemorySize.ToString());
 86 
 87         //顯卡是否支持多線程渲染
 88 
 89         GetMessage("顯卡是否支持多線程渲染",SystemInfo.graphicsMultiThreaded.ToString());
 90 
 91         //支持的渲染目標數量
 92 
 93         GetMessage("支持的渲染目標數量", SystemInfo.supportedRenderTargetCount.ToString());
 94 
 95  
 96 
 97  
 98 
 99  
100 
101  
102 
103  
104 
105         //輸出
106 
107         messageText.text = info.ToString();
108 
109     }
110 
111  
112 
113     // Update is called once per frame
114 
115     void Update()
116 
117     {
118 
119         //退出
120 
121         if (Input.GetKeyUp("escape"))
122 
123         {
124 
125  
126 
127             if (Input.GetKeyUp("escape"))
128 
129             {
130 
131                 Application.Quit();
132 
133             }
134 
135         }
136 
137     }
138 
139     void GetMessage(params string[] str)
140 
141     {
142 
143         if(str.Length==2)
144 
145         {
146 
147             info.AppendLine(str[0]+":"+str[1]);
148 
149         }
150 
151     }  
152 
153 }
複製代碼

 

(2)保存代碼,轉到Unity3D編輯器中,將Text綁定到腳本。

4.執行代碼

執行代碼後可以顯示運行設備的一些信息。

(1)在Unity3D編輯器中運行的結果:

 

(1)在Windows中的運行的結果:

 

(1)在Android中的運行的結果:

 

 

6.其他

有興有趣的話,可以查看官方的文檔瞭解SystemInfo的其他屬性和方法,鏈接是:https://docs.unity3d.com/ScriptReference/SystemInfo.html

 

 

如有錯誤,還望指正!

發佈了158 篇原創文章 · 獲贊 57 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章