public void ShowPopup(GObject popup, GObject owner, PopupDirection direction, PopupConstraint constraint, PopupHideCallback hideCallback = null) { this._popupHideCallback = hideCallback; this._blocker = new GObject(); this._blocker.onClick.Add(this.OnPopupHide); this.AddChild(this._blocker); DisplayObject displayObj = this._blocker.displayObject; ToolSet.SetAnchor(displayObj.rectTransform, AnchorType.Stretch_Stretch); displayObj.RegisterEventTriggerType(EventTriggerType.PointerClick); displayObj.name = "Blocker"; displayObj.size = Vector2.zero; this.popup = popup; this.AddChild(this.popup); owner = owner ?? this; Vector3[] corners = new Vector3[4]; owner.displayObject.rectTransform.GetWorldCorners(corners); Vector3 min = this.displayObject.rectTransform.InverseTransformPoint(corners[0]); Vector3 max = this.displayObject.rectTransform.InverseTransformPoint(corners[2]); PointerEventData pointerEventData = EventSystem.instance.pointerInput.GetLastPointerEventData(PointerInput.K_MOUSE_LEFT_ID); switch (direction) { case PopupDirection.TouchPosition: this.popup.position = this.ScreenToLocal(pointerEventData.position); break; case PopupDirection.Left: this.popup.position = new Vector2(min.x - this.popup.actualSize.x, -max.y); break; case PopupDirection.Upward: this.popup.position = new Vector2(min.x, -max.y - this.popup.actualSize.y); break; case PopupDirection.Right: this.popup.position = new Vector2(max.x, -max.y); break; case PopupDirection.Downward: this.popup.position = new Vector2(min.x, -min.y); break; } if (constraint > 0) { corners = new Vector3[4]; this.popup.displayObject.rectTransform.GetWorldCorners(corners); min = this.displayObject.rectTransform.InverseTransformPoint(corners[0]); max = this.displayObject.rectTransform.InverseTransformPoint(corners[2]); if ((constraint & PopupConstraint.Right) > 0 && max.x > this.size.x) { this.popup.position = new Vector2(this.size.x - this.popup.size.x, this.popup.position.y); } if ((constraint & PopupConstraint.Left) > 0 && min.x < 0) { this.popup.position = new Vector2(0, this.popup.position.y); } if ((constraint & PopupConstraint.Bottom) > 0 && -min.y > this.size.y) { this.popup.position = new Vector2(this.popup.position.x, this.size.y - this.popup.size.y); } if ((constraint & PopupConstraint.Top) > 0 && -max.y < 0) { this.popup.position = new Vector2(this.popup.position.x, 0); } } }