スケールは ScalableBufferManager を使用してコントロールできます。ScalableBufferManager によって、動的解像度システムでスケールするように設定したすべてのレンダーターゲットに対し、動的な幅と高さのスケールをコントロールできるようになります。
例えば、アプリケーションが望ましいフレームレートで実行されているものの、パーティクルの増加、ポストエフェクト、画面の複雑さが組み合わさり、ある状況下で GPU パフォーマンスが低下するとします。Unity の FrameTimingManager を使用することで、CPU または GPU のパフォーマンスが低下し始めた場合に検出できます。この FrameTimingManager を使用することで、フレームレートを所定の範囲内に保つために望ましい幅と高さのスケールを新たに計算し、スケールをその値まで下げて、パフォーマンスを (瞬時に、または設定されたフレーム数で徐々に) 安定させることができます。画面の複雑さが軽減され、GPU のパフォーマンスが安定したら、幅と高ささのスケールを GPU が処理できる計算値に戻してもかまいません。
このスクリプト例は、API の基本的な使用方法を示しています。API をシーンのカメラに追加し、Camera 設定で Allow Dynamic Resolution (動的解像度を許可) にチェックを入れます。また、Player 設定も開きます (メニュー: Edit > Project Settings で Player カテゴリを選択)。ここで Enable Frame Timing Stats (フレームタイミングの統計を有効にする) チェックボックスを有効にします。Enable Frame Timing Stats プロパティの機能の詳細については、FrameTimingManager を参照してください。
マウスをクリックするか、1 本の指で画面をタップすると、高さと幅の解像度がそれぞれ scaleWidthIncrement 変数と scaleHeightIncrement 変数の値だけ下がります。2 本の指でタップすると同じ値だけ解像度が上がります。
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;
}
}