Unity3d 3D GUI display 3D Model

Source: Internet
Author: User

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 upCameraObject,RenderTextureIs
Needed astargetTextureFor rendering. If you use a dynamically createdCamera,
You have one by default whosedepthIs 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 isdepthOfRenderTexture.

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.