When selecting a role for our game, we need to display the 3D model on the upper layer of the 2D GUI. The default 3D model is displayed under the 2D GUI, so we need to create another camera, set the target of the newly created camera to rendertexture, and then display the rendertexture to the GUI.
The Code is as follows:
Create camera:
_ Myobject = new gameobject (_ name, typeof (CAMERA), typeof (skybox ));
_ Mycamera = _ myobject. camera;
_ Mycamera. Depth = 2;
_ Mycamera. clearflags = cameraclearflags. Skybox;
_ Mycamera. backgroundcolor = color. Clear; // color. Black;
_ Mycamera. nearclipplane = 0.3f;
_ Mycamera. fieldofview = 60;
_ Mycamera. orthographicsize = 100366f;
Create rendertexture:
Private Static void inittexture (INT with, int height)
{
// _ Mytexture = new rendertexture (with, height, 2 );
_ Mytexture = rendertexture. gettemporary (with, height, 0, rendertextureformat. argb32 );
_ Mytexture. Name = "storytexture" + _ myobject. getinstanceid ();
_ Mytexture. ispoweroftwo = false;
_ Mycamera. targettexture = _ storytexture;
}
Draw rendertexture:
// Gui. drawtexture (New rect (0, 0, screen. Width, screen. Height ),
_ Mytexture, scalemode. stretchtofill );
Gui. drawtexture (New rect (0, 0, screen. Width, screen. Height ),
_ Mytexture, scalemode. scaletofit );
Note the following points on the mobile end:
1. When creating rendertexture, if the background on the new iPhone is black, find the solution at night as follows:
When setting upCamera
Object,RenderTexture
Is
Needed astargetTexture
For rendering. If you use a dynamically createdCamera
,
You have one by default whosedepth
Is 24. But it seems iOS devices only supportdepth
= 0
, So there are some problems.
To walk around, add a statement like this:
myCamera.targetTexture = RenderTexture.GetTemporary(myWidth, myHeight, 0, myFormat);
Where the third parameter isdepth
OfRenderTexture
.
2. When you paint rendertexture on the screen, if you select stretchtofill, the model on the mobile phone will be scaled. You need to select scaletofit.