You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中SystemInfo.OperatingSystem()无法识别Windows 11,仍显示Windows 10求助

Unity SystemInfo.OperatingSystem 在Windows 11下显示Windows 10的解决方案

问题原因

Unity旧版本的SystemInfo.OperatingSystem API是通过Windows内核版本号判断系统版本的。Windows 11的内核版本采用10.0.x格式(初始版本为10.0.22000),旧版Unity会将所有10.0.x的内核版本判定为Windows 10,导致识别错误。

解决方案

方案1:升级Unity版本

Unity在2021.2.7f1及后续版本中已修复Windows 11识别问题,升级到对应版本后,SystemInfo.OperatingSystem会正确显示Windows 11。

方案2:手动调用Windows原生API(兼容旧Unity版本)

如果无法升级Unity,可通过调用Windows原生API获取真实系统版本,示例代码如下:

using System.Runtime.InteropServices;
using UnityEngine;

public class OSVersionChecker : MonoBehaviour
{
    [StructLayout(LayoutKind.Sequential)]
    private struct OSVERSIONINFOEX
    {
        public uint dwOSVersionInfoSize;
        public uint dwMajorVersion;
        public uint dwMinorVersion;
        public uint dwBuildNumber;
        public uint dwPlatformId;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string szCSDVersion;
        public ushort wServicePackMajor;
        public ushort wServicePackMinor;
        public ushort wSuiteMask;
        public byte wProductType;
        public byte wReserved;
    }

    [DllImport("kernel32.dll")]
    private static extern bool GetVersionEx(ref OSVERSIONINFOEX osvi);

    [DllImport("kernel32.dll")]
    private static extern bool SetProcessMitigationPolicy(uint dwPolicy, ref uint lpBuffer, uint dwLength);

    void Start()
    {
        // 禁用版本欺骗策略,确保获取真实版本
        uint policy = 0x00000020;
        uint value = 0;
        SetProcessMitigationPolicy(policy, ref value, sizeof(uint));

        OSVERSIONINFOEX osvi = new OSVERSIONINFOEX();
        osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(typeof(OSVERSIONINFOEX));
        
        if (GetVersionEx(ref osvi))
        {
            string osName;
            if (osvi.dwMajorVersion == 10 && osvi.dwMinorVersion == 0 && osvi.dwBuildNumber >= 22000)
            {
                osName = "Windows 11";
            }
            else if (osvi.dwMajorVersion == 10 && osvi.dwMinorVersion == 0)
            {
                osName = "Windows 10";
            }
            else
            {
                osName = $"Windows {osvi.dwMajorVersion}.{osvi.dwMinorVersion}";
            }
            Debug.Log($"当前操作系统:{osName} (Build {osvi.dwBuildNumber})");
        }
        else
        {
            Debug.LogError("无法获取操作系统版本信息");
        }
    }
}

代码逻辑:先禁用进程的版本欺骗策略,再通过GetVersionEx获取真实内核版本号,最后根据Build Number判断是否为Windows 11(Build ≥22000即为Win11)。

方案3:读取Windows注册表信息

通过读取注册表中的系统名称信息,示例代码:

using Microsoft.Win32;
using UnityEngine;

public class RegistryOSChecker : MonoBehaviour
{
    void Start()
    {
        try
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
            {
                if (key != null)
                {
                    string productName = key.GetValue("ProductName") as string;
                    Debug.Log($"当前操作系统:{productName}");
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError($"读取注册表失败:{e.Message}");
        }
    }
}

注意:此方法仅适用于Windows平台,且程序需要足够权限读取注册表。

补充说明

  • 若使用Unity 2020及更早版本,优先推荐升级Unity版本,避免手动维护兼容性逻辑。
  • Windows 11后续更新版本(如22H2、23H2)的内核版本仍为10.0.x,只需通过Build Number≥22000即可准确判断。

内容的提问来源于stack exchange,提问作者Hubert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 06:23:17