EditorEvent


封装起来方便用


using UnityEngine;
using UnityEditor;
using System;

public enum Mouse
{
    Left,
    Right,
    Middle
}

public static class EditorEvent
{

    #region public function

    public static Event CommandPaste(this Event current, Action callback)
    {
        Command("Paste", callback);
        return current;
    }

    public static Event CommandCopy(this Event current, Action callback)
    {
        Command("Copy", callback);
        return current;

    }

    public static Event CommandCut(this Event current, Action callback)
    {
        Command("Cut", callback);
        return current;

    }

    public static Event CommandDelete(this Event current, Action callback)
    {
        Command("Delete", callback);
        return current;

    }

    public static Event CommandFrameSelected(this Event current, Action callback)
    {
        Command("FrameSelected", callback);
        return current;

    }

    public static Event CommandDuplicate(this Event current, Action callback)
    {
        Command("Duplicate", callback);
        return current;
    }

    public static Event CommandSelectAll(this Event current, Action callback)
    {
        Command("SelectAll", callback);
        return current;
    }

    public static bool IsKeyDown(this Event current)
    {
        return current.type == EventType.keyDown;
    }

    public static bool IsKeyUp(this Event current)
    {
        return current.type == EventType.KeyUp;
    }

    public static bool IsDoubleClick(this Event current)
    {
        return current.clickCount == 2;
    }

    public static bool IsContextClick(this Event current)
    {
        return current.type == EventType.ContextClick;
    }

    public static bool IsMouseDown(this Event current)
    {
        return current.type == EventType.mouseDown;
    }

    public static bool IsMouseUp(this Event current)
    {
        return current.type == EventType.mouseUp;
    }

    public static bool IsMouseMove(this Event current)
    {
        return current.type == EventType.mouseMove;
    }

    public static bool IsMouseDrag(this Event current)
    {
        return current.type == EventType.mouseDrag;
    }

    public static bool IsDragPerform(this Event current)
    {
        if (current.type == EventType.DragPerform
          || current.type == EventType.DragUpdated)
        {
            DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
                return true;
        }

        return false;
    }

    public static bool IsDragEnd(this Event current)
    {
        return current.type == EventType.DragExited;
    }

    public static bool IsLayout(this Event current)
    {
        return current.type == EventType.Layout;
    }

    public static bool IsDoubleClick(this Rect rect)
    {
        if (!Event.current.IsDoubleClick()) return false;
        return IsRectContains(rect);
    }

    public static bool IsContextClick(this Rect rect)
    {
        if (!Event.current.IsContextClick()) return false;
        return IsRectContains(rect);
    }

    public static bool IsMouseDown(this Rect rect)
    {
        if (!Event.current.IsMouseDown()) return false;
        return IsRectContains(rect);
    }

    public static bool IsMouseUp(this Rect rect)
    {
        if (!Event.current.IsMouseUp()) return false;
        return IsRectContains(rect);
    }

    public static bool IsMouseMove(this Rect rect)
    {
        if (!Event.current.IsMouseMove()) return false;
        return IsRectContains(rect);
    }

    public static bool IsDragPerform(this Rect rect)
    {
        if ((Event.current.type == EventType.DragPerform
          || Event.current.type == EventType.DragUpdated)
          && rect.Contains(Event.current.mousePosition))
        {
            DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
                return true;
        }
        return false;
    }

    public static bool IsDragEnd(this Rect rect)
    {
        if (!Event.current.IsDragEnd()) return false;
        return IsRectContains(rect);
    }

    public static float GetScrollDelta()
    {
        return Event.current.delta.y;
    }

    public static KeyCode GetKeyCode()
    {
        return Event.current.keyCode;
    }

    public static Mouse GetButton()
    {
        return (Mouse)Event.current.button;
    }

    public static Vector2 GetMousePosition()
    {
        return Event.current.mousePosition;
    }

    #endregion

    #region private function

    private static bool IsRectContains(Rect rect)
    {
        return rect.Contains(Event.current.mousePosition);
    }

    private static void Command(string command, Action callback)
    {
        if (null == callback) return;
        if (Event.current.type == EventType.ValidateCommand && Event.current.commandName == command)
        {
            callback();
            Event.current.Use();
        }
    }


    #endregion
}

你可能感兴趣的:(unity3d,unity,editor,event)