您当前的位置: 首页 >  Jave.Lin unity

Unity - 画质设置

Jave.Lin 发布时间:2022-03-17 12:48:33 ,浏览量:6

文章目录
  • 环境
  • GameQualitySettings

Show me Your Code, Talk Is Cheap.

以前自己写的类,现在重新写一份 代码

便于日后直接搬运使用,代码都是相当简单,都是直接调用 unity 的 API 设置即可,可以理解为就是搬运而已

环境

Unity : 2018.2.11f1 Pipeline : BRP

改用在:URP 也是很简单(部分 API 修改一下即可)

GameQualitySettings
// jave.lin 2022.03.17

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

// jave.lin : 输出系统的信息工具类
public class DumpSystemInfoUtil
{
    // jave.lin : 通过反射得方式获取不了
    public static string DumpSystemInfoByReflection()
    {
        var type = typeof(SystemInfo);
        // jave.lin : 下面发现反射不成功
        var fields = type.GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
        var fieldsToStrList = new List();
        foreach (var field in fields)
        {
            // 过滤 过期得 API
            var obsoAttris = field.GetCustomAttributes(typeof(ObsoleteAttribute), true);
            if (obsoAttris != null && obsoAttris.Length > 0) continue;
            fieldsToStrList.Add(field.Name + ":" + field.GetValue(null).ToString());
        }
        return string.Join("\n", fieldsToStrList.ToArray());
    }

    // jave.lin : 所以只能通过一个个得去输出
    public static string DumpSystemInfoByManualyPrint()
    {
        var list = new List(
            new string[]{
                "SystemInfo:\n",
                "\tbatteryLevel:" + SystemInfo.batteryLevel,
                "\tbatteryStatus:" + SystemInfo.batteryStatus,
                "\toperatingSystem:" + SystemInfo.operatingSystem,
                "\toperatingSystemFamily:" + SystemInfo.operatingSystemFamily,
                "\tprocessorType:" + SystemInfo.processorType,
                "\tprocessorFrequency:" + SystemInfo.processorFrequency,
                "\tprocessorCount:" + SystemInfo.processorCount,
                "\tsystemMemorySize:" + SystemInfo.systemMemorySize,
                "\tdeviceUniqueIdentifier:" + SystemInfo.deviceUniqueIdentifier,
                "\tdeviceName:" + SystemInfo.deviceName,
                "\tdeviceModel:" + SystemInfo.deviceModel,
                "\tsupportsAccelerometer:" + SystemInfo.supportsAccelerometer,
                "\tsupportsGyroscope:" + SystemInfo.supportsGyroscope,
                "\tsupportsLocationService:" + SystemInfo.supportsLocationService,
                "\tsupportsVibration:" + SystemInfo.supportsVibration,
                "\tsupportsAudio:" + SystemInfo.supportsAudio,
                "\tdeviceType:" + SystemInfo.deviceType,
                "\tgraphicsMemorySize:" + SystemInfo.graphicsMemorySize,
                "\tgraphicsDeviceName:" + SystemInfo.graphicsDeviceName,
                "\tgraphicsDeviceVendor:" + SystemInfo.graphicsDeviceVendor,
                "\tgraphicsDeviceID:" + SystemInfo.graphicsDeviceID,
                "\tgraphicsDeviceVendorID:" + SystemInfo.graphicsDeviceVendorID,
                "\tgraphicsDeviceType:" + SystemInfo.graphicsDeviceType,
                "\tgraphicsUVStartsAtTop:" + SystemInfo.graphicsUVStartsAtTop,
                "\tgraphicsDeviceVersion:" + SystemInfo.graphicsDeviceVersion,
                "\tgraphicsShaderLevel:" + SystemInfo.graphicsShaderLevel,
                "\tgraphicsMultiThreaded:" + SystemInfo.graphicsMultiThreaded,
                "\tsupportsShadows:" + SystemInfo.supportsShadows,
                "\tsupportsRawShadowDepthSampling:" + SystemInfo.supportsRawShadowDepthSampling,
                "\tsupportsMotionVectors:" + SystemInfo.supportsMotionVectors,
                "\tsupports3DTextures:" + SystemInfo.supports3DTextures,
                "\tsupports2DArrayTextures:" + SystemInfo.supports2DArrayTextures,
                "\tsupports3DRenderTextures:" + SystemInfo.supports3DRenderTextures,
                "\tsupportsCubemapArrayTextures:" + SystemInfo.supportsCubemapArrayTextures,
                "\tcopyTextureSupport:" + SystemInfo.copyTextureSupport,
                "\tsupportsComputeShaders:" + SystemInfo.supportsComputeShaders,
                "\tsupportsInstancing:" + SystemInfo.supportsInstancing,
                "\tsupportsHardwareQuadTopology:" + SystemInfo.supportsHardwareQuadTopology,
                "\tsupports32bitsIndexBuffer:" + SystemInfo.supports32bitsIndexBuffer,
                "\tsupportsSparseTextures:" + SystemInfo.supportsSparseTextures,
                "\tsupportedRenderTargetCount:" + SystemInfo.supportedRenderTargetCount,
                "\tsupportsMultisampledTextures:" + SystemInfo.supportsMultisampledTextures,
                "\tsupportsMultisampleAutoResolve:" + SystemInfo.supportsMultisampleAutoResolve,
                "\tsupportsTextureWrapMirrorOnce:" + SystemInfo.supportsTextureWrapMirrorOnce,
                "\tusesReversedZBuffer:" + SystemInfo.usesReversedZBuffer,
                "\tnpotSupport:" + SystemInfo.npotSupport,
                "\tmaxTextureSize:" + SystemInfo.maxTextureSize,
                "\tmaxCubemapSize:" + SystemInfo.maxCubemapSize,
                "\tsupportsAsyncCompute:" + SystemInfo.supportsAsyncCompute,
                "\tsupportsAsyncGPUReadback:" + SystemInfo.supportsAsyncGPUReadback,
                "\tsupportsMipStreaming:" + SystemInfo.supportsMipStreaming,
            });
        return string.Join("\n", list.ToArray());
    }
}

// jave.lin : 设备定档级别枚举
public enum eDeviceLevel
{
    Unknow = -1,
    VeryLow = 0,
    Low,
    Middle,
    High,
}

// jave.lin : 画质级别
public enum eQualityLevel
{
    Low = 1,
    Middle = 2,
    High = 3,
    Ultra = 4,
}

// jave.lin : shader lod
public enum eShaderLOD
{
    //High = 800,
    //Middle = 400,
    //Low = 200,
    //VeryLow = 100,
    //UnLimit = -1,
    // jave.lin : 太低的值对 built-in shader 的影响太大
    High = 800,
    Middle = 600,
    Low = 400,
    VeryLow = 200,
    UnLimit = -1,
}

// jave.lin : 游戏的质量设置类
public class GameQualitySettings
{
    private const string QS_POWER_SAVE_MODE_KEY = "graphics_setting.power_save_mode";
    private const string QS_QUALITY_LEVEL_KEY = "graphics_setting.quality_level";

    // 当 品质有调整事出发的事件函数
    public static Action onLevelChanged;

    // 源来的 AA 和 阴影设置
    private static int srcAntiAliasing;
    private static ShadowQuality srcShadows;

    // 当前 品质等级
    private static eQualityLevel curLevel;

    // 获取 设备定档的质量级别
    public static eQualityLevel DeviceAdapterLevel
    {
        get; private set;
    }

    // 获取 或 设置 公开给外部的画质设置的属性
    public static eQualityLevel GraphicsLevel
    {
        get { return curLevel; }
        set
        {
            if (curLevel != value)
            {
                curLevel = value;
                _SetCurLevel(value);
                PlayerPrefs.SetInt(QS_QUALITY_LEVEL_KEY, (int)value);
                if (null != onLevelChanged)
                {
                    onLevelChanged.Invoke(value);
                }
            }
        }
    }

    // 获取 或 设置 省电模式, true: 30FPS, false: 60FPS
    public static bool PowerSaveMode
    {
        get
        {
            return Application.targetFrameRate = 2500 &&
            SystemInfo.processorCount >= 8 &&
            SystemInfo.systemMemorySize >= (6 * 1024) &&
            SystemInfo.graphicsMemorySize >= (2 * 1024) &&
            SystemInfo.graphicsShaderLevel >= 30 &&
            SystemInfo.graphicsMultiThreaded &&
            SystemInfo.supportsShadows &&
            SystemInfo.supportsInstancing &&
            SystemInfo.supports32bitsIndexBuffer
            )
        {
            return eQualityLevel.Ultra;
        }
        else if (SystemInfo.processorFrequency >= 2000 &&
            SystemInfo.processorCount >= 4 &&
            SystemInfo.systemMemorySize >= (4 * 1024) &&
            SystemInfo.graphicsMemorySize >= (1 * 1024) &&
            SystemInfo.graphicsShaderLevel >= 20
            )
        {
            return eQualityLevel.High;
        }
        else if (SystemInfo.processorFrequency >= 1500 &&
            SystemInfo.processorCount >= 2 &&
            SystemInfo.systemMemorySize >= (2 * 1024) &&
            SystemInfo.graphicsMemorySize >= (512) &&
            SystemInfo.graphicsShaderLevel >= 10
            )
        {
            return eQualityLevel.Middle;
        }
        else
        {
            return eQualityLevel.Low;
        }
    }

    // 设置 当前品质等级
    private static void _SetCurLevel(eQualityLevel level)
    {
        _SetAntiAliasing(level);
        _SetResolution(level);
        _SetTexMipmapOffset(level);
        _SetShadow(level);
        _SetLODBias(level);
        _SetGraphicsTier(level);
        _SetShaderLOD(level);
        _SetGlobalShaderKW(level);
    }

    // 设置 AA
    private static void _SetAntiAliasing(eQualityLevel level)
    {
        if (level >= eQualityLevel.High)
        {
            QualitySettings.antiAliasing = srcAntiAliasing;
        }
        else
        {
            QualitySettings.antiAliasing = 0;
        }
    }

    // 设置分辨率
    private static void _SetResolution(eQualityLevel level)
    {
        // jave.lin : BRP(Built-In Rendering Pipeline) 中
        // 需要对应的 Camera 开启 AllowDynamicResolution 后才能生效
        switch (level)
        {
            case eQualityLevel.Low:
                QualitySettings.resolutionScalingFixedDPIFactor = 0.75f;
                break;
            case eQualityLevel.Middle:
                QualitySettings.resolutionScalingFixedDPIFactor = 0.85f;
                break;
            case eQualityLevel.High:
                QualitySettings.resolutionScalingFixedDPIFactor = 0.85f;
                break;
            case eQualityLevel.Ultra:
                QualitySettings.resolutionScalingFixedDPIFactor = 1.00f;
                break;
        }
    }

    // 设置 Tex 纹理 mipmap offset
    private static void _SetTexMipmapOffset(eQualityLevel level)
    {
        switch (level)
        {
            case eQualityLevel.Low:
                QualitySettings.masterTextureLimit = DeviceAdapterLevel             
关注
打赏
1688896170
查看更多评论
0.1256s