Execute NGUI button press from script in unity C#

667 views Asked by At

I have a NGUI button and it contains UIButton and UIToggle script. From another script i want to run its click event with all relevant button state changes like colour change etc. For this I found this link and here is my code.

   public UIButton userManagmentBtn;
    public void ClosePanel()
    {
        Debug.Log("Calling panel Colse");
        EventDelegate.Execute(userManagmentBtn.onClick);
    }

But the click event is not firing using this: EventDelegate.Execute

1

There are 1 answers

0
derHugo On

No NGUI expert but afaik to invoke all listeners you can simply call

userManagementBtn.OnClick();

Additionally for having the correct coloring etc you could call SetState

userManagementBtn.SetState(UIButtonColor.State.Pressed, true);

Alternatively you could try and send "manually" the according pointer events via ExecuteEvents.Execute like e.g.

var pointer = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(userManagementBtn.gameObject, pointer, ExecuteEvents.pointerEnterHandler);
ExecuteEvents.Execute(userManagementBtn.gameObject, pointer, ExecuteEvents.pointerClickHandler);