Gizmos 和 Handles 类用于在 Scene 视图和 Game 视图绘制线条和形状以及交互式手柄和控件。这两个类共同提供了一种方法来扩展这些视图中显示的内容,并构建交互式工具以您喜欢的任何方式编辑项目。例如,您可以在游戏中围绕非玩家角色创建一个可拖动的圆圈辅助图标,代表它可听到或看到玩家的区域,而不必在 Inspector 中输入数字。
本页面提供了 Gizmos 和 Handles 类的简单概述。有关 Gizmos 和 Handles 类每个成员的完整文档和详尽参考,请参阅 Gizmos 和 Handles 脚本参考页面。
Gizmos 类允许您将线条、球体、立方体、图标、纹理和网格绘制到 Scene 视图中,在开发项目时用作调试、设置的辅助手段或工具。
例如,要在游戏对象周围绘制一个 10 个单位的黄色线框立方体,您可以使用以下代码:
using UnityEngine;
public class GizmosExample : MonoBehaviour
{
void OnDrawGizmosSelected()
{
// 在变换位置绘制一个黄色立方体
Gizmos.color = Color.yellow;
Gizmos.DrawWireCube(transform.position, new Vector3(10, 10, 10));
}
}
这是将立方体放置在一个方向光游戏对象上时的外观。
有关如何使用 Gizmos 的完整文档,请参阅 Gizmos 脚本参考页面。
Handles 类似于 Gizmos,但在交互性和操作方面提供了更多功能。Unity 本身提供的用于在 Scene 视图中操作项目的 3D 控件是 Gizmos 和 Handles 的组合。内置的 Handle GUI 有很多,如通过变换组件定位、缩放和旋转对象等熟悉的工具。不过,您可以自行定义 Handle GUI,以与自定义组件编辑器结合使用。此类 GUI 对于编辑以程序方式生成的场景内容、“不可见”项和相关对象的组(如路径点和位置标记)非常实用。
例如,以下是如何创建带有箭头手柄的圆弧区域,用于在 Scene 视图中修改 “shield area”:
using UnityEditor;
using UnityEngine;
using System.Collections;
//项目中应已包含了此类
public class WireArcExample : MonoBehaviour
{
public float shieldArea;
}
// 使用附加到圆盘的 ScaleValueHandle 创建一个 180 度的线弧,
// 允许您修改 WireArcExample 中的 "shieldArea" 的值
[CustomEditor(typeof(WireArcExample))]
public class DrawWireArc : Editor
{
void OnSceneGUI()
{
Handles.color = Color.red;
WireArcExample myObj = (WireArcExample)target;
Handles.DrawWireArc(myObj.transform.position, myObj.transform.up, -myObj.transform.right, 180, myObj.shieldArea);
myObj.shieldArea = (float)Handles.ScaleValueHandle(myObj.shieldArea, myObj.transform.position + myObj.transform.forward * myObj.shieldArea, myObj.transform.rotation, 1, Handles.ConeHandleCap, 1);
}
}
有关如何使用 Handles 的完整文档,请参阅 Handles 脚本参考页面。
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.