51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SetRenderTextureSize : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
[Range(1, 400)]
|
|
private int resolutionWidth;
|
|
private int oldResolutionWidth;
|
|
|
|
[SerializeField]
|
|
private RawImage renderImage;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
RecalculateTexture();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (resolutionWidth != oldResolutionWidth)
|
|
{
|
|
RecalculateTexture();
|
|
}
|
|
}
|
|
|
|
private void RecalculateTexture()
|
|
{
|
|
Camera camera = gameObject.GetComponent<Camera>();
|
|
if (camera.targetTexture != null) {
|
|
camera.targetTexture.Release();
|
|
}
|
|
|
|
float aspectRatio = (float)Screen.width / Screen.height;
|
|
int resolutionHeight = Mathf.RoundToInt(resolutionWidth / aspectRatio);
|
|
if (resolutionHeight < 1)
|
|
{
|
|
resolutionHeight = 1;
|
|
}
|
|
|
|
camera.targetTexture = new RenderTexture( resolutionWidth, resolutionHeight, 24 );
|
|
camera.targetTexture.filterMode = FilterMode.Point;
|
|
camera.targetTexture.wrapMode = TextureWrapMode.Clamp;
|
|
|
|
renderImage.texture = camera.targetTexture;
|
|
|
|
oldResolutionWidth = resolutionWidth;
|
|
}
|
|
}
|