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即可获取具体点击的是哪个物体。
这个方法是为了在不同的按钮使用同一点击事件时非常有效。
UGUI获取当前的点击对象
EventSystem.current.currentSelectedGameObject 需引用命名空间
using UnityEngine.EventSystems;
需要在对应UI上添加 Selectable 组件。
//GameObject obj = EventSystem.current.currentSelectedGameObject;//只对UGUI起作用
//GameObject obj = GameObject.FindWithTag("Sprite");//当有多个对象时默认点击的是第一个
//bool b = UICamera.Raycast(Input.mousePosition);//射线检测 为bool值判断
//GameObject obj = UICamera.currentTouch.current;//对NGUI器作用