NGUI中如何获取当前点击的物体

NGUI中如何获取当前点击的物体  

2013-05-14 17:19:46|  分类: Unity|举报|字号 订阅函数

NGUI的UICamera中定义了一个内部类MouseOrTouch表示当前的输入设备:鼠标,Touch,或是游戏杆。ui

//begin源代码//spa

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 trackedorm

  public Camera pressedCam;  // Camera that the OnPress(true) was fired withblog

  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;
 }ip

//end源代码//get

在MouseOrTouch中定义了当前点击的物体:currentit

当咱们点击某个物体时,只须要调用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);  }