public static Vector2 GetMousePosInMeHack(this RectTransform ParamMe) { GameObject go = new GameObject("MyGO", typeof(RectTransform)); RectTransform canvasRT = WispVisualComponent.GetMainCanvas().GetComponent <RectTransform>(); RectTransform goRT = go.GetComponent <RectTransform>(); goRT.SetParent(canvasRT); goRT.AnchorTo("center-center"); goRT.PivotAround("center-center"); goRT.anchoredPosition = Vector2.zero; goRT.SetParent(ParamMe); Vector2 result = new Vector2(goRT.anchoredPosition.x, goRT.anchoredPosition.y); MonoBehaviour.Destroy(go); return(result); }
/// <summary> /// <para>Set pivot position.</para> /// <para>Example : AnchorTo("left-top"); to anchor to the Top Left of the parent RectTransform.</para> /// <para>Another Example : AnchorTo("center-center"); to anchor to the Center of the parent RectTransform.</para> /// <para>X options : right,center and left.</para> /// <para>Y options : top,center and Bottom.</para> /// </summary> public static void PivotAround(this RectTransform ParamMe, string ParamPivot) { string[] pivot = ParamPivot.Split('-'); if (pivot.Length != 2) { WispVisualComponent.LogError("Invalid pivoting parameters."); return; } float x = 0.5f; float y = 0.5f; if (pivot[0] == "right") { x = 1f; } else if (pivot[0] == "center") { x = 0.5f; } else if (pivot[0] == "left") { x = 0f; } else if (pivot[0] == "top") { y = 1f; } else if (pivot[0] == "center") { y = 0.5f; } else if (pivot[0] == "bottom") { y = 0f; } else { WispVisualComponent.LogError("Invalid pivoting parameters."); return; } if (pivot[1] == "top") { y = 1f; } else if (pivot[1] == "center") { y = 0.5f; } else if (pivot[1] == "bottom") { y = 0f; } else if (pivot[1] == "right") { x = 1f; } else if (pivot[1] == "center") { x = 0.5f; } else if (pivot[1] == "left") { x = 0f; } else { WispVisualComponent.LogError("Invalid pivoting parameters."); return; } ParamMe.pivot = new Vector2(x, y); }
/// <summary> /// <para>Set anchor position.</para> /// <para>Example : AnchorTo("left-top"); to anchor to the Top Left of the parent RectTransform.</para> /// <para>Another Example : AnchorTo("center-center"); to anchor to the Center of the parent RectTransform.</para> /// <para>X options : right,center and left.</para> /// <para>Y options : top,center and Bottom.</para> /// </summary> public static void AnchorTo(this RectTransform ParamMe, string ParamAnchor) { string[] anchors = ParamAnchor.Split('-'); if (anchors.Length != 2) { WispVisualComponent.LogError("Invalid anchoring parameters."); return; } float x = 0.5f; float y = 0.5f; if (anchors[0] == "right") { x = 1f; } else if (anchors[0] == "center") { x = 0.5f; } else if (anchors[0] == "left") { x = 0f; } else if (anchors[0] == "top") { y = 1f; } else if (anchors[0] == "center") { y = 0.5f; } else if (anchors[0] == "bottom") { y = 0f; } else { WispVisualComponent.LogError("Invalid anchoring parameters."); return; } if (anchors[1] == "top") { y = 1f; } else if (anchors[1] == "center") { y = 0.5f; } else if (anchors[1] == "bottom") { y = 0f; } else if (anchors[1] == "right") { x = 1f; } else if (anchors[1] == "center") { x = 0.5f; } else if (anchors[1] == "left") { x = 0f; } else { WispVisualComponent.LogError("Invalid anchoring parameters."); return; } ParamMe.anchorMin = new Vector2(x, y); ParamMe.anchorMax = new Vector2(x, y); }