227 lines
5.6 KiB
C#
227 lines
5.6 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Assembly : Unity
|
|||
|
// Author : KimCh
|
|||
|
// Created :
|
|||
|
//
|
|||
|
// Last Modified By : KimCh
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "KHighLightMask" company=""></copyright>
|
|||
|
// <summary></summary>
|
|||
|
// ***********************************************************************
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.EventSystems;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
public class KHighLightMask : Graphic, /*ICanvasRaycastFilter,*/ IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
|
|||
|
{
|
|||
|
public Button.ButtonClickedEvent maskClicked = new Button.ButtonClickedEvent();
|
|||
|
|
|||
|
public RectTransform targetRectTransform1;
|
|||
|
|
|||
|
private Vector3 _targetPosition1 = Vector3.zero;
|
|||
|
|
|||
|
private Vector2 _targetSize1 = Vector2.zero;
|
|||
|
|
|||
|
private bool _isValid;
|
|||
|
|
|||
|
private Vector3 _scaleF;
|
|||
|
|
|||
|
readonly Vector3[] _targetCornels = new Vector3[4];
|
|||
|
readonly Vector3[] _thisCornels = new Vector3[4];
|
|||
|
|
|||
|
protected override void Start()
|
|||
|
{
|
|||
|
_scaleF = rectTransform.lossyScale;
|
|||
|
this.SetAllDirty();
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
if (!Application.isPlaying || !KUIRoot.Instance.uiCamera)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (this.targetRectTransform1 == null || !this.targetRectTransform1.gameObject.activeInHierarchy)
|
|||
|
{
|
|||
|
if (_isValid)
|
|||
|
{
|
|||
|
_isValid = false;
|
|||
|
this.SetAllDirty();
|
|||
|
}
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (!_isValid)
|
|||
|
{
|
|||
|
_isValid = true;
|
|||
|
this.SetAllDirty();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (_targetPosition1 != this.targetRectTransform1.position || _targetSize1 != this.targetRectTransform1.rect.size)
|
|||
|
{
|
|||
|
_targetPosition1 = this.targetRectTransform1.position;
|
|||
|
_targetSize1 = this.targetRectTransform1.rect.size;
|
|||
|
this.SetAllDirty();
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
|
|||
|
{
|
|||
|
if (this.targetRectTransform1 == null || !this.targetRectTransform1.gameObject.activeInHierarchy)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
bool flag = RectTransformUtility.RectangleContainsScreenPoint(this.targetRectTransform1, sp, eventCamera);
|
|||
|
return !flag;
|
|||
|
}
|
|||
|
|
|||
|
public void OnPointerDown(PointerEventData eventData)
|
|||
|
{
|
|||
|
//PassEvent(eventData, ExecuteEvents.pointerDownHandler);
|
|||
|
}
|
|||
|
|
|||
|
//监听抬起
|
|||
|
public void OnPointerUp(PointerEventData eventData)
|
|||
|
{
|
|||
|
//PassEvent(eventData, ExecuteEvents.pointerUpHandler);
|
|||
|
}
|
|||
|
|
|||
|
//监听点击
|
|||
|
public void OnPointerClick(PointerEventData eventData)
|
|||
|
{
|
|||
|
if (!IsRaycastLocationValid(eventData.position, KUIRoot.RootCamera))
|
|||
|
{
|
|||
|
maskClicked.Invoke();
|
|||
|
PassEvent(eventData, ExecuteEvents.pointerClickHandler);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
readonly List<RaycastResult> _raycastResults = new List<RaycastResult>();
|
|||
|
//把事件透下去
|
|||
|
public void PassEvent<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function)
|
|||
|
where T : IEventSystemHandler
|
|||
|
{
|
|||
|
_raycastResults.Clear();
|
|||
|
List<RaycastResult> results = _raycastResults;
|
|||
|
EventSystem.current.RaycastAll(data, results);
|
|||
|
GameObject current = data.pointerCurrentRaycast.gameObject;
|
|||
|
for (int i = 0; i < results.Count; i++)
|
|||
|
{
|
|||
|
if (current != results[i].gameObject)
|
|||
|
{
|
|||
|
ExecuteEvents.Execute(results[i].gameObject, data, function);
|
|||
|
if (this.targetRectTransform1 == results[i].gameObject.transform || results[i].gameObject.GetComponent<Selectable>())
|
|||
|
//RaycastAll后ugui会自己排序,如果你只想响应透下去的最近的一个响应,这里ExecuteEvents.Execute后直接break就行。
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
_raycastResults.Clear();
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnPopulateMesh(VertexHelper vh)
|
|||
|
{
|
|||
|
if (!Application.isPlaying)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
vh.Clear();
|
|||
|
//if (!_isValid)
|
|||
|
//{
|
|||
|
// return;
|
|||
|
//}
|
|||
|
rectTransform.GetWorldCorners(_thisCornels);
|
|||
|
if (!targetRectTransform1)
|
|||
|
{
|
|||
|
this.Rect2(vh);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
targetRectTransform1.GetWorldCorners(_targetCornels);
|
|||
|
for (int i = 0; i < 4; i++)
|
|||
|
{
|
|||
|
_thisCornels[i].z = 0f;
|
|||
|
_thisCornels[i] /= _scaleF.y;
|
|||
|
_targetCornels[i].z = 0f;
|
|||
|
_targetCornels[i] /= _scaleF.y;
|
|||
|
}
|
|||
|
|
|||
|
this.Rect(vh);
|
|||
|
}
|
|||
|
|
|||
|
private void Rect(VertexHelper vh)
|
|||
|
{
|
|||
|
UIVertex simpleVert = UIVertex.simpleVert;
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[0];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[1];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[2];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[3];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _targetCornels[0];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _targetCornels[1];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _targetCornels[2];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _targetCornels[3];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
vh.AddTriangle(0, 1, 5);
|
|||
|
vh.AddTriangle(5, 4, 0);
|
|||
|
vh.AddTriangle(0, 4, 3);
|
|||
|
vh.AddTriangle(3, 4, 7);
|
|||
|
vh.AddTriangle(7, 2, 3);
|
|||
|
vh.AddTriangle(7, 6, 2);
|
|||
|
vh.AddTriangle(6, 1, 2);
|
|||
|
vh.AddTriangle(6, 5, 1);
|
|||
|
}
|
|||
|
|
|||
|
private void Rect2(VertexHelper vh)
|
|||
|
{
|
|||
|
UIVertex simpleVert = UIVertex.simpleVert;
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[0];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[1];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[2];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
simpleVert.position = _thisCornels[3];
|
|||
|
simpleVert.color = this.color;
|
|||
|
vh.AddVert(simpleVert);
|
|||
|
|
|||
|
vh.AddTriangle(0, 1, 2);
|
|||
|
vh.AddTriangle(2, 3, 0);
|
|||
|
}
|
|||
|
|
|||
|
}
|