Rect.Overlaps()

作者:追风剑情 发布于:2019-10-14 14:25 分类:Unity3d

示例:判断矩形碰撞

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

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号