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 @ 2021-09-10 17:30  lunoctis  阅读(237)  评论(0编辑  收藏  举报