using UnityEngine;
using UnityEngine.UI;
public class CameraRTT
{
public RenderTexture ShowTex { get { return m_renderTexture; } }
static int Layer_ModelRTT = 9;
static int s_count = 0;
private RenderTexture m_renderTexture;
private GameObject m_gameObject;
private Camera m_camera;
public CameraRTT(int size = 512)
{
s_count++;
m_renderTexture = new RenderTexture(size, size, 24, RenderTextureFormat.ARGB32);
m_renderTexture.name = "RTTTexture" + s_count;
m_gameObject = new GameObject("RTTGameObject" + s_count);
m_gameObject.layer = Layer_ModelRTT;
m_gameObject.transform.localPosition = new Vector3(s_count * 100f, 0f, 0f);
m_gameObject.SetActive(false);
m_camera = m_gameObject.AddComponent<Camera>();
m_camera.orthographic = true;
m_camera.orthographicSize = 1;
m_camera.cullingMask = 1 << Layer_ModelRTT;
m_camera.clearFlags = CameraClearFlags.SolidColor;
}
public void ShowSingleModel(GameObject obj, RawImage uiTexture, Vector3 pos, Vector3 rot, Vector3 scale)
{
if (m_gameObject == null || m_camera == null) return;
obj.transform.parent = m_gameObject.transform;
obj.layer = Layer_ModelRTT;
obj.transform.localPosition = pos;
obj.transform.eulerAngles = rot;
obj.transform.localScale = scale;
m_camera.targetTexture = m_renderTexture;
uiTexture.texture = m_renderTexture;
m_gameObject.SetActive(true);
}
}
网友评论