Unity EditorWindow GUI裁剪

Unity2017,想在编辑器自己实现一个类似TreeView的东西

public void OnGUI(Rect rect)
{
    // ...
    for (int i = 0; i < 100; i++)
    {
        int row = Mathf.FloorToInt(i / visibleColumns);
        int column = i % m_VisibleColumns;

        float x = rect.x + padding.left + column * (cellSize.x + spacing.x);
        float y = rect.y + padding.top + row * (cellSize.y + spacing.y);
        Rect cellRect = new Rect(x, y - scrollPos, cellSize.x, cellSize.y);
        GUI.Box(cellRect, i.ToString());
    }
}

然后发现一个问题:在y < rect.y时,超出了预想的区域,却画了出来,在网上没有搜到,记录一下

查阅对应源码,通过反射调用GUIClip.Push(),GUIClip.Pop()即可

// 反射工具类
public static class GUIClip
{
    private static object CallReflection(string name, params object[] arguments)
    {
        Assmebly assembly = Assembly.Load("UnityEngine");
        Type type = assembly.GetType("UnityEngine.GUIClip");
        MethodInfo method = type.GetMethod(name, BindingFlags.Static | BindingFlags.NonPublick);
        return method.Invoke(null, arguments);
    }

    // Push a clip rect to the stack with pixel offsets.
    public static void Push(Rect screenRect, Vector2 scrollOffset, Vector2 renderOffset, bool resetOffset)
    {
        CallReflection("Push", screenRect, scrollOffset, renderOffset, resetOffset);
    }

    // Removes the topmost clipping rectangle, undoing the effect of the lastest GUIClip.Push
    public static void Pop()
    {
        CallReflection("Pop");
    }
}

一开始的OnGUI修改为

public void OnGUI(Rect rect)
{
    // ...
    GUIClip.Push(rect, Vector2.zero, Vector2.zero, false);
    for (int i = 0; i < 100; i++)
    {
        int row = Mathf.FloorToInt(i / visibleColumns);
        int column = i % m_VisibleColumns;

        // 在clip的处理过程中不需要再手动加上rect的位置了
        float x = padding.left + column * (cellSize.x + spacing.x);
        float y = padding.top + row * (cellSize.y + spacing.y);
        Rect cellRect = new Rect(x, y - scrollPos, cellSize.x, cellSize.y);
        GUI.Box(cellRect, i.ToString());
    }
    GUIClip.Pop();
}
```csharp
posted @   lunoctis  阅读(255)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示