private void SetUIPosition() { if (Vector2.Distance(mNowPosition, Input.mousePosition) > 10f) { mNowPosition = Input.mousePosition; } Vector2 v; RectTransformUtility.ScreenPointToLocalPointInRectangle(mRoot, mNowPosition, null, out v); mRect.anchoredPosition = v; //如果有強制設定定位點,就直接套用 if (mPrefferedAnchor != AnchorPivot.Default) { mNowAnchorPivot = mPrefferedAnchor; ResetUIPivot(); FixOutPosition(); return; } if (CheckUIOutZone(out mNowAnchorPivot)) { ResetUIPivot(); } }
IEnumerator WaitForUIInitial(TipType type, AnchorPivot ac, params object[] value) { while (null == mRoot) { yield return(null); } if (type == TipType.Unknow) { yield break; } mNowTipContentObj = null; yield return(CreateTip(type, ac)); while (null == mNowTipContentObj) { yield return(null); } switch (type) { case TipType.Txt: ShowTip((string)value[0], (string)value[1], (TextAnchor)value[2], (TextAnchor)value[3], 0f); break; case TipType.Img: ShowTip((string)value[0], 0f); break; } }
/// <summary>Image Tip Content</summary> /// <param name="root"></param> /// <param name="assetImageId"></param> public static void Show(string assetImageId, bool useTween = false, AnchorPivot preferredAnchor = AnchorPivot.Default) { if (null == Instance) { Initialize(); } Instance.mUseTween = useTween; Instance.StartCoroutine(Instance.WaitForUIInitial(TipType.Img, preferredAnchor, new object[1] { assetImageId })); }
/// <summary>確認是否超出螢幕範圍</summary> /// <param name="anchorPivot">ui pivot</param> private bool CheckUIOutZone(out AnchorPivot anchorPivot) { Vector3[] mRectPointArr = new Vector3[4]; mRect.GetWorldCorners(mRectPointArr); Vector3[] mParentRectPointArr = new Vector3[4]; mRoot.GetWorldCorners(mParentRectPointArr); Vector3 mRPoint = mRectPointArr[2]; Vector3 mPPoint = mParentRectPointArr[2]; if (mRPoint.x >= mPPoint.x) { anchorPivot = (mRPoint.y >= mPPoint.y) ? AnchorPivot.TopRight : AnchorPivot.BottomRight; return(true); } else { anchorPivot = (mRPoint.y >= mPPoint.y) ? AnchorPivot.TopLeft : AnchorPivot.BottomLeft; return(true); } }
IEnumerator CreateTip(TipType tipType, AnchorPivot ac = AnchorPivot.Default) { if (!mTipDict.ContainsKey(tipType)) { switch (tipType) { case TipType.Img: ImageTipObj(); break; case TipType.Txt: TextTipObj(); break; } } while (!mTipDict.ContainsKey(tipType)) { yield return(null); } mNowTipType = tipType; mPrefferedAnchor = ac; //tip GameObject mNowTipContentObj = mTipDict[mNowTipType]; mNowTipContentObj.SetActive(true); //tip RectTransform mRect = mNowTipContentObj.GetComponent <RectTransform>(); mRect.pivot = mRectAnchorDict[mPrefferedAnchor]; //tip CanvasGroup mCanvasGroup = mNowTipContentObj.GetComponent <CanvasGroup>(); }
/// <summary>Text Tip Content</summary> /// <param name="root"></param> /// <param name="topContent"></param> /// <param name="bottomContent"></param> public static void Show(string topContent, string bottomContent = "", bool useTween = false, AnchorPivot preferredAnchor = AnchorPivot.Default, TextAnchor topCTAnchor = TextAnchor.UpperLeft, TextAnchor bottomCTAnchor = TextAnchor.UpperLeft) { if (null == Instance) { Initialize(); } Instance.mUseTween = useTween; Instance.StartCoroutine(Instance.WaitForUIInitial(TipType.Txt, preferredAnchor, new object[4] { topContent, bottomContent, topCTAnchor, bottomCTAnchor })); }