LayoutElement3D.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace AutoLayout3D
  5. {
  6. [ExecuteInEditMode]
  7. public class LayoutElement3D : MonoBehaviour
  8. {
  9. public Vector3 center;
  10. public Vector3 size = Vector3.one;
  11. private void Reset()
  12. {
  13. MeshRenderer r = GetComponent<MeshRenderer>();
  14. if (r != null)
  15. {
  16. center = r.bounds.center - transform.position;
  17. center.x /= transform.lossyScale.x;
  18. center.y /= transform.lossyScale.y;
  19. center.z /= transform.lossyScale.z;
  20. size.x = r.bounds.size.x / transform.lossyScale.x;
  21. size.y = r.bounds.size.y / transform.lossyScale.y;
  22. size.z = r.bounds.size.z / transform.lossyScale.z;
  23. }
  24. }
  25. private void OnDrawGizmos()
  26. {
  27. Gizmos.color = new Color(0.5f, 0.5f, 1.0f, 1.0f);
  28. Gizmos.matrix = transform.localToWorldMatrix;
  29. Gizmos.DrawWireCube(center, size);
  30. }
  31. }
  32. }