找回密码
 立即注册
查看: 332|回复: 0

[笔记] Unity四叉树与编辑器拓展

[复制链接]
发表于 2022-5-10 12:15 | 显示全部楼层 |阅读模式
性能大概不是最优的一种写法,但是数据结构表现是这么个意思。参考文章
补充:四叉树是一种 2D 空间性能优化的数据结构方案,用于检索可视区域内空间子对象。在同一时刻下,减少多数无效数据。对场景内有大量物体,并且地图范围较广的项目具有良好的优化表现。存储结构如下图所示。


这次四叉树结构只写了两个小时,但是 Unity 编辑器拓展表现写了两天。T_T菜啊我。属于是重复造轮子了。之前做过一个曲线编辑器的内容,因为代码不够好拓展,所以索性这次直接重写了一套。曲线编辑器长这个样子。


可以在曲线路径上根据迭代次数 iter 填充表现物 prefab 的 2D 自动路线生成器。
这次的四叉树长这样(如下图);支持深度设置,支持动态检索,支持绘制。


工程支持一些基本的图形绘制 square 和 circle 等,point 点支持 mouse 的响应事件。
如果需要拓展,可以参考 editor 里面的代码。
    // QuadTreeUtil.cs 脚本
    /// <summary>
    /// 响应鼠标事件
    /// </summary>
    private void handleInput(Event guiEvent)
    {
        float drawPlaneHeight = 0;
        Ray mouseRay = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition);
        float dstToDrawPlane = (drawPlaneHeight - mouseRay.origin.z) / mouseRay.direction.z;
        Vector3 pos = mouseRay.GetPoint(dstToDrawPlane);

        if (guiEvent.type == EventType.MouseDown)
        {
            foreach (var point in Point.Caches)
            {
                point.OnMouseDown?.Invoke(guiEvent, pos);
            }
            
            isDirty = true;
        }
        else if (guiEvent.type == EventType.MouseUp)
        {
            foreach (var point in Point.Caches)
            {
                point.OnMouseUp?.Invoke(guiEvent, pos);
            }
            
            isDirty = true;
        }
        else if (guiEvent.type == EventType.MouseDrag)
        {
            foreach (var point in Point.Caches)
            {
                point.OnMouseDrag?.Invoke(guiEvent, pos);
                // Debug.Log("Drag--");
            }
            
            isDirty = true;
        }
        else if (guiEvent.type == EventType.MouseMove)
        {
            foreach (var point in Point.Caches)
            {
                // Debug.Log("pointPos: " + point.pos + "pos: " + pos);
                // Debug.Log("<color=\"00ff00\">dis: " + Vector3.Distance(point.pos, pos));
                if (Vector3.Distance(point.pos, pos) < point.radius)
                {
                    point.OnMouseIn?.Invoke();
                }
                else
                {
                    point.OnMouseOut?.Invoke();
                }
            }
        }
    }
这次重复造轮子,也让我理解到,积累的重要性。。。以后不想再写 graphic 框架了。
git 地址:https://gitee.com/colincollins/unity-editor-draw-lib
(写的不是很好,仅供参考吧)
感谢封面提供:久肆

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-3 03:41 , Processed in 0.095346 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表