123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class JointsHighlighter : MonoBehaviour
- {
- private static List<JointsHighlighter> _jointsHighlighters = new List<JointsHighlighter>();
- public GameObject HighCube;
- private MeshRenderer _meshRenderer;
- public HingeJoint _hingeJoint;
- public ConfigurableJoint _confJoint;
- public Rigidbody _rigidbody;
- public float AngleLimitMin;
- public float AngleLimitMax;
- public void Awake()
- {
- _rigidbody = GetComponent<Rigidbody>();
- if(_rigidbody)
- {
- _rigidbody.centerOfMass = _rigidbody.transform.localPosition;
- }
-
- _hingeJoint = GetComponent<HingeJoint>();
- _confJoint = GetComponent<ConfigurableJoint>();
- _meshRenderer = HighCube.GetComponent<MeshRenderer>();
- _jointsHighlighters.Add(this);
- _meshRenderer.enabled =false;
- var collider = HighCube.GetComponent<Collider>();
- if(collider.GetComponent<ColliderProxy>()==null)
- {
- var cp = collider.gameObject.AddComponent<ColliderProxy>();
- cp.HingeJoint = _hingeJoint;
- cp.ConfJoint = _confJoint;
- }
- else
- {
- var cp = collider.gameObject.GetComponent<ColliderProxy>();
- cp.HingeJoint = _hingeJoint;
- cp.ConfJoint = _confJoint;
- }
- }
- public void OnDestroy()
- {
- _jointsHighlighters.Remove(this);
- }
- public void Highlighted()
- {
- //_rigidbody.constraints = RigidbodyConstraints.None;
- _meshRenderer.enabled = true;
- var other = _jointsHighlighters.Where(o => o != this).ToList();
- foreach (var item in other)
- {
- item._meshRenderer.enabled = false;
- //_rigidbody.constraints = RigidbodyConstraints.FreezeAll;
- }
- }
- public class ChildObject
- {
- public Transform Transform;
- public Quaternion Rotation;
- public Vector3 Position;
- public Rigidbody Rigidbody;
- public ChildObject (GameObject go)
- {
- Transform = go.transform;
- Rotation = go.transform.localRotation;
- Position = go.transform.localPosition;
- Rigidbody = go.GetComponent<Rigidbody>();
- }
- public void Update()
- {
- Transform.localRotation = Rotation;
- Transform.localPosition = Position;
- //if(Rigidbody)
- //{
- // Rigidbody.angularVelocity = Vector3.zero;
- // Rigidbody.isKinematic = false;
- //}
-
- }
- public void Reset()
- {
- if(Rigidbody)
- {
- Rigidbody.isKinematic = true;
- }
- }
- }
- public List<ChildObject> GetChildren ()
- {
- var result = new List<ChildObject>();
- var trs = GetComponentsInChildren<Transform>();
-
- foreach (var tr in trs)
- {
- if(tr!= transform)
- {
- if(tr.GetComponent<HingeJoint>()!= null)
- {
- result.Add(new ChildObject(tr.gameObject));
- }
-
- }
- }
- return result;
- }
- }
|