QualitySettingsPackage/Packages/com.bywaystudios.qualitytuner/Runtime/HardwareInfo.cs
2026-04-28 19:57:30 +08:00

177 lines
7.1 KiB
C#

using UnityEngine;
using UnityEngine.Rendering;
namespace Byway.Quality
{
/// <summary>
/// Hardware stats to identify performance index of device
/// </summary>
public class HardwareStats
{
/// <summary>
/// Name of device model
/// <seealso cref="UnityEngine.SystemInfo.deviceModel" />
/// </summary>
public string DeviceModel { get; internal set; }
/// <summary>
/// Memory size of GPU in MegaBytes
/// <remarks>Return zero on editor</remarks>
/// </summary>
public int GpuMemorySizeMb { get; internal set; }
/// <summary>
/// Name of GPU
/// <seealso cref="UnityEngine.SystemInfo.graphicsDeviceName" />
/// </summary>
public string GpuName { get; internal set; }
/// <summary>
/// Major series of GPU
/// </summary>
public GpuMajorSeries GpuMajorSeries { get; internal set; }
/// <summary>
/// Major series of GPU
/// </summary>
public GpuMinorSeries GpuMinorSeries { get; internal set; }
/// <summary>
/// Series number of GPU.
/// ex: Adreno 650 -> 650
/// </summary>
public int GpuSeriesNumber { get; internal set; }
/// <summary>
/// Name of SoC
/// <remarks>Only available on Android12 or later</remarks>
/// </summary>
public string SocName { get; internal set; }
/// <summary>
/// Memory size of system in MegaBytes
/// <remarks>Return zero on editor</remarks>
/// </summary>
public int SystemMemorySizeMb { get; internal set; }
// ---------------------------------------------------------------------
// v0.3.0 additions (additive only; existing matchers/assets unaffected).
// ---------------------------------------------------------------------
/// <summary>Android <c>Build$VERSION.SDK_INT</c>; 0 on non-Android / editor.</summary>
public int AndroidSdkInt { get; internal set; }
/// <summary>True when the device is detected as an Android emulator (BlueStacks/MuMu/LDPlayer/AVD/etc.). False on iOS and physical Android.</summary>
public bool IsEmulator { get; internal set; }
/// <summary><see cref="UnityEngine.SystemInfo.graphicsDeviceType" />.</summary>
public GraphicsDeviceType GraphicsDeviceType { get; internal set; }
/// <summary><see cref="UnityEngine.SystemInfo.graphicsShaderLevel" />.</summary>
public int GraphicsShaderLevel { get; internal set; }
/// <summary><see cref="UnityEngine.Screen.width" /> at hardware-stats capture time.</summary>
public int ScreenWidth { get; internal set; }
/// <summary><see cref="UnityEngine.Screen.height" /> at hardware-stats capture time.</summary>
public int ScreenHeight { get; internal set; }
/// <summary><see cref="UnityEngine.Screen.dpi" /> at hardware-stats capture time. May be 0 if unknown.</summary>
public float ScreenDpi { get; internal set; }
/// <summary><see cref="UnityEngine.SystemInfo.processorCount" />.</summary>
public int ProcessorCount { get; internal set; }
/// <summary><see cref="UnityEngine.SystemInfo.processorFrequency" /> in MHz.</summary>
public int ProcessorFrequencyMHz { get; internal set; }
/// <summary><see cref="UnityEngine.SystemInfo.processorType" />. Strong x86 emulator signal on Android.</summary>
public string ProcessorType { get; internal set; }
/// <summary>Android <c>Build.MANUFACTURER</c>; empty on non-Android.</summary>
public string BuildManufacturer { get; internal set; }
/// <summary>Android <c>Build.BRAND</c>; empty on non-Android.</summary>
public string BuildBrand { get; internal set; }
/// <summary>Android <c>Build.MODEL</c>; empty on non-Android. Distinct from <see cref="DeviceModel" /> which is Unity's composed string.</summary>
public string BuildModel { get; internal set; }
/// <summary>Android <c>Build.HARDWARE</c>; empty on non-Android. Useful emulator signal (<c>goldfish</c>/<c>ranchu</c>).</summary>
public string BuildHardware { get; internal set; }
/// <summary>Android <c>Build.PRODUCT</c>; empty on non-Android. Useful emulator signal (<c>sdk_*</c>/<c>vbox*</c>).</summary>
public string BuildProduct { get; internal set; }
/// <summary>Android <c>Build.SOC_MANUFACTURER</c> (API 31+); empty otherwise.</summary>
public string BuildSocManufacturer { get; internal set; }
/// <summary>Derived Mali architectural generation; <see cref="Quality.MaliGeneration.Unknown" /> on non-Mali / non-Android.</summary>
public MaliGeneration MaliGeneration { get; internal set; }
/// <summary>Android <c>PowerManager.getCurrentThermalStatus()</c> (API 29+); -1 if unsupported / not Android.</summary>
public int ThermalStatus { get; internal set; }
internal static HardwareStats CreateDefault()
{
return new HardwareStats
{
DeviceModel = SystemInfo.deviceModel,
GpuName = SystemInfo.graphicsDeviceName,
GpuMemorySizeMb = SystemInfo.graphicsMemorySize,
GpuMajorSeries = GpuMajorSeries.Unknown,
GpuMinorSeries = GpuMinorSeries.Unknown,
GpuSeriesNumber = 0,
SocName = "",
SystemMemorySizeMb = SystemInfo.systemMemorySize,
AndroidSdkInt = 0,
IsEmulator = false,
GraphicsDeviceType = SystemInfo.graphicsDeviceType,
GraphicsShaderLevel = SystemInfo.graphicsShaderLevel,
ScreenWidth = Screen.width,
ScreenHeight = Screen.height,
ScreenDpi = Screen.dpi,
ProcessorCount = SystemInfo.processorCount,
ProcessorFrequencyMHz = SystemInfo.processorFrequency,
ProcessorType = SystemInfo.processorType,
BuildManufacturer = "",
BuildBrand = "",
BuildModel = "",
BuildHardware = "",
BuildProduct = "",
BuildSocManufacturer = "",
MaliGeneration = MaliGeneration.Unknown,
ThermalStatus = -1
};
}
}
public static class HardwareInfo
{
// singleton cache
private static HardwareStats _cachedHardwareStats;
/// <summary>
/// Get hardware stats of running device
/// </summary>
/// <returns>HardwareStats</returns>
public static HardwareStats GetHardwareStats()
{
if (_cachedHardwareStats != null)
return _cachedHardwareStats;
_cachedHardwareStats = HardwareStats.CreateDefault();
#if UNITY_EDITOR_OSX || UNITY_IOS
HardwareInfoIos.SetIosHardwareStats(_cachedHardwareStats);
#elif UNITY_ANDROID
HardwareInfoAndroid.SetAndroidHardwareStats(_cachedHardwareStats);
#endif
return _cachedHardwareStats;
}
}
}