2017-04-20 51 views
1

我有一个摄像机预制,我在不同的位置实例化4次,我想在它上面添加渲染纹理(作为目标纹理),所以我可以采用相同的纹理,并应用在一个场景中进行监视。请问是否不清楚。我试图做一个监视监视,但不知道如何做到这一点,我坚持这一点。请举例先谢谢。如何在实例化的相机上应用渲染纹理(作为目标纹理)?

回答

1

我认为统一手册很好地解释了它https://docs.unity3d.com/Manual/class-RenderTexture.html

更具体一点,这里有一个可能的实现:

在AssetFolder创建一些RenderTextures,比你必须将它们链接到您的相机脚本,让他们呈现。将此文件添加到您的TextureRender-Camera。

using System.Collections; 
using UnityEngine; 

public class Camera2Texture : MonoBehaviour { 

public RenderTexture[] renderTextures; 
private Camera cam; 

private void Awake() 
{ 
    cam = GetComponent<Camera>(); 
} 

private void Start() 
{ 
    StartCoroutine(RenderTexturesCoroutine()); 
} 

IEnumerator RenderTexturesCoroutine() 
{ 
    for (int i = 0; i < renderTextures.Length; i++) 
    { 
     // just move the camera a little bit and focus the center of the scene 
     this.transform.position += Vector3.left * 2 * i; 
     cam.transform.LookAt(Vector3.zero); 

     cam.targetTexture = renderTextures[i]; 
     yield return new WaitForSeconds(1f); 
     cam.Render(); 
    } 

    cam.targetTexture = null; 
    this.gameObject.SetActive(false); 
} 
} 

我开始一个协程,其移动我的TextureRender-相机一点点每一秒,放入下渲染纹理从阵列和渲染图像。最后,我禁用相机。这是当您将所有4个RenderTextures放在四边形上时的结果:Result

+0

感谢您的回复。相机从预制中实例化。如何将渲染纹理渲染到实例化的相机并应用于视图上。 – RingR89

+0

您只需在资产窗口中将您的RenderTexture添加到相机预制件(使用上面的脚本)即可。当你实例化预制件时,它会引用你的RenderTextur资产。要显示RenderTextures,只需添加一个Quad(或其他),然后拖动RenderTexture就可以更改其材质。一旦你的相机已经渲染你看到四边形纹理。 –