UGUI インターフェイスがクリックされたかどうかを判断する (ツール クラス)

空白の領域をクリックして現在のページを閉じる必要があることがよくあるため、インターフェイスが閉じられたときにカスタム イベントを追加できる簡単なツール クラスを作成しました。閉じたい。

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class CloseSlefLogic : MonoBehaviour
{
    public UnityEvent closeHandle;

    private GameObject nowClick;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            nowClick = ClickObject();

            if (nowClick == null || (nowClick != gameObject && nowClick.transform.parent != transform))
            {
                closeHandle.Invoke();
            }
        }
    }

    public GameObject ClickObject()
    {
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
        if (results.Count > 0)
        {
            return results[0].gameObject;
        }
        else
        {
            return null;
        }
    }
}

おすすめ

転載: blog.csdn.net/qq_33325776/article/details/122624962