Unity编辑器拓展-事件处理

事件处理

  • 分类
    • 键盘事件
    • 鼠标事件
  • 获取事件
var evt = Event.current;

FAQ

  • Input的事件机制在编辑器模式下还生效吗?
    • 不生效了

代码

  • 不是我想水文章,这个模块的机制相当单纯,但是开发的时候有很多业务事件处理并不单纯。
  • 下面的代码展示了编辑器下处理事件的方式
using System;
using UnityEditor;
using UnityEngine;

namespace DC.DCIMGUIBox
{
    public class EventProcessWindow : EditorWindow
    {
        [MenuItem("DC/IMGUI/EventProcessWindow")]
        public static void Open()
        {
            var window = GetWindow<EventProcessWindow>();
            window.minSize = new Vector2(800, 600);
        }

        private string content = "";

        private Vector2 scrollVPos;

        public void OnGUI()
        {
            var evt = Event.current;

            content += string.Format("evt.type {0}, {1}\n", Enum.GetName(typeof(EventType), evt.type),
                Enum.GetName(typeof(EventType), evt.rawType));

            if (content.Length > 1000)
            {
                content = content.Substring(content.Length - 1000);
            }

            if (evt.type == EventType.MouseDrag)
            {
                Debug.Log(evt.mousePosition);
            }

            scrollVPos = GUILayout.BeginScrollView(scrollVPos, GUILayout.Height(400));
            GUILayout.TextArea(content);
            GUILayout.EndScrollView();

            if (GUILayout.Button("Test Input"))
            {
                Debug.Log("Input.mousePosition " + Input.mousePosition);
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                Debug.Log("key down a");
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值