Unity EventSystem.current.IsPointerOverGameObject ()

本文介绍了一个Unity脚本,用于实现通过触控或鼠标输入来旋转游戏中的物体。该脚本支持移动设备和桌面平台,能检测手指触摸或鼠标移动,并根据输入的方向和速度旋转指定的游戏对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//返回一个布尔值,进入了UI上就返回true,用的时候要 using UnityEngine.EventSystems;

EventSystem.current.IsPointerOverGameObject ()

 

旋转一个物体

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Linq;


public class TouchController : MonoBehaviour
{
    public GameObject Cube;
    public float Speed = 0.1f;

    void Update()
    {
#if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)

            //Touch
            int touchCount = Input.touchCount;
        
            if (touchCount == 1)
            {            
                Touch t = Input.GetTouch (0);
                if (EventSystem.current.IsPointerOverGameObject (t.fingerId))
                    return;
            
                switch (t.phase)
                {
                case TouchPhase.Moved:
                
                    float xAngle = t.deltaPosition.y * Speed;
                    float yAngle = -t.deltaPosition.x * Speed;
                    float zAngle = 0;
                
                    Cube.transform.Rotate (xAngle, yAngle, zAngle, Space.World);
                
                    break;
                }            
            }

#else
        //Mouse
        if (Input.GetMouseButton(0))
        {
            if (EventSystem.current.IsPointerOverGameObject())
                return;

            float xAngle = Input.GetAxis("Mouse Y") * Speed * 80;
            float yAngle = -Input.GetAxis("Mouse X") * Speed * 80;
            float zAngle = 0;

            Cube.transform.Rotate(xAngle, yAngle, zAngle, Space.World);
        }
#endif
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值