コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Game ビューの解像度を特定の範囲でのみ変更する構造体

ソースコード

#if UNITY_EDITOR

using System;
using UnityEditor;

namespace Kogane
{
    public struct SetPlayModeWindowCustomRenderingResolutionScope : IDisposable
    {
        private readonly uint m_width;
        private readonly uint m_height;

        private bool m_isInitialized;

        public SetPlayModeWindowCustomRenderingResolutionScope
        (
            uint width,
            uint height
        ) : this
        (
            width: width,
            height: height,
            baseName: $"{width}x{height}"
        )
        {
        }

        public SetPlayModeWindowCustomRenderingResolutionScope
        (
            uint   width,
            uint   height,
            string baseName
        )
        {
            PlayModeWindow.GetRenderingResolution
            (
                out m_width,
                out m_height
            );

            PlayModeWindow.SetCustomRenderingResolution
            (
                width: width,
                height: height,
                baseName: baseName
            );

            m_isInitialized = true;
        }

        public void Dispose()
        {
            if ( !m_isInitialized ) return;
            m_isInitialized = false;

            PlayModeWindow.SetCustomRenderingResolution
            (
                width: m_width,
                height: m_height,
                baseName: $"{m_width}x{m_height}"
            );
        }
    }
}

#endif