1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.EventSystems;
- public class UI_BuildInfo : MonoBehaviour
- {
- public int buildId;
- public Action<int> touchDownAC = null;
- public Action<int> touchUpAC = null;
- /// <summary>
- /// 按下
- /// </summary>
- private void OnMouseDown()
- {
- if (IsPointerOverUIObject())
- {
- return;
- }
- if (touchDownAC != null)
- {
- touchDownAC(buildId);
- }
- }
- /// <summary>
- /// 抬起
- /// </summary>
- private void OnMouseUp()
- {
- if (IsPointerOverUIObject())
- {
- return;
- }
- if (touchUpAC != null)
- {
- touchUpAC(buildId);
- }
- }
- /// <summary>
- /// 是点否在UI上
- /// </summary>
- /// <returns>结果</returns>
- private bool IsPointerOverUIObject()
- {
- PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
- eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
- List<RaycastResult> results = new List<RaycastResult>();
- EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
- return results.Count > 0;
- }
- }
|