碧血红天的HomePage

U3D杂谈 — 使用EditorWindow

如果我们想自定义编辑器窗口,那么我们我们能继承EditorWindow类,然后通CustomEditor属性标签来指明这个类型。创建一个窗口的代码如下:

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using LuaInterface;

[CustomEditor(typeof(LuaCmder))]
public class LuaCmder : UnityEditor.EditorWindow
{
    [MenuItem("RG_SLG/LuaCmder")]
    public static void ShowWindow()
    {
        //GetWindow创建
        UnityEditor.EditorWindow.GetWindow(typeof(LuaCmder));
    }

    GUIStyle TextAreaSt;

    void OnEnable()
    {
        TextAreaSt = new GUIStyle();
    }

    void OnGUI()
    {
        GUILayout.BeginVertical(GUILayout.ExpandHeight(true));
        scrollPos = GUILayout.BeginScrollView(scrollPos);
        TextAreaSt.stretchHeight = true;
        TextAreaSt.normal.textColor = Color.white;
        TextAreaSt.fontSize = 14;
        TextAreaSt.margin = new RectOffset(5, 5, 5, 5);

        luaScript = GUILayout.TextArea(luaScript, TextAreaSt);
        GUIStyle btnSt = new GUIStyle(GUI.skin.button);
        btnSt.margin = new RectOffset(5, 5, 5, 10);
        GUILayout.EndScrollView();
        bool exec = GUILayout.Button("执行", btnSt);
        if (exec)
        {
            ExecScript();
        }
        GUILayout.EndVertical();

    }
}

如何我们想在里面响应键盘事件,我们不能使用Input类,需要使用Event。方法如下:

void OnKeyEvent()
{
        Event e = Event.current;
        if (e.isKey)
        {
            if (e.keyCode == KeyCode.PageUp && e.type == EventType.KeyUp)
            {
                
               //当前窗口会脱离焦点,我们从新设置一下
                Focus();
            }
            else if (e.keyCode == KeyCode.PageDown && e.type == EventType.KeyUp)
            {
                 //当前窗口会脱离焦点,我们从新设置一下
                Focus();
            }
            else if (e.keyCode == KeyCode.Home && e.type == EventType.KeyUp)
            {
                 //当前窗口会脱离焦点,我们从新设置一下
                Focus();
            }
        }
}

然后我们把上面的方法在OnGUI方法中调用,这样我们技能监听到键盘等响应事件了。

19 评论

  1. he blog was how do i say it… relevant, finally something that helped me. Thanks

  2. Great website! I am loving it!! Will come back again. I am bookmarking your feeds also

  3. Just what I was searching for, regards for putting up.

  4. Keep up the good work, I read few blog posts on this site and I believe that your web blog is real interesting and contains circles of wonderful information.

  5. Hello There. I discovered your blog the use of msn. That is an extremely smartly written article. I will make sure to bookmark it and return to read extra of your useful info. Thanks for the post. I will certainly return.

  6. I’m impressed, I must say. Really hardly ever do I encounter a blog that’s each educative and entertaining, and let me inform you, you might have hit the nail on the head. Your concept is excellent; the issue is one thing that not enough individuals are talking intelligently about. I am very completely satisfied that I stumbled across this in my seek for one thing referring to this.

  7. I got what you mean ,saved to bookmarks, very nice site.

  8. Hello! Do you use Twitter? I’d like to follow you if that would be ok. I’m absolutely enjoying your blog and look forward to new updates.

  9. Heya i’m for the primary time here. I came across this board and I find It really helpful & it helped me out much. I am hoping to provide something again and help others such as you aided me.

  10. I got what you mean , thankyou for posting.Woh I am pleased to find this website through google.

  11. I like this blog very much, Its a very nice office to read and incur info .

  12. I was recommended this blog through my cousin. I’m now not certain whether or not this submit is written by means of him as no one else recognise such distinctive about my problem. You are incredible! Thank you!

  13. Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.

  14. I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thanks again!

  15. Thanks for the sensible critique. Me & my neighbor were just preparing to do some research about this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such wonderful information being shared freely out there.

  16. hello!,I like your writing so much! share we communicate more about your post on AOL? I need a specialist on this area to solve my problem. Maybe that’s you! Looking forward to see you.

  17. What i do not realize is actually how you’re not actually much more well-liked than you might be now. You are so intelligent. You realize thus significantly relating to this subject, produced me personally consider it from so many varied angles. Its like women and men aren’t fascinated unless it’s one thing to accomplish with Lady gaga! Your own stuffs excellent. Always maintain it up!

  18. Hiya, I’m really glad I’ve found this information. Nowadays bloggers publish only about gossips and internet and this is really annoying. A good website with interesting content, that’s what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Can’t find it.

  19. There are actually numerous particulars like that to take into consideration. That could be a nice point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you carry up the place an important factor can be working in sincere good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game. Each boys and girls feel the affect of just a second’s pleasure, for the remainder of their lives.

发表评论