Unity中Collider和刚体Collider性能对比

时间:2022-10-04 17:17:02

测试方式:

每个对象做大范围正弦移动,创建1000-5000个对象,保证场景分割树的实时更新,并测试帧率

测试脚本:

移动脚本:

using UnityEngine;
using System.Collections; public class Test1 : MonoBehaviour
{
public Rigidbody rigidbodyObj; void FixedUpdate()
{
switch (Random.Range(, ))
{
case : transform.position = Vector3.forward * Mathf.Sin(Time.time * ) * ;
//rigidbodyObj.position = Vector3.forward * Mathf.Sin(Time.time * 180) * 180; break; case : transform.position = Vector3.left * Mathf.Sin(Time.time * ) * ;
//rigidbodyObj.position = Vector3.left * Mathf.Sin(Time.time * 180) * 180; break; case : transform.position = Vector3.right * Mathf.Sin(Time.time * ) * ;
//rigidbodyObj.position = Vector3.right * Mathf.Sin(Time.time * 180) * 180; break;
}
}
}

生成器脚本:

using UnityEngine;
using System.Collections; public class Test2 : MonoBehaviour
{
public int count = ;
public GameObject template; void Awake()
{
for (int i = ; 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并且相对稳定,也可能是错觉。