UI Tool Kit 使用

这篇具有很好参考价值的文章主要介绍了UI Tool Kit 使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Unity 2021 已经把UIBuilder 内置了,项目组也打算 后续工具采用 toolkit来写,这边也是找了一下教程熟悉了一下。

UI 工具包 - Unity 手册

UI Tool Kit 使用,unity,UIToolKit,UIBuilder

 首先 先创建一个EditorWindow

UI Tool Kit 使用,unity,UIToolKit,UIBuilder

 会生成相应的C#,UXML,USS代码

UI Tool Kit 使用,unity,UIToolKit,UIBuilder

 默认会把显示的MenuItem代码生成,以及Root VisualElement生成,会默认加载对应的uxml文件。

 [MenuItem("Tools/TestTool")]
    public static void ShowExample()
    {
        TestTool wnd = GetWindow<TestTool>();
        wnd.titleContent = new GUIContent("TestToolPanel");
    }

public void CreateGUI()
    {
        // Each editor window contains a root VisualElement object
        VisualElement root = rootVisualElement;

        // VisualElements objects can contain other VisualElement following a tree hierarchy.
        VisualElement label = new Label("Hello World! From C#");
        root.Add(label);

        // Import UXML
        var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Cube/delete.uxml");
        VisualElement labelFromUXML = visualTree.Instantiate();
        root.Add(labelFromUXML);

        // A stylesheet can be added to a VisualElement.
        // The style will be applied to the VisualElement and all of its children.
        var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Cube/delete.uss");
        VisualElement labelWithStyle = new Label("Hello World! With Style");
        labelWithStyle.styleSheets.Add(styleSheet);
        root.Add(labelWithStyle);
    }

新的方式 是通过 像Root 结点添加 各种组件的方式,可以在 UIBuilder面板上操作,也可以代码添加。

UI Tool Kit 使用,unity,UIToolKit,UIBuilder

 代码添加:

 var rightPart = root.Q<VisualElement>("right");
 HelpBox tipBox = new HelpBox("Test Tip", HelpBoxMessageType.Info);   
 rightPart.Add(tipBox);

想使用 面板上的 结点可以采用代码查找Name的方式:

 _objFiled = root.Q<ObjectField>("ObjectField");

 还可以给某些组件 添加事件,比如 Toggle

toggle.RegisterValueChangedCallback(TogValueChanged);

 private void TogValueChanged(ChangeEvent<bool> evt)
 {
     // evt.newValue
 }

按钮组件的 事件 绑定:

_createBtn = root.Q<Button>("CreateBtn");
_createBtn.clicked += CreateEvent;

ListView组件的使用:

 _leftList = root.Q<ListView>("LeftListView");
 //赋值
 _leftList.itemsSource = _objs;
//单个Item
 _leftList.makeItem = MakeListItem;
//数据绑定
 _leftList.bindItem = BindListItem;
   //List Item 选中事件
 _leftList.onSelectionChange += OnSelectChange;

新版UI 也支持 IMGUI嵌入

//嵌入IMGUI 
 rightPart.Add(new IMGUIContainer(() =>
  {
            if (GUILayout.Button("TestBtn"))
            {
                Debug.Log("TestLog");
            }
            
   }));              

支持计时器任务:

//计时器(默认 打开界面 就会执行)
        var scheduleItem = root.schedule.Execute(() =>
        {
            Debug.Log("Schedule Event");
        });
//间隔2秒执行一次
        //scheduleItem.Every(2000);
        //打开页面之后延迟两秒执行
        //scheduleItem.ExecuteLater(2000);

UI Tool Kit 使用,unity,UIToolKit,UIBuilder

 完整代码:


    //打开页面执行一次
    public void CreateGUI()
    {
        // Each editor window contains a root VisualElement object
        VisualElement root = rootVisualElement;

        // Import UXML
        var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/Test/TestTool.uxml");
        VisualElement labelFromUXML = visualTree.Instantiate();
        root.Add(labelFromUXML);
        var rightPart = root.Q<VisualElement>("right");
        HelpBox tipBox = new HelpBox("Test Tip", HelpBoxMessageType.Info);
        
        rightPart.Add(tipBox);
        _objFiled = root.Q<ObjectField>("ObjectField");
        if (_objFiled != null)
        {
            _objFiled.objectType = typeof(GameObject);
            _objFiled.allowSceneObjects = false;
            //注册一个回调 当有变化的时候
            _objFiled.RegisterValueChangedCallback((obj) => { Debug.Log(obj.newValue.name); });
        }

        _createBtn = root.Q<Button>("CreateBtn");
        _createBtn.clicked += CreateEvent;
        _refreshBtn = root.Q<Button>("RefreshBtn");
        _refreshBtn.clicked += RefreshEvent;

        _leftList = root.Q<ListView>("LeftListView");
        
        _leftList.onSelectionChange += OnSelectChange;

        _nameText = root.Q<TextField>("NameTextField");
        _posText = root.Q<Vector3Field>("PosField");
       //数据绑定
        _nameText.bindingPath = "m_Name";
        _posText.bindingPath = "m_LocalPosition";

        //嵌入IMGUI 
        rightPart.Add(new IMGUIContainer(() =>
        {
            if (GUILayout.Button("TestBtn"))
            {
                Debug.Log("TestLog");
            }
            
        }));

        //计时器(默认 打开界面 就会执行)
        var scheduleItem = root.schedule.Execute(() =>
        {
            Debug.Log("Schedule Event");
        });

        //间隔2秒执行一次
        //scheduleItem.Every(2000);
        //打开页面之后延迟两秒执行
        //scheduleItem.ExecuteLater(2000);
    
    }

    private void OnSelectChange(IEnumerable<object> obj)
    {
        foreach (var item in obj)
        {
            GameObject go = item as GameObject;
            Selection.activeGameObject = go;
            SerializedObject serializedObj = new SerializedObject(go);
            _nameText.Bind(serializedObj);
            SerializedObject serializedTrans = new SerializedObject(go.transform);
            _posText.Bind(serializedTrans);
        }
    }

    private void BindListItem(VisualElement arg1, int index)
    {
        Label label = arg1 as Label;
        var go = _objs[index];
        label.text = go.name;
    }

    private VisualElement MakeListItem()
    {
        var label = new Label();
        label.style.unityTextAlign = TextAnchor.MiddleCenter;
        label.style.marginLeft = 5;
        return label;
    }

    private void CreateEvent()
    {
        if (_objFiled.value == null)
        {
            return;
        }
        GameObject prefab = _objFiled.value as GameObject;
        GameObject obj = Instantiate(prefab);
        obj.transform.position = new Vector3(Random.Range(0, 10), 0, Random.Range(0, 10));
    }

    private void RefreshEvent()
    {
        Scene currentScene = SceneManager.GetActiveScene();
        _objs = currentScene.GetRootGameObjects();
        _leftList.itemsSource = _objs;
        _leftList.makeItem = MakeListItem;
        _leftList.bindItem = BindListItem;
    }

   

同时UIToolKit 也支持 脚本编辑器扩展:

[CustomEditor(typeof(TestCube))]
public class TestCubeEditor : Editor
{

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
    }

    //该函数优先级大于上面
    public override VisualElement CreateInspectorGUI()
    {
        //return base.CreateInspectorGUI();
        VisualElement root = new VisualElement();
        Button testBtn = new Button();
        testBtn.style.width = 200;

        root.Add(testBtn);

        Label testLabel = new Label("TestLabel");

        root.Add(testLabel);

        Toggle tog = new Toggle();
        root.Add(tog);

        return root;
    }

}

UI Tool Kit 使用,unity,UIToolKit,UIBuilder文章来源地址https://www.toymoban.com/news/detail-521513.html

到了这里,关于UI Tool Kit 使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • FlutKit v15.0 – Flutter UI Kit Crack

    FlutKit v15.0 – Flutter UI Kit Crack

    FlutKit  is a nicely designed and developed multi-purpose mobile application UI Kit developed using Flutter. Flutter is an open-source mobile application development SDK created by Google and used to develop applications for Android and iOS. FlutKit  makes it an easy job for developers to have the modern look and feel in the mobile application. It saves lo

    2024年02月09日
    浏览(8)
  • 教你如何使用GPA导出模型,另送一个 GPA CSV2MESH Tool in unity

    教你如何使用GPA导出模型,另送一个 GPA CSV2MESH Tool in unity

    以前写过一篇:Unity - RenderDoc 抓帧导出 FBX(带UV) 我估计GPA是怕收律师函,因为如果 GPA 将所有资源一键提取,一键导出,那么可能很多开发商会告他 可以看到也好几个帖子问 GPA 官方,都是被官方忽悠回答了: UV MAPS capture GPA: No UV map support? (除了这个,我自己还搜索过还

    2023年04月09日
    浏览(36)
  • Unity5.4.1打砖块游戏Breakout_Game_Starter_Kit

    Unity5.4.1打砖块游戏Breakout_Game_Starter_Kit

    Unity5.4.1打砖块游戏Breakout_Game_Starter_Kit 童年的回忆   项目地址:https://download.csdn.net/download/Highning0007/88042779Unity游戏源码分享-

    2024年02月15日
    浏览(19)
  • Unity GPU Skinning Tool: 提升3D游戏动画性能的新利器

    项目地址:https://gitcode.com/ForeverZack/Unity-Gpu-Skinning-Tool 在Unity引擎的世界里,Unity GPU Skinning Tool是一个强大的工具,它将骨骼动画计算从CPU转移到GPU,显著提高了游戏中的角色动画性能。对于那些需要大量3D角色与复杂动画的游戏开发者而言,这是一个非常值得尝试的技术。 Un

    2024年04月25日
    浏览(13)
  • 【Unity 踩坑系列】VScode的csproj文件路径失效more errors occurred. (This project is not supported in C# Dev Kit.)

    【Unity 踩坑系列】VScode的csproj文件路径失效more errors occurred. (This project is not supported in C# Dev Kit.)

    在使用VScode配置Unity的开发环境时候遇到了以下的问题: 在VScode打开Unity项目中的脚本时在Project OutPut里会报出一大堆csproj文件路径失效的报错 [warning] The project file ‘d:UnityProjectUnity.Services.Core.Configuration.csproj’ is in unsupported format (for example, a traditional .NET Framework project). It

    2024年02月07日
    浏览(42)
  • 使用Unity生成UI预制体 (Unity3D)

    在Unity中,预制体(Prefab)是一种非常有用的工具,用于生成可重复使用的UI元素。预制体使得UI的创建和管理变得更加简单和高效。在本文中,我们将详细介绍如何使用Unity生成UI预制体,并提供相应的源代码示例。 步骤1:创建UI元素 首先,我们需要创建UI元素,例如按钮、

    2024年02月05日
    浏览(51)
  • Unity使用GUI封装一个UI系统

    Unity使用GUI封装一个UI系统

    Untiy中设计UI不会使用GUI,因为它必须要运行代码才可以看出UI的布局结果,而且GUI不支持屏幕分辨率自适应,所以一般会使用UGUI等设计,但是为了搞清楚高级UI的原理,通过GUI去设计一个类似于UGUI的工具是个很好的学习方法。所以本文属于是脱裤子放屁,但是只有脱下裤子把

    2024年02月04日
    浏览(11)
  • Unity UI表格布局组件+滑动组件组合使用

    Unity UI表格布局组件+滑动组件组合使用

    Grid Layout Group: 表格(网格)布局组件,可以让数据按表格的形式排列。 Padding:控制所有子物体的整体的外边距。 Cell Size:子物体尺寸; Spacing:子物体之间的间距; Start Corner:子物体开始的角度位置; Start Axis:子物体开始的轴向; Child Alignment:子物体对其方式; Constrain

    2024年02月04日
    浏览(14)
  • unity 使用OnMouseDown函数UI点击穿透的问题

    OnMouseDown函数挂载在物体上,点击直接响应函数,使用起来十分快捷方便,但是一直有UI穿透的问题。 通常解决UI穿透的问题是加EventSystem.current.IsPointerOverGameObject()来判断是否点击在UI上,但是在OnMouseDown函数中,这行代码似乎是失效的。 解决这个问题的方法就是: 不用OnMou

    2024年02月04日
    浏览(14)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包