DrawGizmo
// 摘要:
// Defines when the gizmo should be invoked for drawing.
// 定义Gizmo什么时候会被唤醒去绘画
// 参数:
// gizmo:
// Flags to denote when the gizmo should be drawn.
public DrawGizmo(GizmoType gizmo);
// 摘要:
// Same as above. drawnGizmoType determines of what type the object we are drawing
// the gizmo of has to be.
//
// 参数:
// gizmo:
// Flags to denote when the gizmo should be drawn.
//
// drawnGizmoType:
// Type of object for which the gizmo should be drawn.
public DrawGizmo(GizmoType gizmo, Type drawnGizmoType);
GizmoType
//
// 摘要:
// Determines how a gizmo is drawn or picked in the Unity editor.
public enum GizmoType
{
NotSelected = -127,
SelectedOrChild = -127,
//
// 摘要:
// The gizmo can be picked in the editor.
Pickable = 1,
//
// 摘要:
// Draw the gizmo if it is not selected and also no parent/ancestor is selected.
NotInSelectionHierarchy = 2,
//
// 摘要:
// Draw the gizmo if it is selected.
Selected = 4,
//
// 摘要:
// Draw the gizmo if it is active (shown in the inspector).
Active = 8,
//
// 摘要:
// Draw the gizmo if it is selected or it is a child/descendent of the selected.
InSelectionHierarchy = 16,
//
// 摘要:
// Draw the gizmo if it is not selected.
NonSelected = 32
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class InspectorMethodTest : MonoBehaviour
{
private Camera mainCamera;
//实现场景中一直显示主摄像机的视野范围
private void OnDrawGizmos()
{
if (mainCamera == null)
mainCamera = Camera.main;
Gizmos.color = Color.green;
//设置gizmos的矩阵
Gizmos.matrix = Matrix4x4.TRS(mainCamera.transform.position, mainCamera.transform.rotation, Vector3.one);
Gizmos.DrawFrustum(Vector3.zero, mainCamera.fieldOfView, mainCamera.farClipPlane, mainCamera.nearClipPlane, mainCamera.aspect);
}
}
public class MyScriptGizmoDrawer
{
//用于Gizmos渲染,将逻辑与调试代码分离
//DrawGizmo 属性可用于为任意 Component 提供辅助图标渲染器。
//渲染器函数必须是静态的,并且采用两个参数:一个是 绘制辅助图标所面向的对象,另一个是 GizmoType 参数,表示绘制辅助图标时所处的上下文。
//渲染器函数可在任何类中定义,包括编辑器脚本。因此, 您可以将辅助图标绘制代码与组件脚本分离,从而 使其不会包含在构建中。
[DrawGizmo(GizmoType.Selected | GizmoType.Active)]
public static void DrawGizmoForMyScript(InspectorMethodTest scr, GizmoType gizmoType)
{
Vector3 position = scr.transform.position;
if (Vector3.Distance(position, Camera.current.transform.position) > 10f)
// The icon has to be stored in Assets/Gizmos
//Gizmos.DrawIcon(position, "MyScript Gizmo.tiff");
Gizmos.DrawLine(Vector3.zero,position);
}
}
把脚本挂载在对象上,获得如下效果参考文献
https://blog.csdn.net/QWBin/article/details/84955331?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link