您的位置:首页 > 移动开发 > Unity3D

Unity3D 场景编辑器扩展学习笔记-Handles

2018-03-23 17:07 507 查看
Handles
Namespace: UnityEditor
DescriptionCustom 3D GUI controls and drawing in the scene view.Handles are the 3D controls that Unity uses to manipulate items in the scene view. There are a number of built-in Handle GUIs, such as the familiar tools to position, scale and rotate an object via the Transform component. However, it is also possible to define your own Handle GUIs to use with custom component editors. Such GUIs can be a very useful way to edit procedurally-generated scene content, "invisible" items and groups of related objects, such as waypoints and location markers.You can also supplement the 3D Handle GUI in the scene with 2D buttons and other controls overlaid on the scene view. This is done by enclosing standard Unity GUI calls in a Handles.BeginGUI / EndGUI pair within the /
OnSceneGUI
/ function. You can use HandleUtility.GUIPointToWorldRay and HandleUtility.WorldToGUIPoint to convert coordinates between 2D GUI and 3D world coordinates.
总的来说,Handles就是在SceneView里画一些能响应用户操作的几何图案,并影响指定的变量数值。这个类的接口大致分为以下几种:

Draw类:绘制元几何体,如点、线、面等。DrawAAPolyLine Draw anti-aliased line specified with point array and width.DrawBezier Draw textured bezier line through start and end points with the given tangents. To get an anti-aliased effect use a texture that is 1x2 pixels with one transparent white pixel and one opaque white pixel. The bezier curve will be swept using this texture.
DrawLine Draw a line from p1 to p2.
DrawPolyLine Draw a line going through the list of all points.
DrawSolidArc Draw a circular sector (pie piece) in 3D space.
DrawSolidDisc Draw a solid flat disc in 3D space.
DrawSolidRectangleWithOutline Draw a solid outlined rectangle in 3D space.
DrawWireArc Draw a circular arc in 3D space.
DrawWireDisc Draw the outline of a flat disc in 3D space.

Handle类:可视化操作数值,比如Vector3,Vector2,float等DoPositionHandle 类似对象移动的三向操作器DoRotationHandle 类似对象旋转的三向圆环操作器
DoScaleHandle 类似对象缩放的三轴操作器
FreeMoveHandle Make an unconstrained movement handle.
FreeRotateHandle Make an unconstrained rotation handle.
PositionHandle Make a 3D Scene view position handle.
RadiusHandle Make a Scene view radius handle.
RotationHandle Make a Scene view rotation handle.
ScaleHandle Make a Scene view scale handle.
ScaleValueHandle Make a single-float draggable handle.

Caps类:绘制多边形几何体,比如方块,点精灵,球形,圆锥等。ArrowCapDraw an arrow like those used by the move tool.SphereCapDraw a Sphere. Pass this into handle functions.
CircleCap Draw a camera-facing Circle. Pass this into handle functions.
ConeCap Draw a Cone. Pass this into handle functions.
CubeCap Draw a cube. Pass this into handle functions.
CylinderCap Draw a Cylinder. Pass this into handle functions.
DotCap Draw a camera-facing dot. Pass this into handle functions.
RectangleCap 画个矩形
SelectionFrameDraw a camera facing selection frame.

转载,来自github

using UnityEditor;
using UnityEngine;

namespace Developer.MechanicalDrive
{
[CustomEditor(typeof(Gear), true)]
[CanEditMultipleObjects]
public class GearEditor : MechanismEditor
{
         protected Gear script { get { return target as Gear; } }

         protected virtual void OnSceneGUI()
{
Handles.color = blue;
DrawSphereCap(script.transform.position, Quaternion.identity, nodeSize);
DrawCircleCap(script.transform.position, script.transform.rotation, script.radius);
DrawArrow(script.transform.position, script.transform.forward, arrowLength, nodeSize, "Axis", blue);
}
#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: