Unity中Collider和刚体Collider性能对比
测试方式:
每个对象做大范围正弦移动,创建1000-5000个对象,保证场景分割树的实时更新,并测试帧率
测试脚本:
移动脚本:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using UnityEngine; using System.Collections; public class Test1 : MonoBehaviour { public Rigidbody rigidbodyObj; void FixedUpdate() { switch (Random.Range(0, 3)) { case 0: transform.position = Vector3.forward * Mathf.Sin(Time.time * 180) * 180; //rigidbodyObj.position = Vector3.forward * Mathf.Sin(Time.time * 180) * 180; break; case 1: transform.position = Vector3.left * Mathf.Sin(Time.time * 180) * 180; //rigidbodyObj.position = Vector3.left * Mathf.Sin(Time.time * 180) * 180; break; case 2: transform.position = Vector3.right * Mathf.Sin(Time.time * 180) * 180; //rigidbodyObj.position = Vector3.right * Mathf.Sin(Time.time * 180) * 180; break; } } }
生成器脚本:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using UnityEngine; using System.Collections; public class Test2 : MonoBehaviour { public int count = 10000; public GameObject template; void Awake() { for (int i = 0; i < count; i++) { var go = Instantiate(template); go.transform.position = Random.insideUnitSphere * 10f; } } }
测试结果:
i5的配置下
500个动力学打开的刚体Collider已经小于20fps,使用Rigidbody.position移动和使用Transform.position移动差别不大。而且500个打开了重力的刚体Collider仅有3fps
1万个非刚体Collider可以达到30-35fps,并且和是否开关Trigger无关。由于非刚体Collider是被动触发,我还测试了下没有Collider的情况,似乎帧率是快了1-2fps并且相对稳定,也可能是错觉。