UnityEngine.Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);

for (int i = 0; i < selObjs.Length; i++)
{
  UnityEngine.Object selObj = selObjs[i];
  string path = AssetDatabase.GetAssetPath(selObj);
  if(path.EndsWith(".prefab"))
  {
    var targetComponent = AssetDatabase.LoadAssetAtPath<GameObject>(path);
    if (targetComponent.GetComponent<BoxCollider>())
      return;
    BoxCollider collider = targetComponent.AddComponent<BoxCollider>();
    SkinnedMeshRenderer meshReader = targetComponent.GetComponentInChildren<SkinnedMeshRenderer>();
    collider.size = new Vector3(meshReader.bounds.extents.x * 2, meshReader.bounds.extents.z * 2, meshReader.bounds.extents.z * 2);
    collider.center = new Vector3(meshReader.bounds.center.x , meshReader.bounds.center.y, meshReader.bounds.center.z );
  }
}

 

注:通过获取meshReader.bounds 的数据来生成BoxCollider 的大小和位置

posted on 2020-11-18 16:44  蚁丘  阅读(257)  评论(0编辑  收藏  举报