using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 为点击不规则图形而生,参与渲染
/// </summary>
public class UIImageClick : Image
{
    private Collider2D[] mColliders;   // 可以带有多种碰撞区域

    protected override void Awake()
    {
        base.Awake();
        mColliders = GetComponents<Collider2D>();
        if ((mColliders != null) && (mColliders.Length == 0))
        {
            mColliders = null;
        }
        else
        {
            for (int i = 0; i < mColliders.Length; i++)
            {
                mColliders[i].enabled = false;      // 等需要判断的时候打开,减少性能消耗
            }
        }
        useLegacyMeshGeneration = false;
    }

    public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    {
        if (mColliders != null)
        {
            bool valid = false;
            for (int i = 0; i < mColliders.Length; i++)
            {
                var collider = mColliders[i];
                collider.enabled = true;
                valid = collider.OverlapPoint(eventCamera.ScreenToWorldPoint(screenPoint));
                collider.enabled = false;
                if (valid)
                {
                    break;
                }
            }
            return valid;
        }
        return base.IsRaycastLocationValid(screenPoint, eventCamera);
    }
}
最后修改:2023 年 06 月 09 日
如果觉得我的文章对你有用,请随意赞赏