unity3d 平衡EditorWindow OnGUI方法内部的调用导致问题

x6yk4ghg  于 2023-02-13  发布在  其他
关注(0)|答案(1)|浏览(180)

我正在创建一个系统来平衡EditorWindow的OnGUI方法内部的调用。我正在做以下工作:

public void Update()
        {
            Repaint();
        }

在我的OnGUI方法里面我调用这个Balancer.我有一个回调列表(List).所以这个想法很简单,有些callvaxc我跳过了一些repaint帧,为有完整GUI的回调调用,并为其他回调调用每个repaint(例如,一个字幕标签或显示gif).
由于某种原因,发生了此错误"重新绘制时,在仅包含0个控件的组中获取控件0的位置"

private int m_repaintCounter;

        public void Draw()
        {
            Event e = Event.current;
            try
            {
                foreach (var action in m_actions)
                {
                    try
                    {

                            // Test 1
                            // MainAction is a class that inherits from Action (class MainAction : Action)
                            if (action is MainAction)
                            {
                                bool isDesignedType = e.rawType == EventType.Repaint || e.rawType == EventType.Layout;

                                if (isDesignedType)
                                    ++m_repaintCounter;

                                if (!(m_repaintCounter == 20 && isDesignedType))
                                    continue;
                                else
                                    m_repaintCounter = 0;
                            }

                            // Test 2
                            action.Value();
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }
            }
            catch
            {
                // Due to recompile the collection will modified, so we need to avoid the exception
            }
        }

但如果我评论"测试1",一切都很好。
在类的ctor上,我们需要指定对GUI方法的回调,例如:

public Balancer(Action drawAction)
        {
            m_actions = new List<Action>();
            m_actions.Add(drawAction);
        }

因此,我们可以轻松地(在EditorWindow内部):
一个三个三个一个
我想不出来。我在网上找不到任何信息。有人能帮我吗?

8yparm6h

8yparm6h1#

好吧,OnGui(IMGui)很难用,如果你不想用它来编辑脚本,那就用新的4.6 UI(UGui)来代替。

好了,问题来了。
OnGui每帧至少被调用两次,其中一次是计算布局,另一次是实际绘制东西(“重画”)。

  • 如果在这两次调用之间事物的数量、大小或其他任何东西发生了变化 *,Unity将错误显示“在进行重绘时,获取控件0在仅有0个控件的组中的位置”。

也就是说:在Layout之后和Repaint之前,您无法在IMGui系统中更改UI状态。
仅,仅,***仅***更改状态(从而绘制哪些对象),并且仅,仅,***仅***改变下一帧的状态(或者,在Event.current == EventType.Layout期间进行更改,前提是此相同的新状态将在重画期间导致相同的代码路径)。在任何情况下,在重画过程中进行先前布局中不存在的更改。

相关问题