DrawCube(Vector3, Vector3)
Draws a solid box at center with size in the Scene view for visualization and debugging purposes.
Read time 2 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public static void DrawCube(Vector3 center, Vector3 size)
Parameters
Remarks
DrawCubeDrawCubeDrawCubeThe cube is drawn using the current Gizmos color, which can be set using Gizmos.color before calling . The cube is drawn using the current Gizmos matrix, which can be set using Gizmos.matrix before calling .
DrawCubeDrawCubeTypically, you'd use this method within MonoBehaviour.OnDrawGizmos() or MonoBehaviour.OnDrawGizmosSelected() functions in your MonoBehaviour scripts, or within a method with the DrawGizmo attribute. Like all Gizmos, it's an Editor-only tool and should not be used for game functionality. This method has no effect in the GameView or at runtime. If you want to draw similar shapes at runtime, refer to Debug.DrawLine.
The cube respects the 3D space and perspective of the scene, including depth and occlusion by other objects. It also respects the alpha channel of the Gizmos color, allowing for semi-transparent cubes as well as the transformation of Gizmos.matrix.
using UnityEngine;public class CubeGizmoExample : MonoBehaviour{ public Vector3 size = Vector3.one; [Range(0f, 1f)] public float alpha = 0.5f; private void OnDrawGizmos() { // Set the color with custom alpha. Gizmos.color = new Color(0f, 1f, 0f, alpha); // Green with custom alpha // Draw the cube. Gizmos.DrawCube(transform.position, size); // Draw a wire cube outline. Gizmos.color = Color.white; Gizmos.DrawWireCube(transform.position, size); }}
Additional Resources: MonoBehaviour.OnDrawGizmos(), MonoBehaviour.OnDrawGizmosSelected(), DrawGizmo, Gizmos.color, Gizmos.matrix.