更新水平Scrollview OnGUI

时间:2019-07-01 12:45:28

标签: c# unity3d

我想做的是当我单击onGUI按钮时,滚动视图viewRect y轴同时增加。这意味着,我想更新水平滚动视图。但是,我的代码无法正常工作。我该如何处理?

   private void OnGUI()
    {
        int subPartsSpacing = 0;
        float spacing = 30;
        float x = 7 + spacing;
        float y = 68;
        float scrollview_y = 236;

        HumanBodyPart mainBodyPart = bodyVisualizer.BodyData.Body.SubParts[0];
        List<HumanBodyPart> nextPartsToRender = new List<HumanBodyPart>(new HumanBodyPart[] { mainBodyPart });        
        while (nextPartsToRender.Count > 0)
        {

            HumanBodyPart currentPart = nextPartsToRender[0];
            nextPartsToRender.RemoveAt(0);

            scrollPosition = GUI.BeginScrollView(new Rect(7, 68, 236, 426), scrollPosition, new Rect(7, 68, 500, scrollview_y));
            GUI.Label(new Rect(currentPart.DrawDepth * spacing + x + subPartsSpacing, y, 200, 20), currentPart.EnglishTitle);

            if (currentPart.SubParts.Count != 0)
            {
                if (GUI.Button(new Rect(x - spacing + currentPart.DrawDepth * spacing + subPartsSpacing, y, 20, 20), "+"))
                {
                    if (!currentPart.IsExpanded)
                    {
                        currentPart.IsExpanded = true;
                        subPartsSpacing += 20;
                    }
                    else
                        currentPart.IsExpanded = false;
                }
                if (currentPart.IsExpanded)
                {
                    //The wrong part I guess...
                    scrollview_y += 20 * currentPart.SubParts.Count;
                    //
                    nextPartsToRender.InsertRange(0, currentPart.SubParts);
                }
            }
            y += spacing;
        }
        // End the scroll view that we began above.
        GUI.EndScrollView();
    }

GUI错误:您推送的GUIClip多于弹出的数量。确保它们平衡)

1 个答案:

答案 0 :(得分:0)

缩小到您正在做的事情

while (/*...*/)
{
    // ...
    scrollPosition = GUI.BeginScrollView(/*...*/);
    // ...
}
GUI.EndScrollView();

因此,您可能会在循环后收到多个BeginScrollView的调用,但只有一个EndScrollView的调用。

因此,要么将其移至while循环,以使每次BeginScrollView调用都会调用它

while (/*...*/)
{
    // ...
    scrollPosition = GUI.BeginScrollView(/*...*/);
    // ...
    GUI.EndScrollView();
}

或根据您的课程需求将两者都移到外面

scrollPosition = GUI.BeginScrollView(/*...*/);
while (/*...*/)
{
    // ...
}
GUI.EndScrollView();