示例:判断矩形碰撞
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestOverlaps : MonoBehaviour { public Canvas canvas; public Text text; public RectTransform redRect; public RectTransform greenRect; //true: 允许Rect的Width或Height为负值 public bool allowInverse = true; //true: 两个矩形区存在重叠部分 public bool overlaps; [SerializeField] private Rect m_Red; [SerializeField] private Rect m_Green; void Update() { m_Red = UGUITool.GetCanvasRect(redRect, canvas); m_Green = UGUITool.GetCanvasRect(greenRect, canvas); overlaps = m_Red.Overlaps(m_Green, allowInverse); text.text = "overlaps=" + overlaps; } }