欢迎光临散文网 会员登陆 & 注册

【Unity Tips】关于GridBackground的正确用法

2023-03-30 14:28 作者:YouSing  | 我要投稿

前言

        最近重构一下自己写的连连看的通用编辑器,发现GridBackground一直都是黑的,根本没格网。

https://docs.unity.cn/cn/2022.1/ScriptReference/Experimental.GraphView.GridBackground.html

压根等于没写

解决方案

        UIElements其实有个类似css的文件,也就是后缀名为uss的文件。很多UIElements组件的样式,需要你自己写uss来实现修改。其实原本一个脚本可以搞定,你现在就要分两个地方写了。

https://docs.unity3d.com/cn/2021.2/Manual/UIE-USS.html

        那么,我们所需要的uss文件内容,大概如下:

GridBackground{

    --spacing: 25;

    --thick-lines: 10;

    --line-color: #232323;

    --thick-line-color: #FF0000;

    --grid-background-color: #7F7F7F;

}

        测试的代码如下:

using UnityEditor;

using UnityEditor.Experimental.GraphView;

using UnityEngine.UIElements;

public class GridBackgroundTest:GraphViewEditorWindow {

    public class GraphView

        :UnityEditor.Experimental.GraphView.GraphView

    {

        public GraphView() {

            style.flexGrow=1;

            //

            var bg=new GridBackground();

            var obj=AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/GridBackgroundTest.uss");

            if(obj!=null) {bg.styleSheets.Add(obj);}

            Insert(0,bg);

            //

            SetupZoom(ContentZoomer.DefaultMinScale,2.0f);

            this.AddManipulator(new ContentDragger());

            this.AddManipulator(new SelectionDragger());

            this.AddManipulator(new RectangleSelector());

        }

    }


    public GraphView graphView;

    [MenuItem("Test/GridBackground")]

    public static void Open() {

        GridBackgroundTest w=GetWindow<GridBackgroundTest>("GridBackgroundTest");

        if(w.graphView==null) {

            w.graphView=new GraphView();

            w.rootVisualElement.Add(w.graphView);

    }

    w.Show();

    }

}

完结撒花

感言

        Unity在文档方面,感觉越来越不填坑。看来有时候,还是需要把踩过的坑记录一下,避免重复跌倒。


【Unity Tips】关于GridBackground的正确用法的评论 (共 条)

分享到微博请遵守国家法律