Create a new C# script in Unity and paste this code into it. Drag and drop this script to the panel you want to close after clicking outside of the panel, and you are good to go.
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class ClickOutsideToClose : MonoBehaviour
- {
- void Update()
- {
- if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
- {
- this.gameObject.SetActive(ClickingSelfOrChild());
- }
- }
- private bool ClickingSelfOrChild()
- {
- RectTransform[] rectTransforms = GetComponentsInChildren<RectTransform>();
- foreach(RectTransform rectTransform in rectTransforms)
- {
- if (EventSystem.current.currentSelectedGameObject == rectTransform.gameObject)
- {
- return true;
- };
- }
- return false;
- }
- }
This comment has been removed by a blog administrator.
ReplyDelete