RigidbodyDisable.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace VoxelImporter
  6. {
  7. public class RigidbodyDisable : MonoBehaviour
  8. {
  9. public bool massSet = true;
  10. public float massRate = 0.1f;
  11. void Awake()
  12. {
  13. Action<Rigidbody> Set = (rb) =>
  14. {
  15. rb.isKinematic = true;
  16. if(massSet)
  17. {
  18. var meshFilter = rb.GetComponent<MeshFilter>();
  19. if(meshFilter != null && meshFilter.mesh)
  20. {
  21. var size = meshFilter.mesh.bounds.size.x * meshFilter.mesh.bounds.size.y * meshFilter.mesh.bounds.size.z;
  22. rb.mass = size * massRate;
  23. }
  24. }
  25. };
  26. {
  27. var rigidbody = GetComponent<Rigidbody>();
  28. if (rigidbody != null)
  29. {
  30. Set(rigidbody);
  31. }
  32. }
  33. {
  34. var rigidbodys = GetComponentsInChildren<Rigidbody>();
  35. for (int i = 0; i < rigidbodys.Length; i++)
  36. {
  37. Set(rigidbodys[i]);
  38. }
  39. }
  40. Destroy(this);
  41. }
  42. }
  43. }