kanzi插件之节点树可视化

背景:随着项目增大,项目中预设件增多,关联错综复杂,维护修改成本很高,需要一款可视化工具,来辅助开发。

1. 需求分析

  • 节点树可视化-卡片形式
  • 如果节点是Prefab View 2D或者Prefab Placeholder 2D类型,展开它的属性Prefab Template,支持递归
  • 如果节点里有状态机,状态机中设置了属性Prefab Template,也需要展开,支持递归
  • 卡片之间有连线,有上下级关系,状态机的要包围起来,表示对外只有一个节点
  • 左键单击选中卡片,kanzi studio焦点自动跳转
  • 右键拖拽视图
  • 滚轮缩放视图
  • 支持前进后退视图

2. 效果

Prefab View 2D

Prefab Placeholder 2D

状态机

3. 设计

3.1 kanzi窗口插件,名字PluginWinShowPrefabTree

如何创建插件,请参考以往博客

3.2 业务逻辑

  • 点击【Update】按钮,获取当前节点
c 复制代码
this.studio.SelectedItems.ToList();
  • 先判断有没有状态机
c 复制代码
if ("Node2DPrefabPlaceholderPluginWrapper" == node.GetType().Name ||
    "ComponentNode2DPluginWrapper" == node.GetType().Name)
{
    Node2D node2d = node as Node2D;
    if (node2d != null && node2d.PropertyTypes.NodeStateManager != null)
    {
        _NodeStateManager = node2d.Get(node2d.PropertyTypes.NodeStateManager);
    }
}
  • 获取状态机组StateGroup,获取组里的所有状态State,获取状态里的StateObject,获取其属性"PrefabViewConcept.Prefab",递归获取子节点DiguiHandleRefab

  • 如果没有状态机,递归获取子节点HandlePrefabResource,

c 复制代码
if ("Node2DPrefabPlaceholderPluginWrapper" == node.GetType().Name)
{
   var _Node2DPrefabPlaceholder = node as Node2DPrefabPlaceholder;
   if (_Node2DPrefabPlaceholder != null)
   {
       ResourceReference<Node2DPrefabTemplate> tempplateRef = _Node2DPrefabPlaceholder.Get(_Node2DPrefabPlaceholder.PropertyTypes.Node2DPrefabPlaceholderTemplate);
     
   }
}
else if ("ComponentNode2DPluginWrapper" == node.GetType().Name)
{
   var _ComponentNode2D = node as ComponentNode2D;
   if (_ComponentNode2D != null)
   {
       ResourceReference<PrefabTemplate> _PrefabViewConceptPrefab = _ComponentNode2D.Get(_ComponentNode2D.PropertyTypes.PrefabViewConceptPrefab);
      
   }
}
  • 最后遍历获取其子节点
c 复制代码
var childNodes = node.ChildNodes.ToList();
foreach (Node child in childNodes)
{
    var childTreeNode = DiguiHandleRefab(child, node);
    if (childTreeNode != null)
    {
        treeNode.Children.Add(childTreeNode);
    }
}
  • 获取的数据储存在树状节点里,claude生成ui代码
  • 点击卡片,跳转焦点
c 复制代码
//搜索screen
var pp = path.Replace(project.Screen.Path + "/", "");
ProjectItem target = project.Screen.GetProjectItemByPath(pp);
this.studio.Commands.SelectProjectItems(new List<ProjectItem> { target as ProjectItem });

//搜索预设件
var target = item.GetProjectItemByPath(childPath);
if (target != null)
{
    studio.Log($"[Select] Found prefab: {target.Name} (path: {target.Path})");
    this.studio.Commands.SelectProjectItems(new List<ProjectItem> { target as ProjectItem });
    return true;
}

搜索场景树的规则是

假如路径是 Screens/Screen1/RootPage/Empty Node 2D/Prefab View 2D

要这样搜 project.Screen.GetProjectItemByPath("RootPage/Empty Node 2D/Prefab View 2D");

搜索prefab的规则是

假如路径是 Prefabs/Text Block 2D_1/Text Block 2D_1/a

要这样搜 item.GetProjectItemByPath("Text Block 2D/a");

相关推荐
雨落倾城夏未凉5 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫6 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫7 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6257 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902117 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠8 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫10 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech11 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf12 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m62512 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#