[英]How to bind ngui events in StrangeIoC in unity3d
I have some problem with binding ngui events in the StrangeIoC framework. 我在StrangeIoC框架中绑定ngui事件有一些问题。
This is an unityGUI sample: 这是一个unityGUI示例:
public class TestView : View
{
private readonly Rect buttonRect = new Rect(0, 0, 200, 50);
public Signal buttonClicked = new Signal();
public void OnGUI()
{
if (GUI.Button(buttonRect, "Test"))
{
buttonClicked.Dispatch();
}
}
}
This is the NGUI version: 这是NGUI版本:
public class NGUIView : View
{
public UIButton Button;
public Signal buttonClicked = new Signal();
private void Start()
{
if (Button != null)
{
EventDelegate.Add(Button.onClick, buttonClicked.Dispatch);
}
}
}
In the NGUI version, the the buttonClicked is never dispatched. 在NGUI版本中,从不调度buttonClicked。 I noticed in the scene the Notify property on that button has an empty value. 我在场景中注意到该按钮上的Notify属性具有空值。
This one works, but the buttonClicked is triggered several times :( 这是可行的,但是buttonClicked被触发了几次:(
public class NGUIView : View
{
public UIButton Button;
public Signal buttonClicked = new Signal();
void Update()
{
if (Button.state == UIButtonColor.State.Pressed)
{
buttonClicked.Dispatch();
}
}
}
Could you kindly tell me how do you handle this NGUI-StrangeIoC situation? 您能告诉我如何处理这种NGUI-StrangeIoC情况吗? Thanks! 谢谢!
I finally figured out. 我终于想通了。 Add the delegate in Awake
instead of Start
. 在Awake
添加Awake
而不是Start
。
Now here's the perfect solution(not sure enough): 现在这是一个完美的解决方案(不够肯定):
public class TestView : View
{
private readonly Rect buttonRect = new Rect(0, 0, 200, 50);
public UIButton Button;
public Signal buttonClicked = new Signal();
private void OnGUI()
{
if (GUI.Button(buttonRect, "Test"))
{
buttonClicked.Dispatch();
}
}
private void Awake()
{
EventDelegate.Add(Button.onClick, buttonClicked.Dispatch);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.