noradninja

Camera Resolution Scaler

Jun 5th, 2021 (edited)
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode]
  4.  
  5. // Add this to your main camera//////////////////////////////////////////////////////////////////////////////////////////////
  6. public class CameraResolutionScaler : MonoBehaviour
  7. {
  8.     public enum currentResolution
  9.     {
  10.         [Tooltip("960x544")] Full,
  11.         [Tooltip("720x408")] Mid,
  12.         [Tooltip("640x363")] Low,
  13.         [Tooltip("480x272")] PSP
  14.     }
  15.  
  16.     public enum internalResolution
  17.     {
  18.         [Tooltip("960x544")] Full,
  19.         [Tooltip("720x408")] Mid,
  20.         [Tooltip("640x363")] Low,
  21.         [Tooltip("480x272")] PSP
  22.     }
  23.  
  24.     public bool enableInternalResolution = true;
  25.     public internalResolution InternalResolution;
  26.     public currentResolution screenResolution;
  27.     public FilterMode filterMode = FilterMode.Trilinear;
  28.     private new Camera camera;
  29.     private int height;
  30.     private Rect originalRect;
  31.     private float renderDivisor;
  32.     private RenderTexture renderTex;
  33.     private Rect scaledRect;
  34.     private int width;
  35.    
  36.     private RenderTexture GetTemporaryTexture(int width, int height) //set up our RT
  37.     {
  38.         var temporaryTexture = RenderTexture.GetTemporary(480, 272);
  39.         temporaryTexture.wrapMode = TextureWrapMode.Clamp;
  40.         temporaryTexture.anisoLevel = 0;
  41.         temporaryTexture.filterMode = filterMode;
  42.         return temporaryTexture;
  43.     }
  44.    
  45.     private void Awake()
  46.     {
  47.         camera = GetComponent<Camera>();
  48.  
  49.         if (!Application.isEditor)
  50.             switch (screenResolution)
  51.             {
  52.                 //set resolution and 30Hz vsync
  53.                 case currentResolution.Full:
  54.                     Screen.SetResolution(960, 544, true);
  55.                     QualitySettings.vSyncCount = 2;
  56.                     break;
  57.                 case currentResolution.Mid:
  58.                     Screen.SetResolution(720, 408, true);
  59.                     QualitySettings.vSyncCount = 2;
  60.                     break;
  61.                 case currentResolution.Low:
  62.                     Screen.SetResolution(640, 368, true);
  63.                     QualitySettings.vSyncCount = 2;
  64.                     break;
  65.                 case currentResolution.PSP:
  66.                     Screen.SetResolution(480, 272, true);
  67.                     QualitySettings.vSyncCount = 2;
  68.                     break;
  69.             }
  70.         else //disable vsync in Editor
  71.             QualitySettings.vSyncCount = 1;
  72.     }
  73.  
  74.     private void OnDestroy()
  75.     {
  76.         camera.rect = originalRect;
  77.     }
  78.  
  79.     private void OnPreRender()
  80.     {
  81.         if (enableInternalResolution)
  82.         {
  83.             switch (InternalResolution) //set up values for RT
  84.             {
  85.                 case internalResolution.Full:
  86.                     width = 960;
  87.                     height = 544;
  88.                     renderDivisor = 1;
  89.                     break;
  90.                 case internalResolution.Mid:
  91.                     width = 720;
  92.                     height = 408;
  93.                     renderDivisor = 1.334f;
  94.                     break;
  95.                 case internalResolution.Low:
  96.                     width = 640;
  97.                     height = 368;
  98.                     renderDivisor = 1.5f;
  99.                     break;
  100.                 case internalResolution.PSP:
  101.                     width = 480;
  102.                     height = 272;
  103.                     renderDivisor = 2.0f;
  104.                     break;
  105.             }
  106.  
  107.             originalRect = camera.rect;
  108.             scaledRect.Set(originalRect.x, originalRect.y,
  109.                             originalRect.width / renderDivisor,
  110.                             originalRect.height / renderDivisor);
  111.             camera.rect = scaledRect; //scale cam rect for RT
  112.         }
  113.     }
  114.  
  115.     private void OnRenderImage(RenderTexture src, RenderTexture dest)
  116.     {
  117.         if (!enableInternalResolution) Graphics.Blit(src, dest); //just blit to screen if we arent scaling
  118.         else //create and blit RT for resolution scaling
  119.         {
  120.             renderTex = GetTemporaryTexture(width, height);
  121.             camera.rect = originalRect;
  122.             src.filterMode = filterMode;
  123.             Graphics.Blit(src, renderTex);
  124.             Graphics.Blit(renderTex, dest);
  125.             RenderTexture.ReleaseTemporary(renderTex);
  126.         }
  127.     }
  128. }
Add Comment
Please, Sign In to add comment