Version: 2023.1

DrawingScope

struct in UnityEditor

切换到手册

描述

用于自动设置和恢复 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 恢复为进入作用域之前的值。您无需手动调用此方法。