1. 程式人生 > >unity 讀取外部exe程序控制臺信息

unity 讀取外部exe程序控制臺信息

ble rect 設備名 redirect hit ali == 自帶 AR

由於需要獲取顯卡信息,但是unity的自帶函數,只能輸出1個顯卡

c#倒是可以但是引用了一個下載的dll System.Management.dll

這個dll放到unity用不了,因為mono不支持

所以先用vs寫個外部exe程序

using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        string Gname = "";

        ManagementObjectSearcher objvide 
= new ManagementObjectSearcher("select * from Win32_VideoController"); foreach (ManagementObject obj in objvide.Get()) { Gname += ("Name - " + obj["Name"] + "</br>"); //System.Console.WriteLine("Name - " + obj["Name"] + "</br>"); //System.Console.WriteLine("DeviceID - " + obj["DeviceID"] + "</br>");
//System.Console.WriteLine("AdapterRAM - " + obj["AdapterRAM"] + "</br>"); //System.Console.WriteLine("AdapterDACType - " + obj["AdapterDACType"] + "</br>"); //System.Console.WriteLine("Monochrome - " + obj["Monochrome"] + "</br>"); //System.Console.WriteLine("InstalledDisplayDrivers - " + obj["InstalledDisplayDrivers"] + "</br>");
//System.Console.WriteLine("DriverVersion - " + obj["DriverVersion"] + "</br>"); //System.Console.WriteLine("VideoProcessor - " + obj["VideoProcessor"] + "</br>"); //System.Console.WriteLine("VideoArchitecture - " + obj["VideoArchitecture"] + "</br>"); //System.Console.WriteLine("VideoMemoryType - " + obj["VideoMemoryType"] + "</br>"); //Console.WriteLine("====================================================="); } Console.WriteLine(Gname); Console.WriteLine("====================================================="); //Console.ReadKey(); } }

然後生成運行下

Unity代碼

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

public class GetSystemInfo : MonoBehaviour {

    string a = "";

    // Use this for initialization
    void Start () {


        //這種方法可以
        GetStr();

        //這種方法也可以
        //OpenEXE("C://Users/zts/source/repos/ConsoleApp9/ConsoleApp9/bin/Debug/ConsoleApp9.exe", "");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="_exePathName">路徑</param>
    /// <param name="_exeArgus">啟動參數</param>
    public void OpenEXE(string _exePathName, string _exeArgus)
    {
        try
        {
            Process myprocess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo(_exePathName, _exeArgus);
            myprocess.StartInfo = startInfo;
            myprocess.StartInfo.CreateNoWindow = true;
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.StartInfo.RedirectStandardOutput = true;
            //myprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myprocess.Start();
            a += myprocess.StandardOutput.ReadLine();//只能讀取1行
            UnityEngine.Debug.Log(a);
            myprocess.WaitForExit();
        }
        catch (Exception ex)
        {
            UnityEngine.Debug.Log("出錯原因:" + ex.Message);
        }
    }

    public void GetStr()
    {
        try
        {
            Process proc = new Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "C://Users/zts/source/repos/ConsoleApp9/ConsoleApp9/bin/Debug/ConsoleApp9.exe";
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            //proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//這句會讓unity卡死
            proc.Start();
            string fingerprint = proc.StandardOutput.ReadLine();
            UnityEngine.Debug.Log(fingerprint);
            proc.WaitForExit();
        }
        catch (Exception)
        {

            throw;
        }

    }

    /********************************unity獲取設備信息*******************************/
    string systemInfo;
    public void GetUnityInfo()
    {
        systemInfo = "\tTitle:當前系統基礎信息:\n設備模型:" + SystemInfo.deviceModel + "\n設備名稱:" + SystemInfo.deviceName + "\n設備類型:" + SystemInfo.deviceType +
            "\n設備唯一標識符:" + SystemInfo.deviceUniqueIdentifier + "\n顯卡標識符:" + SystemInfo.graphicsDeviceID +
            "\n顯卡設備名稱:" + SystemInfo.graphicsDeviceName + "\n顯卡廠商:" + SystemInfo.graphicsDeviceVendor +
            "\n顯卡廠商ID:" + SystemInfo.graphicsDeviceVendorID + "\n顯卡支持版本:" + SystemInfo.graphicsDeviceVersion +
            "\n顯存(M):" + SystemInfo.graphicsMemorySize + "\n顯卡像素填充率(百萬像素/秒),-1未知填充率:" + SystemInfo.graphicsPixelFillrate +
            "\n顯卡支持Shader層級:" + SystemInfo.graphicsShaderLevel + "\n支持最大圖片尺寸:" + SystemInfo.maxTextureSize +
            "\nnpotSupport:" + SystemInfo.npotSupport + "\n操作系統:" + SystemInfo.operatingSystem +
            "\nCPU處理核數:" + SystemInfo.processorCount + "\nCPU類型:" + SystemInfo.processorType +
            "\nsupportedRenderTargetCount:" + SystemInfo.supportedRenderTargetCount + "\nsupports3DTextures:" + SystemInfo.supports3DTextures +
            "\nsupportsAccelerometer:" + SystemInfo.supportsAccelerometer + "\nsupportsComputeShaders:" + SystemInfo.supportsComputeShaders +
            "\nsupportsGyroscope:" + SystemInfo.supportsGyroscope + "\nsupportsImageEffects:" + SystemInfo.supportsImageEffects +
            "\nsupportsInstancing:" + SystemInfo.supportsInstancing + "\nsupportsLocationService:" + SystemInfo.supportsLocationService +
            "\nsupportsRenderTextures:" + SystemInfo.supportsRenderTextures + "\nsupportsRenderToCubemap:" + SystemInfo.supportsRenderToCubemap +
            "\nsupportsShadows:" + SystemInfo.supportsShadows + "\nsupportsSparseTextures:" + SystemInfo.supportsSparseTextures +
            "\nsupportsStencil:" + SystemInfo.supportsStencil + "\nsupportsVertexPrograms:" + SystemInfo.supportsVertexPrograms +
            "\nsupportsVibration:" + SystemInfo.supportsVibration + "\n內存大小:" + SystemInfo.systemMemorySize;
    }
    void OnGUI()
    {
        GUILayout.Label(systemInfo);
    }
    /************************************************************************/

    // Update is called once per frame
    void Update () {
        
    }
}

缺陷:這種讀取只能讀取控制臺的1行數據,當然你可以把數據集中起來,一行輸出

不知道有沒有其他辦法可以獲取多行控制臺信息

unity 讀取外部exe程序控制臺信息