using System.Collections.Generic; using DG.Tweening; using Sirenix.OdinInspector; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace BlueWater.Uis { public class TycoonUiManager : Singleton { // Variables #region Variables [field: SerializeField] public Canvas MainCanvas { get; private set; } [field: SerializeField] public Canvas WorldCanvas { get; private set; } [SerializeField] private Image _fadeImage; [Title("테스트")] [SerializeField] private bool _isUiClickTest; [SerializeField] private GraphicRaycaster _raycaster; [SerializeField] private EventSystem _eventSystem; #endregion // Unity events #region Unity events protected override void OnAwake() { InitializeComponents(); } private void Start() { EventManager.OnFadeInOut += FadeInOut; } private void Update() { if (!_isUiClickTest) return; if (Input.GetMouseButtonDown(0)) { // 1. UI Raycast PointerEventData pointerData = new PointerEventData(_eventSystem) { position = Input.mousePosition }; List uiResults = new List(); _raycaster.Raycast(pointerData, uiResults); if (uiResults.Count > 0) { Debug.Log($"Clicked on UI: {uiResults[0].gameObject.name}"); return; } // 2. 3D Raycast Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit)) { Debug.Log($"Clicked on 3D Object: {hit.collider.gameObject.name}"); return; } // 3. 2D Raycast Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit2D = Physics2D.Raycast(mousePosition, Vector2.zero); if (hit2D.collider != null) { Debug.Log($"Clicked on 2D Object: {hit2D.collider.gameObject.name}"); } } } private void OnDestroy() { EventManager.OnFadeInOut -= FadeInOut; } #endregion // Initialize methods #region Initialize methods [Button("셋팅 초기화")] private void InitializeComponents() { MainCanvas = GetComponent(); WorldCanvas = GameObject.Find("WorldCanvas").GetComponent(); } #endregion // Methods #region Methods public void FadeInOut(float fadeInTime, float fadeOutTime, Color? fadeColor = null, float delayAfterFadeIn = 0f) { var newColor = new Color(1f, 1f, 1f, 0f); if (fadeColor != null) { newColor = (Color)fadeColor; } _fadeImage.color = newColor; _fadeImage.enabled = true; _fadeImage.DOFade(1f, fadeInTime).OnComplete(() => { DOVirtual.DelayedCall(delayAfterFadeIn, () => { _fadeImage.DOFade(0f, fadeOutTime).OnComplete(() => { _fadeImage.enabled = false; }); }); }); } #endregion } }