MaximilianPs

ObjectSpawner

Jun 12th, 2024 (edited)
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | Source Code | 0 0
  1.    [CanEditMultipleObjects]
  2.    public class ObjectSpawner : MonoBehaviour
  3.    {
  4.        [SerializeField] private GameObject[] objects;
  5.        [Space]
  6.        [SerializeField] private Color gizmoColor = new Color(193, 96, 36, 185);
  7.  
  8.        private float averangeSize;
  9.  
  10.        private void Awake()
  11.        {
  12.            Instantiate(objects[Random.Range(0, objects.Length - 1)], transform);
  13.            CalculateAverageSize();
  14.        }
  15.  
  16.        private void OnDrawGizmos()
  17.        {
  18.            if (objects != null && objects.Length > 0) CalculateAverageSize();
  19.  
  20.            DrawGizmo();
  21.        }
  22.  
  23.        private void OnDrawGizmosSelected()
  24.        {
  25.            if (objects != null && objects.Length > 0)  DrawGizmo();
  26.        }
  27.  
  28.        private void DrawGizmo()
  29.        {
  30.            float averageHeight = CalculateAverageSizeY();
  31.            Vector3 gizmoPositionLocal = new Vector3(0, averageHeight / 2, 0);
  32.  
  33.            Gizmos.matrix = transform.localToWorldMatrix;
  34.  
  35.            Gizmos.color = gizmoColor;
  36.            Gizmos.DrawCube(gizmoPositionLocal, DrawAverageSizeGizmo());
  37.            Gizmos.color = gizmoColor * 2;
  38.            Gizmos.DrawWireCube(gizmoPositionLocal, DrawAverageSizeGizmo());
  39.        }
  40.  
  41.        public float CalculateAverageSize()
  42.        {
  43.            float volumeSum = 0f;
  44.            foreach (GameObject obj in objects)
  45.            {
  46.                Vector3 objSize = obj.transform.lossyScale;
  47.                float objVolume = objSize.x * objSize.y * objSize.z;
  48.                volumeSum += objVolume;
  49.            }
  50.            averangeSize = volumeSum / objects.Length;
  51.            return averangeSize;
  52.        }
  53.  
  54.        public Vector3 DrawAverageSizeGizmo()
  55.        {
  56.            float averageSizeX = CalculateAverageSizeX();
  57.            float averageSizeY = CalculateAverageSizeY();
  58.            float averageSizeZ = CalculateAverageSizeZ();
  59.  
  60.            // Create a new Vector3 with the average size as the length of the vector
  61.            return new Vector3(averageSizeX, averageSizeY, averageSizeZ);
  62.        }
  63.  
  64.        private float CalculateAverageSizeX()
  65.        {
  66.            float sum = 0f;
  67.            foreach (GameObject obj in objects)
  68.            {
  69.                Bounds bounds = obj.GetComponent<Renderer>().bounds;
  70.                sum += bounds.extents.x * 2; // Get the width of the bounding box
  71.            }
  72.            return sum / objects.Length;
  73.        }
  74.  
  75.        private float CalculateAverageSizeY()
  76.        {
  77.            float sum = 0f;
  78.            foreach (GameObject obj in objects)
  79.            {
  80.                Bounds bounds = obj.GetComponent<Renderer>().bounds;
  81.                sum += bounds.extents.y * 2; // Get the height of the bounding box
  82.            }
  83.            return sum / objects.Length;
  84.        }
  85.  
  86.        private float CalculateAverageSizeZ()
  87.        {
  88.            float sum = 0f;
  89.            foreach (GameObject obj in objects)
  90.            {
  91.                Bounds bounds = obj.GetComponent<Renderer>().bounds;
  92.                sum += bounds.extents.z * 2; // Get the depth of the bounding box
  93.            }
  94.            return sum / objects.Length;
  95.        }
  96.    }
Advertisement
Add Comment
Please, Sign In to add comment