比较重要的代码
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using SpacePlanner.Core.DataStructure;
/// <summary>
/// 悬浮面板基类
/// </summary>
public class UIFloatPanel : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public bool isFixed;//是否固定位置,为false可被移动
public virtual void SetPanelData(object panelVO)
{
}
public virtual void OnBeginDrag(PointerEventData eventData)
{
if (isFixed) return;
}
public virtual void OnDrag(PointerEventData eventData)
{
if (isFixed) return;
Vector3 offset = new Vector3(eventData.delta.x, eventData.delta.y, 0);
CanvasScaler canvasScaler = UIManager.Instance.GetComponent<CanvasScaler>();
if (canvasScaler.uiScaleMode == CanvasScaler.ScaleMode.ScaleWithScreenSize)
{
float resolutionX = canvasScaler.referenceResolution.x;
float resolutionY = canvasScaler.referenceResolution.y;
float rate = (Screen.width / canvasScaler.referenceResolution.x) * (1 - canvasScaler.matchWidthOrHeight) + (Screen.height / canvasScaler.referenceResolution.y) * canvasScaler.matchWidthOrHeight;
offset /= rate;
}
transform.localPosition += offset;
}
public virtual void OnEndDrag(PointerEventData eventData)
{
if (isFixed) return;
}
public virtual void Close()
{
Destroy(transform.parent.gameObject);
}
}
遮罩面板
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 遮罩面板,处于所有面板最低部,用于监听并传递鼠标事件
/// </summary>
[ExecuteInEditMode]
public class Mask : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public UIFloatPanel floatPanel;
public GameObject dragObject;
void Start()
{
if (!floatPanel)
floatPanel = transform.parent.GetComponentInChildren<UIFloatPanel>();
}
public void OnPointerClick(PointerEventData eventData)
{
EventSystem.current.PropgateEvent(eventData, ExecuteEvents.pointerClickHandler, gameObject);
if (floatPanel && dragObject == null)
floatPanel.Close();
}
public void OnBeginDrag(PointerEventData eventData)
{
if (dragObject != null)
ExecuteEvents.Execute<IBeginDragHandler>(dragObject, eventData, ExecuteEvents.beginDragHandler);
dragObject = EventSystem.current.PropgateEvent(eventData, ExecuteEvents.beginDragHandler, gameObject, dragObject);
if (floatPanel)
floatPanel.gameObject.SetActive(false);
}
public void OnDrag(PointerEventData eventData)
{
if (dragObject != null)
ExecuteEvents.Execute<IDragHandler>(dragObject, eventData, ExecuteEvents.dragHandler);
EventSystem.current.PropgateEvent(eventData, ExecuteEvents.dragHandler, gameObject, dragObject);
//if (floatPanel)
// floatPanel.Close();
}
public void OnEndDrag(PointerEventData eventData)
{
if (dragObject != null)
ExecuteEvents.Execute<IEndDragHandler>(dragObject, eventData, ExecuteEvents.endDragHandler);
EventSystem.current.PropgateEvent(eventData, ExecuteEvents.endDragHandler, gameObject, dragObject);
if (floatPanel)
floatPanel.Close();
}
public void OnPointerDown(PointerEventData eventData)
{
EventSystem.current.PropgateEvent(eventData, ExecuteEvents.pointerDownHandler, gameObject);
}
}
遮罩面板的重写 点击传递事件
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public static class EventSystemExtension
{
/// <summary>
/// 把事件继续往后面传递,需要给定类型,这里比较需要注意的就是你传出来的跟你希望响应的事件类型需要一致的
/// 现在的做法是先获得所有的raycast这里其实是有优化方法的
/// 然后从from开始(假如能找到),发送事件
/// 示例EventSystem.current.PropgateEvent(eventData, ExecuteEvents.pointerClickHandler);
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="es"></param>
/// <param name="eventData"></param>
/// <param name="functor">参考示例</param>
/// <param name="from">从哪个物体开始计算</param>
/// <param name="to">强制指定只能发送给谁</param>
/// <param name="count">最多传递多少次,默认一次</param>
public static GameObject PropgateEvent<T>(this EventSystem es,
PointerEventData eventData, ExecuteEvents.EventFunction<T> functor,
GameObject from,
GameObject to = null, int count = 1) where T : IEventSystemHandler
{
List<RaycastResult> raycastResults = new List<RaycastResult>();
es.RaycastAll(eventData, raycastResults);
int fromIdx = 0;
for (int i = 0; i < raycastResults.Count; i++)
{
if (from == raycastResults[i].gameObject)
fromIdx = i + 1;
}
if (fromIdx == 0)
return null;
GameObject target = null;
int tick = 0;
for (; fromIdx < raycastResults.Count; fromIdx++)
{
if (tick >= count)
break;
GameObject go = raycastResults[fromIdx].gameObject;
if (to != null && go != to)
continue;
bool flag = ExecuteEvents.Execute<T>(go, eventData, functor);
if (flag)
{
target = go;
tick++;
}
}
return target;
}
}
网友评论