纳金网

标题: UGUI不规则热区的处理 [打印本页]

作者: 烟雨    时间: 2015-11-30 23:45
标题: UGUI不规则热区的处理

利用系统现有组件,简单处理了下:

1. 自定义了一个CustomRaycaster类,派生自GraphicRaycaster
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5. using UnityEngine.EventSystems;

  6. public class CustomRaycaster : GraphicRaycaster
  7. {
  8.     private List<RaycastResult> _toBeRemoved = new List<RaycastResult>();

  9.     public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  10.     {
  11.         base.Raycast(eventData, resultAppendList);

  12.         _toBeRemoved.Clear();
  13.         foreach (var result in resultAppendList)
  14.         {
  15.             var polygon2D = result.gameObject.GetComponent<PolygonCollider2D>();
  16.             if (!polygon2D)
  17.                 continue;
  18.             
  19.             if (!polygon2D.OverlapPoint(eventData.position))
  20.                 _toBeRemoved.Add(result);
  21.         }

  22.         if (_toBeRemoved.Count > 0)
  23.             resultAppendList.RemoveAll(r => _toBeRemoved.Contains(r));
  24.     }
  25. }
复制代码
2. 删除Canvas上原有的GraphicRaycaster,用这个替换
3. 在不规则UI对象上添加一个PolygonCollider2D组件,编辑节点,覆盖热点区域





欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5