JointsHighlighter.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. public class JointsHighlighter : MonoBehaviour
  6. {
  7. private static List<JointsHighlighter> _jointsHighlighters = new List<JointsHighlighter>();
  8. public GameObject HighCube;
  9. private MeshRenderer _meshRenderer;
  10. public HingeJoint _hingeJoint;
  11. public ConfigurableJoint _confJoint;
  12. public Rigidbody _rigidbody;
  13. public float AngleLimitMin;
  14. public float AngleLimitMax;
  15. public void Awake()
  16. {
  17. _rigidbody = GetComponent<Rigidbody>();
  18. if(_rigidbody)
  19. {
  20. _rigidbody.centerOfMass = _rigidbody.transform.localPosition;
  21. }
  22. _hingeJoint = GetComponent<HingeJoint>();
  23. _confJoint = GetComponent<ConfigurableJoint>();
  24. _meshRenderer = HighCube.GetComponent<MeshRenderer>();
  25. _jointsHighlighters.Add(this);
  26. _meshRenderer.enabled =false;
  27. var collider = HighCube.GetComponent<Collider>();
  28. if(collider.GetComponent<ColliderProxy>()==null)
  29. {
  30. var cp = collider.gameObject.AddComponent<ColliderProxy>();
  31. cp.HingeJoint = _hingeJoint;
  32. cp.ConfJoint = _confJoint;
  33. }
  34. else
  35. {
  36. var cp = collider.gameObject.GetComponent<ColliderProxy>();
  37. cp.HingeJoint = _hingeJoint;
  38. cp.ConfJoint = _confJoint;
  39. }
  40. }
  41. public void OnDestroy()
  42. {
  43. _jointsHighlighters.Remove(this);
  44. }
  45. public void Highlighted()
  46. {
  47. //_rigidbody.constraints = RigidbodyConstraints.None;
  48. _meshRenderer.enabled = true;
  49. var other = _jointsHighlighters.Where(o => o != this).ToList();
  50. foreach (var item in other)
  51. {
  52. item._meshRenderer.enabled = false;
  53. //_rigidbody.constraints = RigidbodyConstraints.FreezeAll;
  54. }
  55. }
  56. public class ChildObject
  57. {
  58. public Transform Transform;
  59. public Quaternion Rotation;
  60. public Vector3 Position;
  61. public Rigidbody Rigidbody;
  62. public ChildObject (GameObject go)
  63. {
  64. Transform = go.transform;
  65. Rotation = go.transform.localRotation;
  66. Position = go.transform.localPosition;
  67. Rigidbody = go.GetComponent<Rigidbody>();
  68. }
  69. public void Update()
  70. {
  71. Transform.localRotation = Rotation;
  72. Transform.localPosition = Position;
  73. //if(Rigidbody)
  74. //{
  75. // Rigidbody.angularVelocity = Vector3.zero;
  76. // Rigidbody.isKinematic = false;
  77. //}
  78. }
  79. public void Reset()
  80. {
  81. if(Rigidbody)
  82. {
  83. Rigidbody.isKinematic = true;
  84. }
  85. }
  86. }
  87. public List<ChildObject> GetChildren ()
  88. {
  89. var result = new List<ChildObject>();
  90. var trs = GetComponentsInChildren<Transform>();
  91. foreach (var tr in trs)
  92. {
  93. if(tr!= transform)
  94. {
  95. if(tr.GetComponent<HingeJoint>()!= null)
  96. {
  97. result.Add(new ChildObject(tr.gameObject));
  98. }
  99. }
  100. }
  101. return result;
  102. }
  103. }