Unity编辑器拓展(GraphView制作对话系统编辑器)

这篇博客介绍了如何使用Unity的GraphView API创建一个具备无限分支和合并对话能力的编辑器。内容包括:设置网格、创建节点、添加搜索窗口、实现小地图、小黑板功能,并提供了代码示例和资源下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.介绍

1.无限的分支和合并的对话能力。
2.对话框、图形保存/加载系统。
3.小地图,便于导航。
4.有节点创建的搜索窗口。
5.黑板填写属性值。
6.用于分组节点的注释块。
7.由Unity的嵌入式GraphView API支持。
8.在压缩包中提供完整demo和代码注释。 

 

2.基础知识

 1.GraphView

Experimental.GraphView.GraphView - Unity 脚本 APIicon-default.png?t=LA92https://docs.unity.cn/cn/current/ScriptReference/Experimental.GraphView.GraphView.html2.UIElement

Unity2019 UIElement 笔记(一)创建脚本_工 具 人-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/qq_43500611/article/details/89604455

3.Node

Experimental.GraphView.Node - Unity 脚本 APIicon-default.png?t=LA92https://docs.unity.cn/cn/current/ScriptReference/Experimental.GraphView.Node.html

3.部分代码示例

       public StoryGraphView(StoryGraph editorWindow)
       {
            //网格绘制
            styleSheets.Add(Resources.Load<StyleSheet>("NarrativeGraph"));
            SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);

            this.AddManipulator(new ContentDragger());
            this.AddManipulator(new SelectionDragger());
            this.AddManipulator(new RectangleSelector());
            this.AddManipulator(new FreehandSelector());

            var grid = new GridBackground();
            Insert(0, grid);
            grid.StretchToParentSize();

            //开始节点
            AddElement(GetEntryPointNodeInstance());
            //搜索窗口
            AddSearchWindow(editorWindow);
        }
 /// <summary>
        /// 工具栏绘制
        /// </summary>
        private void GenerateToolbar()
        {
            var toolbar = new Toolbar();

            var fileNameTextField = new TextField("File Name:");
            fileNameTextField.SetValueWithoutNotify(_fileName);
            fileNameTextField.MarkDirtyRepaint();
            fileNameTextField.RegisterValueChangedCallback(evt => _fileName = evt.newValue);
            toolbar.Add(fileNameTextField);

            toolbar.Add(new Button(() => RequestDataOperation(true)) {text = "Save Data"});

            toolbar.Add(new Button(() => RequestDataOperation(false)) {text = "Load Data"});
            // toolbar.Add(new Button(() => _graphView.CreateNewDialogueNode("Dialogue Node")) {text = "New Node",});
            rootVisualElement.Add(toolbar);
        }


/// <summary>
        /// 小地图绘制
        /// </summary>
        private void GenerateMiniMap()
        {
            var miniMap = new MiniMap {anchored = true};
            var cords = _graphView.contentViewContainer.WorldToLocal(new Vector2(this.maxSize.x - 10, 30));
            miniMap.SetPosition(new Rect(cords.x, cords.y, 200, 140));
            _graphView.Add(miniMap);
        }


/// <summary>
        /// 小黑板绘制
        /// </summary>
        private void GenerateBlackBoard()
        {
            var blackboard = new Blackboard(_graphView);
            blackboard.Add(new BlackboardSection {title = "Exposed Variables"});
            blackboard.addItemRequested = _blackboard =>
            {
                _graphView.AddPropertyToBlackBoard(ExposedProperty.CreateInstance(), false);
            };
            blackboard.editTextRequested = (_blackboard, element, newValue) =>
            {
                var oldPropertyName = ((BlackboardField) element).text;
                if (_graphView.ExposedProperties.Any(x => x.PropertyName == newValue))
                {
                    EditorUtility.DisplayDialog("Error", "This property name already exists, please chose another one.",
                        "OK");
                    return;
                }

                var targetIndex = _graphView.ExposedProperties.FindIndex(x => x.PropertyName == oldPropertyName);
                _graphView.ExposedProperties[targetIndex].PropertyName = newValue;
                ((BlackboardField) element).text = newValue;
            };
            blackboard.SetPosition(new Rect(10,30,200,300));
            _graphView.Add(blackboard);
            _graphView.Blackboard = blackboard;
        }

代码资源下载地址

Unity对话系统编辑器-互联网文档类资源-CSDN文库icon-default.png?t=LA92https://download.csdn.net/download/m0_46712616/46797276

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

萧寒大大

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

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

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

打赏作者

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

抵扣说明:

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

余额充值