Unity3d ---- Handles&Event--------


Handles
Namespace: UnityEditor

Description
Custom 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.


2DGUI类:用GUILayout或EditorGUILayout代替吧。


BeginGUIBegin a 2D GUI block inside the 3D handle GUI.
EndGUIEnd a 2D GUI block and get back to the 3D handle GUI.Slider2D Slide a handle in a 2D plane.

Camera类:没弄懂,求教


DrawCameraDraws a camera inside a rectangle.
SetCameraSet the current camera so all Handles and Gizmos are draw with its settings.ClearCamera Clears the camera.SnapValue Rounds the value val to the closest multiple of snap (snap can only be posiive).


Event
Namespace: UnityEngine

Description
A UnityGUI event.

Events correspond to user input (key presses, mouse actions), or are UnityGUI layout or rendering events.
For each event [OnGUI](file:///D:/Program%20Files/Unity/Editor/Data/Documentation/Documentation/ScriptReference/MonoBehaviour.OnGUI.html) is called in the scripts; so OnGUI is potentially called multiple times per frame. Event.current corresponds to "current" event inside OnGUI call.

Event类和常见的Input系统里传递的参数差不多,只是多了来自于U3D编辑器的菜单栏操作信息。


鼠标相关
button Which mouse button was pressed.
clickCountHow many consecutive mouse clicks have we received.
mousePositionThe mouse position.


键盘相关


character The character typed.
keyCode The raw key code for keyboard events.
modifiers Which modifier keys are held down.


其他信息
delta The relative movement of the mouse compared to last event.
type The type of event.


编辑器类-特别的

commandNameThe name of an ExecuteCommand or ValidateCommand Event.


查询类

command Is Command/Windows key held down? (Read Only)
isKey Is this event a keyboard event? (Read Only)
isMouse Is this event a mouse event? (Read Only)
alt Is Alt/Option key held down? (Read Only)
shift Is Shift held down? (Read Only)
capsLock Is Caps Lock on? (Read Only)
control Is Control key held down? (Read Only)
functionKeyIs the current keypress a function key? (Read Only)
numeric Is the current keypress on the numeric keyboard? (Read Only)

范例代码:

using UnityEngine;
using System.Collections;

public class SceneTag : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    public Vector3 vectorPoint = Vector3.zero;
    public Vector3 vectorPoint2 = Vector3.zero;
    
    public float shieldArea = 5;
    public Quaternion rot = Quaternion.identity;
}
<pre code_snippet_id="194224" snippet_file_name="blog_20140219_1_8902285"></pre><pre code_snippet_id="194224" snippet_file_name="blog_20140219_2_3504874" name="code" class="csharp">using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.Text;

[CustomEditor(typeof(SceneTag))]
public class SceneEditor : Editor {
    

    void OnEnable()
    {
    }

    void OnDisable()
    {

    }

    void OnDestroy()
    {

    }

    void OnInspectorGUI()
    {
    }

    void OnSceneGUI()
    {
        Test_Handles();
        Test_Event();
    }

    private static void Test_Event()
    {
        Vector2 mousePosition = Event.current.mousePosition;
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("({0},{1})"
            , mousePosition.x
            , mousePosition.y);
        //Debug.Log(sb.ToString());
    }

    private void Test_Handles()
    {
        SceneTag scene = target as SceneTag;
        Test_Color(scene);
        //Test_Button(scene);
        Test_ArrowCap(scene);
        Test_CircleCap(scene);
        Test_ChangeValue(scene);
        Test_DrawAAPolyLine(scene);
        Test_DrawSolidArc(scene);
        Test_ScaleValueHandle(scene);
        Test_FreeRotateHandle(scene);
        Test_FreeMoveHandle(scene);
        Test_BeginGUI(scene);

        if (GUI.changed)
            EditorUtility.SetDirty(target);
    }

    private static void Test_FreeMoveHandle(SceneTag scene)
    {
        scene.vectorPoint = Handles.FreeMoveHandle(scene.vectorPoint,
                                  Quaternion.identity,
                                  2.0f,
                                  Vector3.zero,
                                  Handles.ArrowCap);
    }

    private static void Test_FreeRotateHandle(SceneTag scene)
    {
        scene.transform.rotation = Handles.FreeRotateHandle(scene.transform.rotation, scene.transform.position, 0.5f);
    }

    private static void Test_ScaleValueHandle(SceneTag scene)
    {
        scene.shieldArea
                  = Handles.ScaleValueHandle(scene.shieldArea,
                                  scene.transform.position + scene.transform.forward * scene.shieldArea,
                                  scene.transform.rotation,
                                  1,
                                  Handles.ConeCap,
                                  1);
    }

    private static void Test_DrawSolidArc(SceneTag scene)
    {
        Handles.color = new Color(1, 1, 1, 0.2f);
        Handles.DrawSolidArc(scene.transform.position,
                scene.transform.up,
                -scene.transform.right,
                270,
                scene.shieldArea);
        Handles.color = Color.white;
        scene.shieldArea =
        Handles.ScaleValueHandle(scene.shieldArea,
                        scene.transform.position + scene.transform.forward * scene.shieldArea,
                        scene.transform.rotation,
                        1,
                        Handles.ConeCap,
                        1);
    }

    private static void Test_DrawAAPolyLine(SceneTag scene)
    {
        Vector3[] positions = new Vector3[]
        {
            new Vector3(2,0,0),
            new Vector3(2,1,0),
            new Vector3(2,1,1),
            new Vector3(1,2,1),
            new Vector3(1,2,2),
        };
        for (int i = 0; i < positions.Length; ++i)
        {
            positions[i] += scene.transform.position;
        }
        Handles.DrawAAPolyLine(positions);
    }

    private static void Test_ChangeValue(SceneTag scene)
    {
        scene.rot =
                     Handles.Disc(scene.rot,
                             scene.transform.position,
                             new Vector3(1, 1, 0),
                             5,
                             false,
                             1);
        scene.vectorPoint = Handles.DoPositionHandle(scene.vectorPoint, Quaternion.identity);
        scene.vectorPoint2 = Handles.DoPositionHandle(scene.vectorPoint2, Quaternion.identity);
    }

    private static void Test_CircleCap(SceneTag scene)
    {
        float circleSize = 1;
        Handles.color = Color.red;
        Handles.CircleCap(0,
                scene.transform.position + new Vector3(5, 0, 0),
                scene.transform.rotation,
                circleSize);
        Handles.color = Color.green;
        Handles.CircleCap(0,
                scene.transform.position + new Vector3(0, 5, 0),
                scene.transform.rotation,
                circleSize);
        Handles.color = Color.blue;
        Handles.CircleCap(0,
                scene.transform.position + new Vector3(0, 0, 5),
                scene.transform.rotation,
                circleSize);

        Handles.color = Color.red;
        Vector3 newpos = scene.transform.position + new Vector3(5, 0, 0);
        circleSize = HandleUtility.GetHandleSize(newpos);
        Handles.CircleCap(0,
                newpos,
                scene.transform.rotation,
                circleSize);
        Handles.color = Color.green;
        newpos = scene.transform.position + new Vector3(0, 5, 0);
        circleSize = HandleUtility.GetHandleSize(newpos);
        Handles.CircleCap(0,
                newpos,
                scene.transform.rotation,
                circleSize);
        Handles.color = Color.blue;
        newpos = scene.transform.position + new Vector3(0, 0, 5);
        circleSize = HandleUtility.GetHandleSize(newpos);
        Handles.CircleCap(0,
                newpos,
                scene.transform.rotation,
                circleSize);

    }

    //note by kun 2014.2.18
    // 这玩意有啥用?巨难操作!;
    // 3D按钮还会因为视角问题呈现不一样的大小。;
    // 而且按钮的当前状态也更新不及时,逗么?;
    private static void Test_Button(SceneTag scene)
    {
        Handles.Button(scene.transform.position + new Vector3(0, 2, 0),
                              Quaternion.identity,
                              3,
                              3,
                              Handles.RectangleCap);
    }

    private static void Test_BeginGUI(SceneTag scene)
    {
        Handles.color = Color.blue;
        Handles.Label(scene.transform.position + Vector3.up * 2,
                scene.transform.position.ToString() + "\nShieldArea: " +
                scene.shieldArea.ToString());
        
        Handles.color = Color.red;
        Handles.DrawWireArc(scene.transform.position,
                scene.transform.up,
                -scene.transform.right,
                180,
                scene.shieldArea);
        scene.shieldArea =
        Handles.ScaleValueHandle(scene.shieldArea,
                        scene.transform.position + scene.transform.forward * scene.shieldArea,
                        scene.transform.rotation,
                        1,
                        Handles.ConeCap,
                        1);

        //comment by kun 2014.2.18
        // GUI相关的绘制需要在Handles的绘制之后,否则会被覆盖掉;
        // 使用Handles.BeginGUI会导致无法旋转摄像机,原因不详;
        GUILayout.BeginArea(new Rect(Screen.width - 100, Screen.height - 80, 90, 50));
        //Handles.BeginGUI(new Rect(Screen.width - 100, Screen.height - 80, 90, 50));
        try
        {
            float a = float.Parse(GUILayout.TextField(scene.shieldArea.ToString()));
            scene.shieldArea = a;
        }
        catch (System.Exception ex)
        {
            
        }
        if (GUILayout.Button("Reset Area"))
            scene.shieldArea = 5;
        //Handles.EndGUI();
        GUILayout.EndArea();
    }

    private static void Test_ArrowCap(SceneTag scene)
    {
        float arrowSize = 1;
        Handles.color = Color.red;
        Handles.ArrowCap(0,
                scene.transform.position + new Vector3(5, 0, 0),
                scene.transform.rotation,
                arrowSize);
        Handles.color = Color.green;
        Handles.ArrowCap(0,
                scene.transform.position + new Vector3(0, 5, 0),
                scene.transform.rotation,
                arrowSize);
        Handles.color = Color.blue;
        Handles.ArrowCap(0,
                scene.transform.position + new Vector3(0, 0, 5),
                scene.transform.rotation,
                arrowSize);
    }

    private void Test_Color(SceneTag scene)
    {
        Handles.color = Color.magenta;
        scene.vectorPoint = Handles.Slider(scene.vectorPoint,
                                Vector3.zero - scene.transform.position);
    }

}</pre>
<pre></pre>
<pre></pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容