UGUI研究院之判断鼠标或者手指是否点击在UI上(六)

比如战斗场景,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 UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
public class NewBehaviourScript : MonoBehaviour {
 
    // Use this for initialization
    void Start () {
    
    }
    
 
    void Update()
    {
        if (Input.GetMouseButtonDown(0)||(Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
#if IPHONE || ANDROID
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
            if (EventSystem.current.IsPointerOverGameObject())
#endif
                Debug.Log("当前触摸在UI上");
            
            else 
                Debug.Log("当前没有触摸在UI上");
        }
    }
}

这段代码我在Android上已经测试通过。 哦 我用的是unity5.1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Unity,可以使用`Input.GetMouseButton`来检测鼠标是否按下,并且可以通过`Camera.main`获取主摄像机。然后可以根据鼠标的滚轮值来控制相机的缩放比例,进而实现相机视角在物体上的缩放。 以下是一份示例代码: ```csharp using UnityEngine; public class ZoomOnObject : MonoBehaviour { public float zoomSpeed = 1.0f; public float minZoomDistance = 1.0f; public float maxZoomDistance = 10.0f; private float currentZoomDistance; private void Start() { currentZoomDistance = Vector3.Distance(Camera.main.transform.position, transform.position); } private void Update() { if (Input.GetMouseButton(2)) { float scrollValue = Input.GetAxis("Mouse ScrollWheel"); currentZoomDistance -= scrollValue * zoomSpeed; currentZoomDistance = Mathf.Clamp(currentZoomDistance, minZoomDistance, maxZoomDistance); } Vector3 cameraPosition = transform.position - Camera.main.transform.forward * currentZoomDistance; Camera.main.transform.position = cameraPosition; } } ``` 在这个示例代码,我们将脚本挂载到要缩放的物体上。`zoomSpeed`控制缩放速度,`minZoomDistance`和`maxZoomDistance`控制缩放距离的最小值和最大值。 在`Start`方法,我们根据相机和物体的初始距离计算出当前距离。 在`Update`方法,我们检测鼠标是否按下,然后根据鼠标滚轮值来更新当前距离。最后,我们通过计算相机的位置来实现相机视角在物体上的缩放。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值