
比如战斗场景,UI和3D场景同时都需要响应触摸事件,如果同时响应可能就会出现触摸UI的时候影响到了3D部分。为了解决这个问题在判断3D响应之前要先判断手指是否点击在UI上。 以前NGUI的时候都是自己来发送射线判断,现在UGUI好了系统提供了更为简便的方法。
#if UNITY_ANDROID && !UNITY_EDITOR
#define ANDROID
#endif
#if UNITY_IPHONE && !UNITY_EDITOR
#define IPHONE
#endif
using UnityEngine;
using UnityEngineUI;
using SystemCollections;
using UnityEngineEventSystems;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
void Update()
{
if (InputGetMouseButtonDown(0)||(InputtouchCount >0 && InputGetTouch(0)phase == TouchPhaseBegan))
{
#if IPHONE || ANDROID
if (EventSystemcurrentIsPointerOverGameObject(InputGetTouch(0)fingerId))
#else
if (EventSystemcurrentIsPointerOverGameObject())
#endif
DebugLog("当前触摸在UI上");
else
DebugLog("当前没有触摸在UI上");
}
}
}
void Update () {
if(InputGetMouseButtonDown(0))
{
RayControl();
}
if(flagMove)
{
if(Vector3Distance(transformposition,mousePos)>1)
{
transformTranslate(transformworldToLocalMatrix transformforward TimedeltaTime5);//transformforward是世界坐标,通过transformworldToLocalMatrix转换矩阵转到本地坐标然后在本地坐标运动,没有必要必须在本地坐标系运动 但是必须注意要统一起来。
}
else
{
flagMove=false;
}
}
}
void RayControl()
{
Ray ray=CameramainScreenPointToRay(InputmousePosition);//向屏幕发射一条射线
if(PhysicsRaycast(ray,out hit,200))射线长度为200 和地面的碰撞盒做检测
{
GameObject targetPos=GameObjectCreatePrimitive(PrimitiveTypeSphere);//实例化一个Sphere
targetPostransformlocalScale=new Vector3(05f,05f,05f);
mousePos=hitpoint;//获取碰撞点坐标
mousePosy=transformpositiony;
targetPostransformposition=mousePos;//Sphere放到鼠标点击的地方
targetDir=mousePos-transformposition;//计算出朝向
Vector3 tempDir=Vector3Cross(transformforward,targetDirnormalized);//用叉乘判断两个向量是否同方向
float dotValue=Vector3Dot(transformforward,targetDirnormalized);//点乘计算两个向量的夹角,及角色和目标点的夹角
float angle=MathfAcos(dotValue)MathfRad2Deg;if(tempDiry<0)//这块 说明两个向量方向相反,这个判断用来确定假如两个之间夹角30度 到底是顺时 还是逆时针旋转。
{
angle=angle(-1);
}
print(tempDiry);
print("2:"+angle);
transformRotateAround(transformposition,Vector3up,angle);
flagMove=true;
}
}
public class GetMousePos : MonoBehaviour
{
public Canvas canvas;//画布
private RectTransform rectTransform;//坐标
void Start()
{
canvas = GameObjectFind("Canvas")GetComponent<Canvas>();
rectTransform = canvastransform as RectTransform; //也可以写成thisGetComponent<RectTransform>(),但是不建议;
}
void Update()
{
if (InputGetMouseButtonDown(0))
{
Vector2 pos;
if (RectTransformUtilityScreenPointToLocalPointInRectangle(rectTransform, InputmousePosition, canvasworldCamera, out pos))
{
rectTransformanchoredPosition = pos;
DebugLog(pos);
}
}
}
}
新建场景,在场景中拖一个画布(Canvas),然后随便找个地方挂上这个脚本就好了。
1
RectTransformUtility:矩阵变换工具
RectTransformUtilityScreenPointToLocalPointInRectangle 从屏幕点到矩形内的本地点
Parameters 参数
rect The RectTransform to find a point inside
cam The camera associated with the screen space position
screenPoint Screen space position
localPoint Point in local space of the rect transform
Returns
bool Returns true if the plane of the RectTransform is hit, regardless of whether the point is inside the rectangle
Description 描述
Transform a screen space point to a position in the local space of a RectTransform that is on the plane of its rectangle
屏幕空间点转换为矩形变换内部的本地位置,该点在它的矩形平面上。
The cam parameter should be the camera associated with the screen point For a RectTransform in a Canvas set to Screen Space - Overlay mode, the cam parameter should be null
该cam 参数应该是该相机关联的屏幕点。对于在画布上的矩形变换设置该屏幕空间为-Overlay模式,cam 参数应该为空。
When ScreenPointToLocalPointInRectangle is used from within an event handler that provides a PointerEventData object, the correct camera can be obtained by using PointerEventDataenterEventData (for hover functionality) or PointerEventDatapressEventCamera (for click functionality) This will automatically use the correct camera (or null) for the given event
当ScreenPointToLocalPointInRectangle从事件处理器内部提供一个PointerEventData对象被使用时,相机可以通过使用PointerEventDataenterEventData(为悬停功能)或者 PointerEventDatapressEventCamera(为单击功能)被获取。该函数将会自动对指定事件使用正确的相机(或者空)。
RectTransform矩形变换
RectTransformanchoredPosition 锚点位置
The position of the pivot of this RectTransform relative to the anchor reference point
该矩形变换相对于锚点参考点的中心点位置。
The anchor reference point is where the anchors are If the anchors are not together, the four anchor positions are interpolated according to the pivot placement
锚点参考点是锚点的位置。如果锚点不在一起,四个锚点的位置是根据布置的中心点的位置插值替换的。
以上就是关于unity 怎么识别鼠标点击在ui上全部的内容,包括:unity 怎么识别鼠标点击在ui上、在unity3d中如何制作鼠标点击一个物体,然后传送到我想要的坐标上,也就是点击按钮,传送到目的地。、unity UGUI如何获取鼠标指针所在位置的UI对象等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)