NGUI的UICamera中定义了一个内部类MouseOrTouch表示当前的输入设备:鼠标,Touch,或是游戏杆。
//begin源代码//
public class MouseOrTouch
{
public Vector2 pos; // Current position of the mouse or touch event
public Vector2 delta; // Delta since last update
public Vector2 totalDelta; // Delta since the event started being tracked
public Camera pressedCam; // Camera that the OnPress(true) was fired with
public GameObject current; // The current game object under the touch or mouse
public GameObject pressed; // The last game object to receive OnPress
public float clickTime = 0f; // The last time a click event was sent out
public ClickNotification clickNotification = ClickNotification.Always;
public bool touchBegan = true;
public bool pressStarted = false;
public bool dragStarted = false;
}
//end源代码//
在MouseOrTouch中定义了当前点击的物体:current
当我们点击某个物体时,只需要调用UICamera.currentTouch.current即可获取具体点击的是哪个物体。
这个方法是为了在不同的按钮使用同一点击事件时非常有效。
例如:在滚动列表子项中的所有按钮的响应事件都为OnUpgradeBtnClick。在OnUpgradeBtnClick中调用 UICamera.currentTouch.current即可获取我点击的具体按钮。从而知道他的数据:UIData uiData = curObject.transform.parent.GetComponent<UIData>();
for (int i = 0; i < list.Count; i++) {
SerEquip equip = list [i];
GameObject upgradeItem = NGUITools.AddChild (upGradeTable.gameObject, PrefabMgr.Instance ().GetPrefab ("GUI/Main/Equipment/Item/UpgradeItem"));
UIData uiData = upgradeItem.AddComponent<UIData> ();
uiData.Data = equip;
//UpgradePanel下的upgrade按钮的响应函数//
GameTools.EventClick (upgradeItem, "upGradeBtn", wndObject, "OnUpgradeBtnClick");
}
//升级面板 下upgrade按钮响应函数//
public void OnUpgradeBtnClick()
{
GameObject curObject = UICamera.currentTouch.current;
UIData uiData = curObject.transform.parent.GetComponent<UIData>();
WndMgr.Instance().ToggleWnd((int)UIType.Equip);
UpgradeWnd upgradeWnd = (UpgradeWnd)WndMgr.Instance().ShowWnd((int)UIType.Upgrade);
upgradeWnd.SetEquip((SerEquip)uiData.Data);
}