用于自动设置和恢复 Handles.color 和/或 Handles.matrix 的可处置 Helper 结构。
此结构允许您暂时设置代码块内的 Handles.color 和/或 Handles.matrix 的值,并在退出作用域时自动将它们恢复为之前的值。
using UnityEditor; using UnityEngine;
// a custom editor that draws a labeled circle around the selected MeshRenderer in the Scene view [CustomEditor(typeof(MeshRenderer))] public class MeshRendererEditor : Editor { protected virtual void OnSceneGUI() { MeshRenderer meshRenderer = (MeshRenderer)target;
// get an orientation pointing from the selected object to the camera Vector3 cameraToTarget = Camera.current.transform.position - meshRenderer.transform.position; Quaternion billboardOrientation = Quaternion.LookRotation(cameraToTarget, Camera.current.transform.up);
// set the handle matrix to the target's position, oriented facing the camera Matrix4x4 matrix = Matrix4x4.TRS(meshRenderer.transform.position, billboardOrientation, Vector3.one); using (new Handles.DrawingScope(Color.magenta, matrix)) { // draw a magenta circle around the selected object with a label at the top Vector3 size = meshRenderer.bounds.size; float radius = Mathf.Max(size.x, size.y, size.z); Handles.DrawWireArc(Vector3.zero, Vector3.forward, Vector3.right, 360f, radius); Handles.Label(Vector3.up * radius, meshRenderer.name); } } }
originalColor | 创建此 DrawingScope 时的 Handles.color 值。 |
originalMatrix | 创建此 DrawingScope 时的 Handles.matrix 值。 |
Handles.DrawingScope | 创建一个新的 DrawingScope 并将 Handles.color 和/或 Handles.matrix 设置为指定的值。 |
Dispose | 在退出作用域时,自动将 Handles.color 和 Handles.matrix 恢复为进入作用域之前的值。您无需手动调用此方法。 |
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.