Unity UI穿透问题 含多种形式触发

using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static UnityEngine.EventSystems.ExecuteEvents;

namespace Speeic
{
    //UI穿透脚本
    //添加obj
    //绑定此脚本
    internal class SpeeicView : MonoBehaviour, IPointerClickHandler, IPointerUpHandler, IPointerDownHandler,IBeginDragHandler, IEndDragHandler,IDragHandler,IDropHandler
    {
        public bool IsDrag = false;//是否拖动
        public GameObject BeginObj;//初移动物体
        public GameObject EndObj;//
        public void OnPointerClick(PointerEventData eventData)
        {
            if (IsDrag) { return; }
            Debug.Log("VVVS");
            PassEvent(eventData, pointerClickHandler);
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (IsDrag) { return; }
            Debug.Log("VVVS2");
            PassEvent(eventData, pointerUpHandler);
        }
        private void PassEvent<T>(PointerEventData data, EventFunction<T> function,int type=0) where T : IEventSystemHandler
        {
            //Debug.Log("进入事件");
            var results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(data, results);
            var current = data.pointerCurrentRaycast.gameObject;
            for (int a = 0; a < results.Count; a++)
            {
                if (results[a].gameObject != current)
                {
                    //Debug.Log(results[a].ToString());
                    if (type == 1)
                    {
                        BeginObj = results[a].gameObject;
                    }
                    if (type == 2)
                    {
                        EndObj = results[a].gameObject;
                    }
                    if (BeginObj != EndObj&&type==0)
                    {
                        return;
                    }
                    if (type == 99)
                    {
                        ExecuteEvents.ExecuteHierarchy(BeginObj, data, function);
                    }
                    else if(type ==2)
                    {
                        ExecuteEvents.ExecuteHierarchy(BeginObj, data, function);
                    }
                    else
                    {
                        ExecuteEvents.ExecuteHierarchy(results[a].gameObject, data, function);
                    }
                    break;
                }
            }
        }
        public Transform GetParentTr<T>(Transform child)
        {
            if (child.GetComponent<T>() != null)
            {
                return child;
            }
            return GetParentTr<T>(child);
        }
        public void OnPointerDown(PointerEventData eventData)
        {
            if (IsDrag) { return; }
            Debug.Log("VVVS3");
            PassEvent(eventData, pointerDownHandler);
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            Debug.Log("VVVS4");
            IsDrag=true;
            PassEvent(eventData, beginDragHandler,1);

        }

        public void OnEndDrag(PointerEventData eventData)
        {
            IsDrag=false;
            Debug.Log("VVVS5");
            PassEvent(eventData, endDragHandler,2);
            BeginObj = null;
            EndObj = null;
        }

        public void OnDrag(PointerEventData eventData)
        {
            //Debug.Log("VVVS6");
            PassEvent(eventData, dragHandler,99);
        }

        public void OnDrop(PointerEventData eventData)
        {
            if (IsDrag) { return; }
            Debug.Log("VVVS7");
            //PassEvent(eventData, dropHandler);
        }
    }
}

目前问题有:拖动超出屏幕时滚动视图会停止。焦点或触摸还在设备中正常
Tips:热更结构,需将脚本放在主工程中

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值