如何在实例化的相机上应用渲染纹理(作为目标纹理)?

时间:2017-04-20 22:54:12

标签: c# android unity3d unity5 instantiation

我有一个相机预制件,我在不同的位置实例化了4次,我想在其上添加渲染纹理(作为目标纹理),这样我就可以采用相同的纹理并应用于平面上以监控其中一个场景。请问是否不清楚。我正在尝试进行监视监视但不知道如何做到这一点,我对此感到困惑。请举例说明谢谢。

1 个答案:

答案 0 :(得分:1)

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

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

在AssetFolder中创建一些RenderTextures,而不是必须将它们链接到Camera脚本才能渲染它们。将此文件添加到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-Camera一点点,从数组中放入下一个RenderTexture并渲染图像。最后我禁用了相机。这是将所有4个RenderTextures放在四边形上的结果:Result

相关问题