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
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值