#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上");
}
}
}
移动端与pc端的判断函数不同

本文介绍了一个Unity脚本示例,该脚本能够根据不同平台(iOS、Android及PC)的特点来判断用户的触摸事件是否发生在UI元素上。通过条件编译指令实现了移动端与PC端触摸事件的差异化处理。
6602

被折叠的 条评论
为什么被折叠?



