网站建设哪家好建议兴田德润,全案设计公司名字,网站域名要实名认证吗,珠海市住房和城乡建设局网站Unity填坑-CullingGroup的运用
可以使用CullingGroup动态剔除一些对性能有极大影响的脚本、及渲染的进行。 提示#xff1a;写完文章后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 文章目录 Unity填坑-CullingGroup的运用前言二、示例代码如下总结…Unity填坑-CullingGroup的运用
可以使用CullingGroup动态剔除一些对性能有极大影响的脚本、及渲染的进行。 提示写完文章后目录可以自动生成如何生成可参考右边的帮助文档 文章目录 Unity填坑-CullingGroup的运用前言二、示例代码如下总结 前言
CullingGroup可以提供对场景中对象的距离追踪并提供回调效果。 二、示例代码如下
public class CullingGroupTest : MonoBehaviour
{public ListGameObject Targets;public CullingGroup group;public BoundingSphere[] spheres;public GameObject Player;// Start is called before the first frame updatevoid Start(){group new CullingGroup();group.targetCamera Camera.main;group.SetDistanceReferencePoint(Player.transform);float[] distances new float[5];for (int i 0; i 5; i){distances[i] 5f*i;}group.SetBoundingDistances(distances);spheres new BoundingSphere[Targets.Count];for (int i 0; i Targets.Count; i){spheres[i] new BoundingSphere(Targets[i].transform.position, 1f);}group.SetBoundingSpheres(spheres);group.SetBoundingSphereCount(Targets.Count);group.onStateChanged StateChangedMethod;}// Update is called once per framevoid Update(){for (int i 0; i Targets.Count; i){spheres[i].position Targets[i].transform.position;}}private void OnDestroy(){group.Dispose();}private void StateChangedMethod(CullingGroupEvent evt){if (evt.hasBecomeVisible){Debug.LogFormat(Sphere {0} has become visible!, evt.index);Targets[evt.index].SetActive(true);}if (evt.hasBecomeInvisible){Debug.LogFormat(Sphere {0} has become invisible!, evt.index);Targets[evt.index].SetActive(false);}Debug.LogFormat(Sphere {0} has become Change!, evt.index);}
}使用起来很简单如果只想对这些物体针对摄像机的可见性进行回调那么去掉group.SetDistanceReferencePoint(Player.transform);以及group.SetBoundingDistances(distances);这两行代码即可。 效果就是:当追踪的物体通过球体中心与半径判定是否在摄像机视野内不在摄像机视角内时触发回调。
如果想设定一个对摄像机的距离范围来进行回调group.SetBoundingDistances(distances)保留这行代码即可达成目标可以设置不同的范围区域来进行不同处理。
如果想要让这些追踪物体对另外一个目标进行追踪则可以保留group.SetDistanceReferencePoint(Player.transform)代码可以设置trasform会自动实时改变位置也可以传入Vector3的定点。这样可以针对距离这个目标进行回调。 总结
当然完全可以使用自己的方法去每帧进行距离计算也可以到达相同的效果。不过利用CullingGroup进行摄像机的裁减回调还是挺好用的。比如有些纯表现的场景物体直接SetVisable为false可以提升不少性能。