鸟语天空
Rect.Overlaps()
post by:追风剑情 2019-10-14 14:25

示例:判断矩形碰撞

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;
    }
}

UGUITool.cs

运行测试
111111.gif

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容