Version: 6000.3
语言: 中文
动态分辨率简介
控制动态缩放发生时间

使用动态分辨率控制缩放

您可以通过 ScalableBufferManager 控制缩放。ScalableBufferManager 允许您控制已标记的所有渲染目标的动态宽度和高度比例动态分辨率:摄像机设置,允许你动态缩放单个渲染目标,以减少GPU上的工作负载。更多信息
请参阅术语表
系统扩展。

例如,假设您的应用程序以理想的帧速率运行,但在某些情况下,由于粒子增加、后期效果和屏幕复杂性增加,GPU 性能会下降。Unity FrameTimingManager 允许您检测 CPU 或 GPU 性能何时开始下降。因此,您可以使用FrameTimingManager以计算新的所需宽度和高度比例,以将帧速率保持在所需范围内,并将比例降低到该值以保持性能稳定(立即或在设定的帧数内逐渐)。当屏幕复杂性降低且 GPU 性能一致时,您可以将宽度和高度缩放提高回您计算出的 GPU 可以处理的值。

此示例脚本演示了 API 的基本用法。将其添加到场景中的摄像机,然后选中 摄像机设置中的 允许动态分辨率(Allow Dynamic Resolution) 。你还需要打开 播放器(Player) 设置(菜单:编辑(Edit) > 项目设置(Project Settings),然后选择 播放器(Player) 类别),并选中 启用帧时序统计(Enable Frame Timing Stats) 复选框。有关 启用帧计时统计(Enable Frame Timing Stats) 属性背后的功能的更多信息,请参阅 FrameTimingManager

单击鼠标或用一根手指点击屏幕,会将高度和宽度分辨率降低scaleWidthIncrementscaleHeightIncrement变量。用两根手指点击会以相同的增量提高分辨率。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DynamicResolutionTest : MonoBehaviour
{
    public Text screenText;

    FrameTiming[] frameTimings = new FrameTiming[3];

    public float maxResolutionWidthScale = 1.0f;
    public float maxResolutionHeightScale = 1.0f;
    public float minResolutionWidthScale = 0.5f;
    public float minResolutionHeightScale = 0.5f;
    public float scaleWidthIncrement = 0.1f;
    public float scaleHeightIncrement = 0.1f;

    float m_widthScale = 1.0f;
    float m_heightScale = 1.0f;

    // Variables for dynamic resolution algorithm that persist across frames
    uint m_frameCount = 0;

    const uint kNumFrameTimings = 2;

    double m_gpuFrameTime;
    double m_cpuFrameTime;

    // Use this for initialization
    void Start()
    {
        int rezWidth = (int)Mathf.Ceil(ScalableBufferManager.widthScaleFactor * Screen.currentResolution.width);
        int rezHeight = (int)Mathf.Ceil(ScalableBufferManager.heightScaleFactor * Screen.currentResolution.height);
        screenText.text = string.Format("Scale: {0:F3}x{1:F3}\nResolution: {2}x{3}\n",
            m_widthScale,
            m_heightScale,
            rezWidth,
            rezHeight);
    }

    // Update is called once per frame
    void Update()
    {
        float oldWidthScale = m_widthScale;
        float oldHeightScale = m_heightScale;

        // One finger lowers the resolution
        if (Input.GetButtonDown("Fire1"))
        {
            m_heightScale = Mathf.Max(minResolutionHeightScale, m_heightScale - scaleHeightIncrement);
            m_widthScale = Mathf.Max(minResolutionWidthScale, m_widthScale - scaleWidthIncrement);
        }

        // Two fingers raises the resolution
        if (Input.GetButtonDown("Fire2"))
        {
            m_heightScale = Mathf.Min(maxResolutionHeightScale, m_heightScale + scaleHeightIncrement);
            m_widthScale = Mathf.Min(maxResolutionWidthScale, m_widthScale + scaleWidthIncrement);
        }

        if (m_widthScale != oldWidthScale || m_heightScale != oldHeightScale)
        {
            ScalableBufferManager.ResizeBuffers(m_widthScale, m_heightScale);
        }
        DetermineResolution();
        int rezWidth = (int)Mathf.Ceil(ScalableBufferManager.widthScaleFactor * Screen.currentResolution.width);
        int rezHeight = (int)Mathf.Ceil(ScalableBufferManager.heightScaleFactor * Screen.currentResolution.height);
        screenText.text = string.Format("Scale: {0:F3}x{1:F3}\nResolution: {2}x{3}\nScaleFactor: {4:F3}x{5:F3}\nGPU: {6:F3} CPU: {7:F3}",
            m_widthScale,
            m_heightScale,
            rezWidth,
            rezHeight,
            ScalableBufferManager.widthScaleFactor,
            ScalableBufferManager.heightScaleFactor,
            m_gpuFrameTime,
            m_cpuFrameTime);
    }

    // Estimate the next frame time and update the resolution scale if necessary.
    private void DetermineResolution()
    {
        ++m_frameCount;
        if (m_frameCount <= kNumFrameTimings)
        {
            return;
        }
        FrameTimingManager.CaptureFrameTimings();
        FrameTimingManager.GetLatestTimings(kNumFrameTimings, frameTimings);
        if (frameTimings.Length < kNumFrameTimings)
        {
            Debug.LogFormat("Skipping frame {0}, didn't get enough frame timings.",
                m_frameCount);

            return;
        }

        m_gpuFrameTime = (double)frameTimings[0].gpuFrameTime;
        m_cpuFrameTime = (double)frameTimings[0].cpuFrameTime;
    }
}
动态分辨率简介
控制动态缩放发生时间