判断鼠标触摸点是否在UI范围内(NGUI版)
using UnityEngine;
using System.Collections;
public class MouseTool
{
/// <summary>
/// 鼠标是否在UI范围内
/// </summary>
/// <param name="trans"></param>
/// <returns></returns>
public static bool IsTouchInUi(Transform trans)
{
bool isInRect = false;
float _mapWidth = trans.GetComponent<UIWidget>().width;//获取ui的实际宽度
float _mapHight = trans.GetComponent<UIWidget>().height;//长度
Vector3 newPos = trans.localPosition;
//注意,别用错方法了,只能是InverseTransformPoint
Vector3 mousePos = trans.parent.InverseTransformPoint(UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition));
if (mousePos.x < (newPos.x + _mapWidth / 2) && mousePos.x > (newPos.x - _mapWidth / 2) &&
mousePos.y < (newPos.y + _mapHight / 2) && mousePos.y > (newPos.y - _mapHight / 2))
{
isInRect = true;
}
return isInRect;
}
}