unity3d设置3D模型显示在2D背景之前(多个相机分层显示)(转)

解决步骤:

1.添加一个摄像机,命名为BackgroundCamera,然后在Layer添加一个background层。并且将plane拖放到改相机节点下。 然后将BackgroundCamera和Plane都置于background层,修改ClearFlags未Depthonly深度渲染,并且设置 CullingMask为只看到background层,还有设置Depth为-1,说明背景层是最深,其他model所在的相机的Depth是 0,NGUI的是1,这里层次关系就是NGUI在最前面,model层其次,然后是背景层,这样确保3D模型在背景的前面!

2.同样的操作,将MainCamera设置一下,注意的就是,将MainCamera的CullingMask不能包含background层,也就是让它看不到背景层,不然还是会出现背景遮挡住模型的情况。

3.在plane上添加一个脚本,用于动态在plane上加载贴图

01.using UnityEngine;
02.using System.Collections;
03.using System.Collections.Generic;
04. 
05.public class AddBackground : MonoBehaviour
06.{
07.public Texture2D mtexture;
08. 
09.void Awake()
10.{
11.//        Texture2D mtxture = new Texture2D(Screen.width, Screen.height);
12.//        renderer.material.mainTexture = mtxture;
13.//        mtxture.Apply();
14. 
15.gameObject.AddComponent<guitexture>();
16.}
17. 
18.// Use this for initialization
19.void Start()
20.{
21.guiTexture.texture = mtexture;
22.transform.localScale = new Vector3(0, 0, 0);
23.transform.localPosition = new Vector3(0, 0, 25);
24.guiTexture.pixelInset = new Rect(65, 0, Screen.width, Screen.height);
25.}
26.}</guitexture>

上面Start方法中设置position的z轴属性是原来尝试的,想通过z轴来进行深度的修改后来发现行不通,还是层的关系影响渲染的深度。

4.效果图

原文地址:https://www.cnblogs.com/vonly/p/4094506.html