【发布时间】:2019-10-11 10:06:35
【问题描述】:
我正在制作一个系统来平衡 EditorWindow 的 OnGUI 方法内部的调用。 我正在执行以下操作:
public void Update()
{
Repaint();
}
在我的 OnGUI 方法中,我调用了这个 Balancer。我有一个带有回调的列表(列表)。 所以思路很简单,调用vaxc 我正在为具有完整 GUI 的回调跳过一些重绘帧,并为其他回调调用每次重绘(例如,选取框标签或显示 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 内):
private Balancer m_balancer;
public void OnEnable()
{
m_balancer = new Balancer(Draw);
}
public void Draw()
{
// This block will be called every 20 repaints as specified on the if statment
GUILayout.BeginHorizontal("box");
{
GUILayout.Button("I'm the first button");
GUILayout.Button("I'm to the right");
// This marquee will be called on each repaint
m_balancer.AddAction(() => CustomClass.DisplayMarquee("example"));
}
GUILayout.EndHorizontal();
}
// Inside of the Balancer class we have
// We use System.Linq.Expressions to identify actions that were added already
private HashSet<string> m_alreadyAddedActions = new HashSet<string>();
public void AddAction(Expression<Action> callback)
{
if(!m_alreadyAddedActions.Add(callback.ToString()))
return;
m_actions.Add(callback.Compile());
}
我想不通。我在互联网上找不到任何信息。谁能帮帮我?
【问题讨论】:
标签: c# user-interface unity3d balance