matrix
Sets the Matrix4x4 that the Unity Editor uses to draw Gizmos.
Read time 1 minuteLast updated 4 days ago
Definition
- Type: Property
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public static Matrix4x4 matrix { get; set; }
Remarks
The Gizmos.matrix stores the position, rotation and scale of the Gizmos. By default, Gizmos always uses world coordinates. The default Gizmos.matrix transforms the world coordinates using a default identity matrix. Transform.localToWorldMatrix changes local coordinate space to world space.
GameObjects often use local coordinates. Gizmos.matrix changes these local coordinates into world coordinates to allow Gizmos to use them. For example, a rotating object uses local coordinates. A transfer into world coordinates happens using Gizmos.matrix. To visualise the object, use Gizmos.DrawCube. See below.
To use the example to draw a red, semi-transparent, cube gizmo:
-
Place this example script on a Cylinder at the origin.
-
Select the Cylinder in the Hierarchy and then click thebutton.
Play -
Next, click thebutton. The gizmo should appear.
Scene
The cylinder will rotate in mode and be seen rotating in view.
PlaySceneExamples
using UnityEngine;public class GizmosExample : MonoBehaviour{ public float rotationSpeed = 50.0f; void OnDrawGizmosSelected() { Gizmos.color = new Color(0.75f, 0.0f, 0.0f, 0.75f); // Convert the local coordinate values into world // coordinates for the matrix transformation. Gizmos.matrix = transform.localToWorldMatrix; Gizmos.DrawCube(Vector3.zero, Vector3.one); } // Rotate the cube. void Update() { float zRot = rotationSpeed * Time.deltaTime; transform.Rotate(0.0f, 0.0f, zRot); }}