Skip to content

Instantly share code, notes, and snippets.

@Glavak
Last active July 25, 2024 19:04
Show Gist options
  • Save Glavak/14224bf48cc5efb63c5816aed661decc to your computer and use it in GitHub Desktop.
Save Glavak/14224bf48cc5efb63c5816aed661decc to your computer and use it in GitHub Desktop.
Скрипт для уменьшения разрешения камеры в Unity, для туториала https://youtu.be/cpiNuSJBqRQ
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ResolutionScaler : MonoBehaviour
{
[Range(2, 16)] public float Scale = 2;
private Camera cameraComponent;
private RenderTexture texture;
private void Start()
{
CreateTexture();
}
private void CreateTexture()
{
int width = Mathf.RoundToInt(Screen.width / Scale);
int height = Mathf.RoundToInt(Screen.height / Scale);
texture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
texture.antiAliasing = 1;
cameraComponent = GetComponent<Camera>();
}
#if UNITY_EDITOR
private void Update()
{
if (EditorApplication.isPlaying) return;
CreateTexture();
}
#endif
private void OnPreRender()
{
cameraComponent.targetTexture = texture;
}
private void OnPostRender()
{
cameraComponent.targetTexture = null;
}
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
src.filterMode = FilterMode.Point;
Graphics.Blit(src, dest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment