Gizmos.matrix 存储 Gizmos 的位置、旋转和缩放。默认情况下,Gizmos 始终使用世界坐标。默认的 Gizmos.matrix 使用默认的单位矩阵来转换世界坐标。Transform.localToWorldMatrix 将本地坐标空间更改为世界空间。
GameObject 通常使用本地坐标。Gizmos.matrix 将这些本地坐标更改为世界坐标以便 Gizmos 使用它们。例如,旋转对象使用本地坐标。使用 Gizmos.matrix 即可转换为世界坐标。要将对象可视化,请使用 Gizmos.DrawCube。请参阅下文。
要使用该示例绘制红色、半透明的立方体辅助图标,请执行以下操作:
1.将此示例脚本放在位于原点的圆柱体 (Cylinder) 上。
2.在 Hierarchy 中选择该圆柱体,然后单击 Play
按钮。
3.接着,单击 Scene
按钮。此时应该出现辅助图标。\
圆柱体将以 Play
模式旋转,并在 Scene
视图中旋转。
using System.Collections; using System.Collections.Generic; using UnityEngine;
// Gizmos.matrix example
public class Example : MonoBehaviour { // Allow the speed of rotation to be changed. 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 cylinder. void Update() { float zRot = rotationSpeed * Time.deltaTime; transform.Rotate(0.0f, 0.0f, zRot); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.