0

I have some problem with binding ngui events in the StrangeIoC framework.

This is an unityGUI sample:

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:

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. I noticed in the scene the Notify property on that button has an empty value.

This one works, but the buttonClicked is triggered several times :(

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? Thanks!

Rune Vejen Petersen
  • 3,201
  • 2
  • 30
  • 46
Sean C.
  • 1,494
  • 1
  • 13
  • 22

1 Answers1

0

I finally figured out. Add the delegate in Awake instead of 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);
    }
}
Sean C.
  • 1,494
  • 1
  • 13
  • 22