本文共 1395 字,大约阅读时间需要 4 分钟。
有时候,需要在一个UIScrollView中显示一个带有3D模型或者粒子特效的列表
NGUI的UIScrollView无法裁剪3D模型和粒子特效,所以想到一个办法就是使用摄像机来裁剪
最重要的是摄像机的裁剪区域的设置,需要使用到NGUI的UIViewport脚本
预设上需要事先挂好摄像机,并创建一个 topLeft 和一个 bottomRight 作为裁剪区域的左上角和右下角的定位
结构如下:
UI Root
|__UICamera
|___myPanel
|___myUiCamera
|___topLeft
|___bottomRight
|___scrollviewPanel
|___uiGrid
|___item0
|___obj3d
|___item1
|___obj3d
|___item2
|___obj3d
摄像机 myUiCamera 的ClearFlags设置为Depth only,Projection设置为Orthographic(正交)
Culling Mask可以用代码动态设置,假设obj_3d的layer我们设置成24,代码如下
int layer = 24;NGUITools.SetLayer(obj3d, layer);myUiCamera.cullingMask = 2^layer; //相当于1左移24位
接下来是设置裁剪区,接口
public static void SetCullingCamViewport(Camera sourceCam, Camera targetCam, Transform topLeftTrans, Transform bottomRightTrans, GameObject dragRoot){ Vector3 camPos = (topLeftTrans.localPosition + bottomRightTrans.localPosition)*0.5f; targetCam.transform.localPosition = camPos; UIViewport viewport = targetCam.gameObject.AddComponent(); viewport.topLeft = topLeftTrans; viewport.bottomRight= bottomRightTrans; viewport.sourceCamera = sourceCam; targetCam.gameObject.AddComponent (); UIDraggableCamera dragbleCam = targetCam.gameObject.AddComponent (); dragbleCam.rootForBounds = dragRoot;}
调用:
//假设你的NGUI主摄像机是GlobalObj.NGUICameraSetCullingCamViewport(GlobalObj.NGUICamera, myUiCamera, topLeft, bottomRight, scrollviewPanel);
完毕
转载地址:http://jdkvf.baihongyu.com/