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:热更结构,需将脚本放在主工程中