NGUI butten 點選事件監聽
阿新 • • 發佈:2019-02-01
1.直接監聽 用的少
void OnClick()
{
Debug.Log("Button is Click!!!");
}
2.新增事件監聽指令碼UIEventTriggert
Component—>NGUI —>interactiont—>UIEventTriggert 這樣就能新增Ui內部行為事件監聽 interactiont會監聽次遊戲體的行為,自動觸發相應的事件,只需將該事件發生時需要執行的遊戲體拖到相應的位置,選擇相應的方法,值得注意的是 該方法必須是public 型別。
3.使用 UIEventListener
Component—>NGUI —>interactiont—>UIEventListener (此指令碼沒有必要新增到“button”遊戲體物件上,可以進原始碼檢視,自動新增的)
UIEventListener 可以監聽的事件和UIEventTriggert是一樣多的void Start () { obj = GameObject.Find("Button"); UIEventListener.Get(obj).onClick = ButtonClick; } //計算按鈕的點選事件 void ButtonClick(GameObject button) { GameModel.getInstance().scencemodel.level = "play"; obj.GetComponent<ClickIn>().OnclickIn();// }
public delegate void VoidDelegate (GameObject go);
public VoidDelegate onSubmit;
public VoidDelegate onClick;
public VoidDelegate onDoubleClick;
public BoolDelegate onHover;
public BoolDelegate onPress;
public BoolDelegate onSelect;
public FloatDelegate onScroll;
public VoidDelegate onDragStart
public VectorDelegate onDrag;
public VoidDelegate onDragOver;
public VoidDelegate onDragOut
;public VoidDelegate onDragEnd;
public ObjectDelegate onDrop;
public KeyCodeDelegate onKey;
public BoolDelegate onTooltip;
實際上onXXXX都是 VoidDelegate 控制代碼 函式方法指標 當然也有個別的需要更多的引數也就是不一樣了
當遊戲體Butten觸發點選事件時 ButtenClick 自動觸發,總的來說這種方法不適合用GameObject.Find(),倘若這是在同一個Window下的內部事件 強烈建議使用外部拖拽賦值實現。
4.使用EventDelegate
此類並沒有繼承自MonoBehaviour 古在選單項不能找到他,進入原始碼你會發現其實就是對Delegate 的封裝,讓其更加安全可靠,建議使用(此指令碼不方便監聽NGUI事件,用的更多是遊戲體之間的傳遞,或者直接和UImanager 打交道)。
public UIPopupList UserList=null;
public void Start () {
Lsb=gameObject.GetComponent<LoginSenceBusiness>();
EventDelegate.Add(UserList.onChange, setlogin);
}
void setlogin(){
if (UserList.value == "other") {
setLoginPanelTrue();
GuestUserLoginPanel.transform.position=new Vector3(500,0,0);
}
}
UserList 在外邊賦值,當其 onChange 事件出發時 自動執行setlogin 方法
public delegate void Callback();
static public EventDelegate Add (List<EventDelegate> list, Callback callback)
這是原始碼 不難看出 它只是將 callback 放進了 list。原始碼還有其他的過載方法,滿足使用者不同需求。EventDelegate還有其他的用法比喻
void OnMouseUpAsButton()
{
if (mousePressTime > 0.2) {
return;
}
if (UICamera.hoveredObject==null) {
scaleEffect.Play(click);
}
}
void OnMouseDrag(){
mousePressTime += Time.fixedDeltaTime;
}
void OnMouseDown(){
mousePressTime = 0;
}
public void click()
{
GameData.getInstance ().M_ui_data.M_BuildingData.CurrentBuilding = m_BuildingName;
OperateWindowSystem.Instance.ShowUI();
EventDelegate.Execute(ClickedEvent);
}
public List<EventDelegate> ClickedEvent = new List<EventDelegate>();
輸入Size 拖進遊戲體 MMethod Name是MethodInfo 的例項,涉及到對映,筆者能力有限,目測在這個地方 輸入方法名就可以了,此方法不管是公有還是私有都能被呼叫。