using UnityEngine; using System; using UniRx; using UniRx.Triggers; using UnityEngine.EventSystems; public abstract class UISelectorPopUp : UIPopUp where T : Component { [NonSerialized] public IObservable OnOpen = new Subject(); [NonSerialized] public IObservable OnClose = new Subject(); protected T lastUnit { get; private set; } protected override void Awake() { base.Awake(); var onOpen = OnOpenObservable(); if (onOpen != null) onOpen.Subscribe(x => Open(x)).AddTo(this); var onClose = OnCloseObservable(); if (onClose != null) onClose.Subscribe(Close).AddTo(this); IsOpenObservable.Where(x => x) .Subscribe(x => { Observable.Merge ( EventSystem.current.OnMouseDownGlobalAsObservable(0), EventSystem.current.OnTouchDownGlobalAsObservable().Select(touch => touch.position) ) .TakeUntil(OnClose) .Subscribe(OnPress) .AddTo(this); }) .AddTo(this); } private void OnPress(Vector2 position) { var selected = EventSystem.current.currentSelectedGameObject; if (IsOpen && (selected == null || IsOther(selected.transform))) { Close(lastUnit); } } protected bool IsOther(Transform selected) { if (selected == null) return true; if (selected == transform) return false; return IsOther(selected.parent); } protected virtual void Close(T unit) { if (!IsOpen) { Close(); return; } Close(); DebugFormat.Log(this, gameObject.name); ((Subject)OnClose).OnNext(lastUnit); lastUnit = null; } protected virtual bool Open(T unit) { if (IsOpen) return Open(); DebugFormat.Log(this, gameObject.name); ((Subject)OnOpen).OnNext(lastUnit = unit); return Open(); } protected abstract IObservable OnOpenObservable(); protected abstract IObservable OnCloseObservable(); }