unity(角色查看)RTT将模型渲染到UI上面

最近想了想角色选择界面是用的模型直接摆上去的还是用的纹理,然后我就想到了用相机渲染到的纹理附上去康康,想不到还能这样:
具体的步骤:拿一张RenderTexture,将其赋值给模型渲染专用摄像机-渲染,再将其赋给ui的rawImage就可以了:
代码挺简单的:

	public Camera camera;
    public RawImage rwimg;
    public GameObject player;

    RenderTexture rentex;
    // Start is called before the first frame update
    void Start()
    {
        //创建一张纹理
        rentex = new RenderTexture(Screen.width, Screen.height, 0);
        //清除背景颜色
        camera.clearFlags = CameraClearFlags.SolidColor;
        //将临时纹理赋给渲染模型的摄像机
        camera.targetTexture = rentex;
        //渲染纹理
        camera.Render();
        //将纹理赋值给rawimage,(ui)
        rwimg.texture = rentex;

    }
    private float mox;
    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            mox -= Input.GetAxis("Mouse X");
            player.transform.rotation = Quaternion.AngleAxis(mox * 10, player.transform.up);
        }
    }

摄像机处理:
在这里插入图片描述
ui相关:
拿了一张image进行打底,上面一张Rawmage:
在这里插入图片描述
得到的效果:
在这里插入图片描述
最后,如果可以,求赞(每次看见有人赞都觉得身心愉悦+成就感)
ps:加一个出现的问题:
在手机端会出现模型渲染深度出了问题:导致:
在这里插入图片描述
需要将RenderTexture深度修改一下:(感谢大佬解惑)

 rentex.depth = 16;//或者24

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/QO_GQ/article/details/118724743