In-depth understanding of Unity's QualitySettings class: a detailed technical guide (3)

When you're developing a Unity game, it's crucial to understand how to manage and optimize the graphics quality of your game. Unity provides a powerful class called QualitySettings, which allows you to obtain and modify the graphics quality settings of the game at runtime. This article will analyze the QualitySettings class in depth, and provide detailed usage and examples of each attribute.

QualitySettingsOverview

The QualitySettings class provides a way to get and modify Unity's graphics quality settings. These settings include anti-aliasing, shadows, textures, resolution, and more. You can use this class to dynamically adjust these settings to provide the best gaming experience based on the capabilities of the device.

  1. Graphics quality level related:
    names: A string array containing the names of all preset graphics quality levels
    GetQualityLevel(): Returns the index of the currently selected graphics quality level.
    SetQualityLevel(int index, bool applyExpensiveChanges = true): used to set the current graphics quality level
    IncreaseLevel(bool applyExpensiveChanges = true): increase one level current graphics quality level
    DecreaseLevel(bool applyExpensiveChanges = true): decrease one level current graphics quality level
    GetRenderPipelineAssetAt(): get the RenderPipelineAsset of the specified quality level

names : A string array containing the names of all preset graphics quality levels. You can get the name of each level through this array

string[] qualityLevelNames = QualitySettings.names;
foreach (string name in qualityLevelNames)
{
    
    
    Debug.Log(name);
}

GetQualityLevel() : This method returns the index of the currently selected graphics quality level

int currentQualityLevel = QualitySettings.GetQualityLevel();
Debug.Log("当前图像质量等级: " + currentQualityLevel);

SetQualityLevel (int index, bool applyExpensiveChanges = true): This method is used to set the current graphics quality level. You can use this to dynamically change the graphics quality level based on the capabilities of the device

QualitySettings.SetQualityLevel(3, true);

IncreaseLevel(bool applyExpensiveChanges = true) and DecreaseLevel(bool applyExpensiveChanges = true) : These two methods are used to increase or decrease the current graphics quality level by one level. They can be used to create a settings menu that lets players dynamically adjust graphics quality in-game

QualitySettings.IncreaseLevel(true);
QualitySettings.DecreaseLevel(true);

GetRenderPipelineAssetAt(int index) : Returns a RenderPipelineAsset at the specified quality level. RenderPipelineAsset contains the setting information of the rendering pipeline, which can be used to configure how to render the scene

// 获取当前质量等级的RenderPipelineAsset
RenderPipelineAsset renderPipelineAsset = QualitySettings.GetRenderPipelineAssetAt(QualitySettings.GetQualityLevel());
if(renderPipelineAsset != null)
{
    
    
    Debug.Log("当前质量等级的RenderPipelineAsset名称:" + renderPipelineAsset.name);
}
else
{
    
    
    Debug.Log("当前质量等级没有配置RenderPipelineAsset");
}


  1. Graphics settings related
    anisotropicFiltering: This property is used to get or set the anisotropic filtering mode
    antiAliasing: This property controls the quality level of full-screen anti-aliasing
    billboardsFaceCameraPosition: This property determines whether the orientation of the Billboard is based on the camera's position.
    desiredColorSpace: This property gets the desired color space, Linear or Gamma
    masterTextureLimit: This property is used to get or set the maximum level of the mipmap
    realtimeReflectionProbes: This property determines whether real-time reflection probes are enabled
    pixelLightCount: This property is used to get or set the number of pixel lights that can be used in the scene The maximum value
    softParticles: This attribute is used to get or set whether to enable soft particles
    vSyncCount: This attribute sets the frame number interval of vertical synchronization.
    resolutionScalingFixedDPIFactor: This attribute is used to control the scaling of the screen resolution. Used to set a fixed DPI factor for resolution scaling on UI elements and text rendering. Its value is between 0.0 - 1.0, with 0 representing low DPI and 1 representing high DPI. This setting is mainly used to make UI elements and text render correctly and clearly on screens with different resolutions and DPIs.
    renderPipeline: This property gets or sets the currently used RenderPipelineAsset
    softVegetation: Used to determine whether to enable the "soft vegetation" option
    maxQueuedFrames: This is a property that sets the number of frames that Unity allows to queue.

anisotropicFiltering : This property is used to get or set the anisotropic filtering mode. Anisotropic filtering is a texture filtering method that improves the visual quality of textures on slopes and at a distance. The clarity of the texture can be improved when the line of sight is close to the level. The value of this property is the AnisotropicFiltering enumeration type, and there are three options: Disable (disabled), Enable (enabled) and ForceEnable (forced enabled).

QualitySettings.anisotropicFiltering = AnisotropicFiltering.Enable;
Debug.Log(QualitySettings.anisotropicFiltering); //输出:Enable

antiAliasing : This property controls the quality level of full-screen antialiasing. Anti-aliasing is a graphics technique used to smooth the edges of pixels, reducing the "jaggy" effect. The value can be 0 (disabled), 2, 4, 8, etc., representing the anti-aliasing level used.

QualitySettings.antiAliasing = 2;
Debug.Log(QualitySettings.antiAliasing); //输出:2

billboardsFaceCameraPosition : This property determines whether the orientation of the Billboard is based on the camera's position. When this property is true, the Billboard will always face the camera. When false, the billboard will face the direction of the camera, regardless of the camera's height position.

QualitySettings.billboardsFaceCameraPosition = true;
Debug.Log(QualitySettings.billboardsFaceCameraPosition); //输出:True

desiredColorSpace : This property gets the desired color space, Linear or Gamma

Debug.Log(QualitySettings.desiredColorSpace); //输出:Gamma(默认值,具体输出取决于当前设置)

masterTextureLimit : This property is used to get or set the maximum level of the mipmap. Higher limits use smaller textures, saving video memory but reducing texture quality.

QualitySettings.masterTextureLimit = 1;
Debug.Log(QualitySettings.masterTextureLimit); //输出:1

realtimeReflectionProbes : Determines whether realtime reflection probes are enabled.

QualitySettings.realtimeReflectionProbes = true;
Debug.Log(QualitySettings.realtimeReflectionProbes); //输出:True

pixelLightCount : This property is used to get or set the maximum number of pixel lights that can be used in the scene. Increasing this value improves visual quality at the expense of performance overhead.
softParticles : This property is used to get or set whether to enable soft particles. Soft particles are a rendering technique used to eliminate hard edges between particles and the ground.

QualitySettings.pixelLightCount = 4;
QualitySettings.softParticles = true;

vSyncCount : Get or set the vertical sync count

    QualitySettings.vSyncCount = 1;
    Debug.Log(QualitySettings.vSyncCount); //输出:1

softVegetation : Used to determine if the Soft Vegetation option is enabled. When this option is enabled, vegetation (such as grass and small trees) will become translucent when the player's camera is close to them, preventing the situation where the vegetation completely blocks the view

// 开启或关闭 Soft Vegetation
QualitySettings.softVegetation = true;
// 检查是否开启 Soft Vegetation
if(QualitySettings.softVegetation)
{
    
    
    Debug.Log("Soft Vegetation 选项已开启");
}

maxQueuedFrames : is a property used to specify the size of the frame queue in Unity. It determines how many frames of data Unity can store when rendering. In practice, if your game renders at a higher frame rate than the display refresh rate, this property can help you control the size of the frame queue.
This attribute is more difficult to understand, here is an example
If the refresh rate of your monitor is 60Hz, but the rendering frame rate of your game is 300FPS, this means that in one second, your game may render data of about 300 frames, but in fact the monitor can only display 60 frame. In this case, excess frames are stored in the frame queue, waiting to be displayed when the display is refreshed.

The value of maxQueuedFrames is to specify how many frames of data the frame queue can store. If you set it too large, it will consume a lot of memory to store these undisplayed frames. If you set it too low, you can cause screen tearing because there aren't enough frames in the frame queue to fill the monitor's refresh gap.

// 设置帧队列大小为2
QualitySettings.maxQueuedFrames = 2;

The code above sets the frame queue size to 2. This means that Unity will hold up to 2 frames of rendered data in memory, waiting to be displayed when the monitor refreshes. Frames above this amount will be discarded and not rendered.
Notice, the default value of this property is 2, usually, you don't need to modify it. Unless you have special needs, like you need to optimize on specific hardware, or you need to solve specific rendering problems.


  1. LOD and particle system settings
    lodBias: This property is used to get or set the LOD distance
    maximumLODLevel: This property is used to get or set the maximum LOD level
    particleRaycastBudget: This property is used to set the maximum number of ray casts per frame for particle collision detection.

lodBias : This property is used to get or set the LOD distance. LOD technology can dynamically adjust the model complexity and texture size of the object according to the distance between the object and the camera, thereby optimizing game performance. Higher values ​​will result in higher LOD objects being seen from farther away, which may impact performance.
maximumLODLevel : This property is used to get or set the maximum LOD level. Setting it to a lower value improves performance, but may reduce visual quality.

QualitySettings.lodBias = 2.0f;
QualitySettings.maximumLODLevel = 1;

particleRaycastBudget : This property is used to set the maximum number of raycasts per frame for particle collision detection.

QualitySettings.particleRaycastBudget = 3000;
Debug.Log(QualitySettings.particleRaycastBudget); //输出:3000

  1. Character and Skin Settings
    skinWeights: Used to control blend weights for skinned meshes.
    blendWeights: The blending weight used when rendering the character.

skinWeights : used to control the blend weights of the skinned mesh

 QualitySettings.skinWeights = SkinWeights.OneBone;
 Debug.Log(QualitySettings.skinWeights); //输出:OneBone

blendWeights : Determines the blend weights to use when rendering the character

QualitySettings.blendWeights = BlendWeights.FourBones;
Debug.Log(QualitySettings.blendWeights); //输出:FourBones

  1. Rendering shadow related properties
    shadows: This property allows you to set the type of shadow
    shadowResolution: This property is used to get or set the shadow resolution
    shadowDistance: This property is used to get or set the maximum distance for shadow rendering
    shadowmaskMode: This property is used to control the shadow mask mode
    shadowNearPlaneOffset: This property Used to control the offset of the near clipping plane of the shadow of all light sources
    shadowProjection: This property is used to control the shadow projection method, and you can choose Close Fit or Stable Fit.
    shadowCascades: This property is used to control the number of shadow cascades used by parallel light sources. Shadow cascade technology can provide different shadow qualities at different distance levels.
    shadowCascade2Split: This attribute determines the separation position of two cascaded shadows when using a 2-level shadow cascade
    shadowCascade4Split: This property is a Vector3, which determines the separation position of four cascaded shadows when using a 4-level shadow cascade

shadows : This property allows you to set the type of shadows, such as hard shadows or soft shadows, or turn shadows off completely.
shadowResolution : This property is used to get or set the shadow resolution. Higher resolutions produce sharper shadows at the cost of increased performance.
shadowDistance : This property is used to get or set the maximum distance for shadow rendering. Reducing this distance can improve performance, but will reduce the visual quality of the game.

QualitySettings.shadows = ShadowQuality.All;// 设置阴影的类型
QualitySettings.shadowResolution = ShadowResolution.High;
QualitySettings.shadowDistance = 100;

shadowmaskMode : This property is used to control the shadow mask mode, which can be Shadowmask or Distance Shadowmask.

QualitySettings.shadowmaskMode = ShadowmaskMode.DistanceShadowmask;
Debug.Log(QualitySettings.shadowmaskMode); //输出:DistanceShadowmask

shadowCascades : This property is used to control the number of shadow cascades used by directional lights. Shadow cascade technology can provide different shadow quality at different distance levels.

QualitySettings.shadowCascades = 2;// 改变阴影级联数量为2
Debug.Log(QualitySettings.shadowCascades); //输出当前的阴影级联数量:2

shadowCascade2Split : This property determines where two cascaded shadows are separated when using a 2-level shadow cascade. Its value ranges from 0 to 1 and represents the percentage of distance from the camera. For example, if its value is 0.25, then the first cascade shadow will extend to 25% of the field of view, and the second cascade shadow will extend to the remaining 75%.
shadowCascade4Split : This attribute is a Vector3, which determines the separation position of the four cascade shadows when using a 4-level shadow cascade. Each component of the Vector3 is between 0 and 1, and they represent the percentage of the distance of the first, second, and third cascaded shadows from the camera, respectively. For example, if its value is (0.2, 0.3, 0.5), then the first cascade of shadows will extend to 20% of the field of view, the second cascade of shadows will extend to the next 10%, and the third cascade of The shadow extends to the next 20%, and the last cascaded shadow extends to the remaining 50%.

注意, when the value of QualitySettings.shadowCascades is 2, use shadowCascade2Split; when the value of QualitySettings.shadowCascades is 4, use shadowCascade4Split.

// 设置使用2级阴影级联
QualitySettings.shadowCascades = 2;
// 设置两个级联阴影的分隔位置
QualitySettings.shadowCascade2Split = 0.25f;

// 设置使用4级阴影级联
QualitySettings.shadowCascades = 4;
// 设置四个级联阴影的分隔位置
QualitySettings.shadowCascade4Split = new Vector3(0.1f, 0.2f, 0.3f);

  1. Asynchronous upload related
    asyncUploadBufferSize: This property sets the buffer size of asynchronous data upload (in MB)
    asyncUploadPersistentBuffer: whether to keep the buffer of asynchronous upload
    asyncUploadTimeSlice: this property sets the maximum time slice (in milliseconds) that can be used for each frame of asynchronous data upload

asyncUploadBufferSize : This property sets the buffer size (in MB) for asynchronous data uploads. When Unity needs to upload data (such as texture or mesh data) to the GPU, it can choose to use asynchronous upload, which means that the data upload can happen in the background without blocking the main thread. Increasing the size of this buffer may reduce the impact of data uploads on the main thread, but will also increase memory usage.

    // 输出当前异步数据上传的缓冲区大小
    Debug.Log("当前异步数据上传的缓冲区大小: " + QualitySettings.asyncUploadBufferSize + "MB");
    // 改变异步数据上传的缓冲区大小为512MB
    QualitySettings.asyncUploadBufferSize = 512;   
    // 再次输出当前异步数据上传的缓冲区大小
    Debug.Log("更改后的异步数据上传的缓冲区大小: " + QualitySettings.asyncUploadBufferSize + "MB");

asyncUploadPersistentBuffer : When this property is set to true, Unity will persist asynchronously uploaded buffers even when they are no longer needed. This reduces the number of times Unity needs to create and destroy buffers, potentially improving performance. However, this also increases memory usage, as buffers are retained rather than destroyed.

    QualitySettings.asyncUploadPersistentBuffer = true;   
    // 输出当前异步数据上传的缓冲区是否被保留
    Debug.Log("更改后异步数据上传的缓冲区是否被保留: " + QualitySettings.asyncUploadPersistentBuffer);

asyncUploadTimeSlice : This property sets the maximum time slice (in milliseconds) that an asynchronous data upload can use per frame. Reducing this value prevents asynchronous data uploads from taking too much CPU time, but may make data uploads take longer.

    // 改变异步数据上传每帧可以使用的最大时间片为4ms
    QualitySettings.asyncUploadTimeSlice = 4;
    // 输出当前异步数据上传每帧可以使用的最大时间片
    Debug.Log("更改后的异步数据上传每帧可以使用的最大时间片: " + QualitySettings.asyncUploadTimeSlice + "ms");

Note that the usage and effect of these properties may be affected by your specific hardware and Unity version. If you plan to change these settings, it's a good idea to test them first to make sure changing them will have the desired effect.


  1. Mipmap stream processing related
    streamingMipmapsActive: This property is used to detect whether the streaming media mipmap is activated.
    streamingMipmapsAddAllCameras: Whether all cameras should contribute to mipmap streaming.
    streamingMipmapsMemoryBudget: Used to control the memory budget of streaming mipmaps.
    streamingMipmapsRenderersPerFrame: Used to set the number of renderers that can start loading streaming mipmaps per frame.
    streamingMipmapsMaxFileIORequests: Set the maximum number of mipmap flow file I/O requests that Unity can send at one time.
    streamingMipmapsMaxLevelReduction: This property allows you to set the maximum degradation level of the mipmap.

streamingMipmapsActive : Get or set whether streaming Mipmap is active

    QualitySettings.streamingMipmapsActive = true;
    Debug.Log(QualitySettings.streamingMipmapsActive); //输出:True

streamingMipmapsMemoryBudget : Get or set the memory budget of streaming Mipmaps

    QualitySettings.streamingMipmapsMemoryBudget = 512;
    Debug.Log(QualitySettings.streamingMipmapsMemoryBudget); //输出:512

streamingMipmapsMaxFileIORequests : This property sets the maximum number of mipmap stream file I/O requests that Unity can send at one time.

    // 设置Unity一次可以发送的最大的mipmap流文件I/O请求的数量
    QualitySettings.streamingMipmapsMaxFileIORequests = 10;

streamingMipmapsMaxLevelReduction : This property allows you to set the maximum reduction level for mipmaps when memory is tight.

    // 设置内存吃紧时,mipmap的最大降级级别
    QualitySettings.streamingMipmapsMaxLevelReduction = 2;

in conclusion

Unity's QualitySettings class is a powerful tool that helps you dynamically adjust your game's graphics quality based on device capabilities. By gaining a solid understanding of all class members and usage of this class, you can better optimize your game to provide the best possible player experience.

Guess you like

Origin blog.csdn.net/qq_33795300/article/details/131492273