Unity3D: Important classes - Gizmos and Handles

Recommended: Add NSDT Scene Editor to your 3D toolchain
3D Toolset: NSDT Jianshi Digital Twin

Important classes - Gizmos and Handles

The Gizmos and Handles classes are used to draw lines and shapes as well as interactive handles and controls in the Scene view and Game view. Together, these two classes provide a way to extend the content displayed in these views and build interactive tools to edit items in any way you like. For example, instead of having to type numbers in the Inspector, you can create a draggable circle gizmo around a non-player character in your game, representing the areas where it can hear or see the player.

This page provides a brief overview of the Gizmos and Handles classes. For complete documentation and extensive reference for each member of the Gizmos and Handles classes, see the Gizmos and Handles Script Reference page.

Gizmos

The Gizmos class allows you to draw lines, spheres, cubes, icons, textures, and grids into the Scene view, which can be used as debugging, setup aids, or tools while developing your project.

For example, to draw a 10-unit yellow wireframe cube around a game object, you would use the following code:

using UnityEngine;
public class GizmosExample : MonoBehaviour
{
    void OnDrawGizmosSelected()
    {
        // 在变换位置绘制一个黄色立方体
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireCube(transform.position, new Vector3(10, 10, 10));
    }
}

This is what the cube looks like when placed over a directional light game object.

A light GameObject with an extra script applied which draws a cube gizmo around its position

For complete documentation on how to use Gizmos, see the Gizmos Scripting Reference page.

Handles

Handles are similar to Gizmos but offer more functionality in terms of interactivity and manipulation. The 3D controls provided by Unity itself for manipulating items in the Scene view are a combination of Gizmos and Handles. The built-in Handle GUI has many familiar tools such as positioning, scaling, and rotating objects through transform components. However, you can define your own Handle GUI for use with a custom component editor. Such GUIs are useful for editing procedurally generated scene content, "invisible" items, and groups of related objects such as waypoints and position markers.

For example, here's how to create an arc area with arrow handles for modifying the "shield area" in the Scene view:

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);
    }
}

Examples of arc handles and scale handles

For complete documentation on how to use Handles, see the Handles Scripting Reference page.

Guess you like

Origin blog.csdn.net/jianshi2023/article/details/130697883