首页 > 解决方案 > 将渲染纹理分配给相机 - Unity

问题描述

嗨,我正在学习在 Unity 中制作音频可视化器。我面临的问题是我有一个按钮,单击时会在运行时以编程方式加载另一个场景,(称之为场景 Phyllo)Phyllo 有一个摄像头。现在还有一个“主要”场景,称之为 UI_v15。我已将 Phyllo 的相机视图放入渲染纹理中。在UI_v15中,我有一个Cube GameObject,我想将 Phyllo 的渲染纹理分配给cube,以便在按下上述按钮时,Cube可以显示来自 Phyllo 的相机视图。但这是我失败的地方......

千叶

相反,我只是从 Phyllo 那里得到这样的视图我只是从 Phyllo 那里得到完整的视图)。相反,应该发生的是视图应该渲染到我之前谈到的 Cube 中。

当程序运行时,targetTextureforPhyllo的相机没有分配纹理,它应该。如红色部分所示

这是按下按钮(在 UI_v15 内)时调用的方法。

public void SelectVisualiser()
{

    string sceneName = GetComponent<Button>().GetComponentInChildren<Text>().text;
    print("scene name " + sceneName);
    SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
    VisualiserManager.instance.LoadVisualiser(sceneName);
}
              
public void LoadVisualiser(string name)
{
    //the script PhylloCamera is attached to PhylloCamera in the Phyllo scene.
  
    Camera cam = PhylloCameraRef.instance.phylloCam;
    Material mat = new Material(Shader.Find("Standard"));
    RenderTexture rt = CreateTexture();
    mat.SetTexture("_MainTex", rt);
    cam.targetTexture = rt;
    display.GetComponent<MeshRenderer>().materials[0] = mat;

}

PhylloCameraRef 类附加到场景 Phyllo,可以在运行时加载。

    public class PhylloCameraRef : MonoBehaviour
{
    public Camera phylloCam;
    public static PhylloCameraRef instance;

    
    void Start()
    {
        if (instance is null)
        {
            
            instance = this;
            //getting camera
            phylloCam = gameObject.GetComponent<Camera>();
        }
    }
}

有谁知道为什么 Phyllo 的相机没有渲染到 Cube 中?谢谢。

编辑:下面是 CreateTexture() 的代码

public RenderTexture CreateTexture()
{
    RenderTexture rt = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
    rt.Create();
    return rt;
}

由于某些未知的原因,我忘了添加,UIv_15 中的相机 targetTexture 被分配了渲染纹理。立方体是一个实际的 3D 游戏对象。

标签: unity3d

解决方案


推荐阅读