碧血红天的HomePage

U3d杂谈 — 使用GraphView 做工具 — 01

GraphView是U3d提供一个实验性功能,可以使用这个创建节点编辑器的功能。此系统使用的是UIElement新的编辑器UI框架,类似于网页的CSS+XML的UI框架。

此处说明如何简单的使用GraphView然后如何添加节点做一个简单的计算。目的是抛砖引玉,看看如何使用。

首先,我们需要继承GraphView来实现一个自己的GraphView

using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
public class MyGraphView : GraphView
{
    // Start is called before the first frame update
    public MyGraphView()
    {
       //插入背景
        Insert(0, new GridBackground());
//与父类的尺寸同步
        this.StretchToParentSize();
//设置缩放的范围值
        SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
//添加选取拖动的控制器
        this.AddManipulator(new SelectionDragger());
        //graphview窗口内容的拖动
        this.AddManipulator(new ContentDragger());
    }
}

上面是最简单的GraphView,现在需要创建一个EditorWindow编辑器窗口来承载上面的View

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

public class MyGraphWindow : EditorWindow
{
    
    [MenuItem("Cmd/MyGraphView")]
    public static void OpenMyGraphView()
    {
        MyGraphWindow window = GetWindow<MyGraphWindow>("MyGraphWindow");

        MyGraphView view = new MyGraphView();
        window.rootVisualElement.Add(view);
    }
}

我们添加一个自定义菜单命令,打开一个Window窗口,然后把GraphView添加刀根视图节点里面。

完成上面的2个类,我们就能打开一个GraphView的窗口了,但是里面一片空白,我们需要响应GraphView的右键创建节点的委托函数,来创建节点,我这里直接创建一个带输入与输出的int类型节点。

我们在MyGraphView的构造函数中添加如下代码:

this.nodeCreationRequest += (NodeCreationContext t) =>{
            Node s = new Node() { title = "Node"};
            Port p = Port.Create<Edge>(Orientation.Horizontal, Direction.Input, Port.Capacity.Single, typeof(int));
            s.inputContainer.Add(p);

            Port p2 = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Single, typeof(int));
            s.outputContainer.Add(p2);

            this.AddElement(s);
        };

现在我们就能在GraphView中点击右键,会出现一个CreateView的菜单命令,点击一下就能创建一个节点。效果如下:

到达这一步,我们还不能把2个节点的输入输出端口连接起来。上面的代码中,inputContainer和outputContainer是节点默认预留的2个显示区域,位置是固定的。我们创建的Port可以根据情况任意放入任何一个容器中。除了这2个固定节点容器,还预留的有其它容器,可以看Node的定义就清楚了。

Port只能通过提供的静态Static方法创建,这很重要,如果自己通过New创建对象是没法使用的,因为Create方法中还执行了许多其它代码,有给Port添加控制器,否则不能拖出连接线。

Port需要设置连接线的方向,有水平和垂直,水平代码连接线连接节点是水平还是垂直的,主要是显示,跟功能无影响。Direction这个是Port的数据方向,代码是输入Port还是输出Port。Capacity是Port能够是多联还是单连接,根据自己情况设置即可。最后一个是Port的数据类型,正确的数值类型才能进行正确的计算,否则我们不知道啥类型而不知道如何去处理输入的数据。

为了让节点Port能够相互连接线,我们需要覆写一个函数:

  public override List<Port> GetCompatiblePorts(Port startAnchor, NodeAdapter nodeAdapter)
    {

        List<Port> compatibleList = new List<Port>();
        foreach (var port in ports.ToList())
        {
            if (startAnchor.node == port.node || startAnchor.direction == port.direction)
            {
                continue;
            }
            if (startAnchor.portType != port.portType)
            {
                if (port.portType != typeof(object) && !startAnchor.portType.IsAssignableFrom(port.portType) && !port.portType.IsAssignableFrom(startAnchor.portType))
                {
                    continue;
                }
            }
            compatibleList.Add(port);
        }

        return compatibleList;
    }

它的思想很简单,就是在牵出一个Port的线就会调用此函数,然后遍历当前GraphView中所有的Port,根据类型,方向判断哪些Port可以与当前Port连接,返回一个能连接的Port列表。

下一篇介绍如何扩展自己的Node,来通过节点进行计算。

25 评论

  1. I will right away clutch your rss as I can not in finding your e-mail subscription hyperlink or e-newsletter service. Do you’ve any? Kindly permit me know so that I may subscribe. Thanks.

  2. I believe this internet site has some rattling good information for everyone. “A kiss, is the physical transgression of the mental connection which has already taken place.” by Tanielle Naus.

  3. My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am concerned about switching to another platform. I have heard very good things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any help would be greatly appreciated!

  4. I’ve been browsing on-line more than 3 hours as of late, but I by no means discovered any attention-grabbing article like yours. It is pretty value enough for me. Personally, if all web owners and bloggers made just right content material as you probably did, the web shall be much more helpful than ever before. “I think that maybe if women and children were in charge we would get somewhere.” by James Grover Thurber.

  5. I conceive this internet site has got some very excellent information for everyone. “I prefer the wicked rather than the foolish. The wicked sometimes rest.” by Alexandre Dumas.

  6. Good day! I just would like to give an enormous thumbs up for the nice information you’ve got right here on this post. I will likely be coming back to your blog for extra soon.

  7. What’s Taking place i am new to this, I stumbled upon this I’ve found It absolutely useful and it has aided me out loads. I’m hoping to contribute & aid different users like its helped me. Great job.

  8. I got what you mean , regards for putting up.Woh I am thankful to find this website through google. “Spare no expense to make everything as economical as possible.” by Samuel Goldwyn.

  9. Great V I should definitely pronounce, impressed with your site. I had no trouble navigating through all the tabs and related information ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or something, website theme . a tones way for your customer to communicate. Nice task..

  10. My partner and I stumbled over here different web page and thought I may as well check things out. I like what I see so i am just following you. Look forward to exploring your web page again.

  11. Fantastic website. Lots of useful info here. I am sending it to some pals ans additionally sharing in delicious. And obviously, thank you to your effort!

  12. After examine a couple of of the blog posts on your web site now, and I truly like your means of blogging. I bookmarked it to my bookmark web site list and will probably be checking again soon. Pls take a look at my site as properly and let me know what you think.

  13. Wohh exactly what I was searching for, appreciate it for putting up.

  14. Fantastic beat ! I wish to apprentice even as you amend your web site, how could i subscribe for a blog website? The account aided me a applicable deal. I have been tiny bit acquainted of this your broadcast offered vibrant transparent concept

  15. Hello my friend! I want to say that this post is awesome, nice written and include approximately all significant infos. I would like to see more posts like this.

  16. Wow! Thank you! I always wanted to write on my site something like that. Can I take a fragment of your post to my website?

  17. You really make it appear so easy together with your presentation however I in finding this topic to be actually one thing which I think I might by no means understand. It seems too complex and very large for me. I’m looking forward on your next publish, I will try to get the hold of it!

  18. Can I simply say what a relief to find someone who truly knows what theyre speaking about on the internet. You undoubtedly know tips on how to convey an issue to light and make it important. More folks must read this and understand this side of the story. I cant imagine youre not more standard since you undoubtedly have the gift.

  19. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier.

  20. What’s Happening i’m new to this, I stumbled upon this I’ve found It absolutely helpful and it has helped me out loads. I hope to contribute & help other users like its aided me. Great job.

  21. You actually make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I will try to get the hang of it!

  22. I¦ve recently started a website, the information you provide on this website has helped me greatly. Thanks for all of your time & work.

  23. There are some interesting time limits on this article however I don’t know if I see all of them heart to heart. There may be some validity but I’ll take hold opinion until I look into it further. Good article , thanks and we would like more! Added to FeedBurner as effectively

  24. generic 5mg cialis best price Can someone tell me how long I can and should take Femara and if I can not sell it

发表评论