Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created January 18, 2025 23:15
Show Gist options
  • Save baba-s/e66292049be383b43a301682c6564a1b to your computer and use it in GitHub Desktop.
Save baba-s/e66292049be383b43a301682c6564a1b to your computer and use it in GitHub Desktop.
[SerializeField] TextMeshPro tmpStandard;
[SerializeField] TextMeshProUGUI tmpUI;
public void SendTmpToRenderTexture()
{
Vector2 size = new Vector2(100f, 100f);
RenderTexture tex = RenderTexture.GetTemporary(100f, Mathf.RoundToInt(size.y * 200f), 24, RenderTextureFormat.ARGB32);
GL.PushMatrix(); // copy current camera matrix settings
GL.LoadIdentity();
Matrix4x4 proj = Matrix4x4.Ortho(-size.x / 2f, size.x / 2f, -size.x / 2f, size.y / 2f, -10f, 100f);
GL.LoadProjectionMatrix(proj);
RenderTexture currentActiveRT = RenderTexture.active;
Graphics.SetRenderTarget(tex);
GL.Clear(false, true, new Color(0f, 0f, 0f, 0f));
if (tmpUI)
{
tmpUI.materialForRendering.SetPass(0);
Graphics.DrawMeshNow(tmpUI.mesh, Matrix4x4.identity);
}
else
{
if (Application.isPlaying)
tmpStandard.renderer.material.SetPass(0);
else
tmpStandard.renderer.sharedMaterial.SetPass(0);
Graphics.DrawMeshNow(tmpStandard.mesh, Matrix4x4.identity);
}
//restore settings
GL.PopMatrix();
RenderTexture.active = currentActiveRT;
//assign texture to image
img.gameObject.SetActive(true);
imgRt.sizeDelta = thisRt.sizeDelta;
img.texture = tex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment