美文网首页
Unity Scene窗口编辑工具常用鼠标、键盘例子

Unity Scene窗口编辑工具常用鼠标、键盘例子

作者: unlockc | 来源:发表于2021-02-24 20:04 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System;
    
    [CustomEditor(typeof(ArtAnnotate))]
    public class ArtAnnotateEditor : Editor {
    
        public const int MOUSE_BUTTON_LEFT = 0;
    
        public const int MOUSE_BUTTON_RIGHT = 1;
    
    
        void OnSceneGUI()
        {
    
            if (Event.current.type == EventType.MouseDrag) {
    
            }
    
            if (Event.current.type == EventType.MouseDown) {
                if (Event.current.button == MOUSE_BUTTON_LEFT) {
                    //用发射射线来确定点击目标
                     // Event.current.mousePosition屏幕坐标,左上角是(0,0)右下角(camera.pixelWidth,camera.pixelHeight)
    
                     // 需要转换成摄像机可接受的屏幕坐标,左下角是(0,0,0)右上角是(camera.pixelWidth,camera.pixelHeight,0)
    
                    // 当前屏幕坐标,左上角是(0,0)右下角(camera.pixelWidth,camera.pixelHeight)
                    Vector2 mousePosition = Event.current.mousePosition;
    
                    // Retina 屏幕需要拉伸值
                      float mult = 1;
                      #if UNITY_5_4_OR_NEWER
                        mult = EditorGUIUtility.pixelsPerPoint;
                      #endif
    
                    // 转换成摄像机可接受的屏幕坐标,左下角是(0,0,0)右上角是(camera.pixelWidth,camera.pixelHeight,0)
                    mousePosition.y = SceneView.currentDrawingSceneView.camera.pixelHeight - mousePosition.y * mult;
                    mousePosition.x *= mult;
                    //获取世界坐标
                    SceneView.currentDrawingSceneView.camera.ScreenToWorldPoint(mousePosition);
                }
            }
    
            if (Event.current.type == EventType.MouseUp ){
                if (Event.current.button == MOUSE_BUTTON_LEFT) {
                    //用发射射线来确定点击目标
                } else if (Event.current.button == MOUSE_BUTTON_RIGHT) {
                    if ((Event.current.modifiers & EventModifiers.Control) != 0) {
                        //按下Ctrl键盘
                    } else {
                        GenericMenu menu = new GenericMenu ();
                        menu.AddDisabledItem (new GUIContent ("Not Recommend"));
                        menu.AddItem (new GUIContent ("UnSelect"), aTarget.brushType == BrushType.UnSelect, callMethodUnSelect, null);
                        menu.AddSeparator ("");
                        menu.AddItem (new GUIContent ("Cancel"), aTarget.brushType == BrushType.None, callmethodCancel, null);
                        menu.ShowAsContext ();
                    }
                    //设置该事件被使用
                    Event.current.Use ();
    
                } else {
                    //Debug.Log ("Event.current.button");
                }
                    
            }
        }
    
    }
    
    
        //UnSelect
        void callMethodUnSelect(object obj){
    
        }
    
        //取消
        void callmethodCancel(object obj){
    
        }
    
    

    相关文章

      网友评论

          本文标题:Unity Scene窗口编辑工具常用鼠标、键盘例子

          本文链接:https://www.haomeiwen.com/subject/jtqefltx.html