Recently, I was thinking about whether the character selection interface uses a model to be placed directly or a texture. Then I thought of using the texture rendered by the camera to attach it to Kangkang. I couldn’t think of another way like this: Specific steps: Take a RenderTexture
. Just assign it to the camera-rendering dedicated for model rendering, and then assign it to the rawImage of ui:
the code is quite simple:
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);
}
}
Camera processing:
UI related:
I took an image for backgrounding, and the above one is Rawmage:
The effect obtained:
Finally, if you can, please like it (every time I see someone like it, I feel physically and mentally happy + a sense of accomplishment)
ps: Add an appearance Problem:
There will be a problem with the model rendering depth on the mobile phone: resulting in:
the RenderTexture depth needs to be modified: (Thanks to the boss for clarifying the confusion)
rentex.depth = 16;//或者24