UnityEditor.HandleUtility.WorldPointToSizedRect C# (CSharp) Method

WorldPointToSizedRect() public static method

Calculate a rectangle to display a 2D GUI element near a projected point in 3D space.

public static WorldPointToSizedRect ( Vector3 position, GUIContent content, GUIStyle style ) : Rect
position Vector3 The world-space position to use.
content UnityEngine.GUIContent The content to make room for.
style UnityEngine.GUIStyle The style to use. The style's alignment.
return UnityEngine.Rect
        public static Rect WorldPointToSizedRect(Vector3 position, GUIContent content, GUIStyle style)
        {
            Vector2 vector = WorldToGUIPoint(position);
            Vector2 vector2 = style.CalcSize(content);
            Rect rect = new Rect(vector.x, vector.y, vector2.x, vector2.y);
            switch (style.alignment)
            {
                case TextAnchor.UpperCenter:
                    rect.xMin -= rect.width * 0.5f;
                    break;

                case TextAnchor.UpperRight:
                    rect.xMin -= rect.width;
                    break;

                case TextAnchor.MiddleLeft:
                    rect.yMin -= rect.height * 0.5f;
                    break;

                case TextAnchor.MiddleCenter:
                    rect.xMin -= rect.width * 0.5f;
                    rect.yMin -= rect.height * 0.5f;
                    break;

                case TextAnchor.MiddleRight:
                    rect.xMin -= rect.width;
                    rect.yMin -= rect.height * 0.5f;
                    break;

                case TextAnchor.LowerLeft:
                    rect.yMin -= rect.height * 0.5f;
                    break;

                case TextAnchor.LowerCenter:
                    rect.xMin -= rect.width * 0.5f;
                    rect.yMin -= rect.height;
                    break;

                case TextAnchor.LowerRight:
                    rect.xMin -= rect.width;
                    rect.yMin -= rect.height;
                    break;
            }
            return style.padding.Add(rect);
        }

Usage Example

        private static void DrawPoseError(Transform node, Bounds bounds)
        {
            Camera current = Camera.current;

            if (current)
            {
                GUIStyle gUIStyle = new GUIStyle(GUI.skin.label);
                gUIStyle.normal.textColor = Color.red;
                gUIStyle.wordWrap         = false;
                gUIStyle.alignment        = TextAnchor.MiddleLeft;
                Vector3 position = node.position;
                Vector3 vector   = node.position + Vector3.up * 0.2f;
                if (node.position.x <= node.root.position.x)
                {
                    vector.x = bounds.min.x;
                }
                else
                {
                    vector.x = bounds.max.x;
                }
                GUIContent content   = new GUIContent(node.name);
                Rect       position2 = HandleUtility.WorldPointToSizedRect(vector, content, gUIStyle);
                position2.x += 2f;
                if (node.position.x > node.root.position.x)
                {
                    position2.x -= position2.width;
                }
                Handles.BeginGUI();
                position2.y -= gUIStyle.CalcSize(content).y / 4f;
                GUI.Label(position2, content, gUIStyle);
                Handles.EndGUI();
                Handles.color = AvatarSkeletonDrawer.kErrorMessageColor;
                Handles.DrawLine(position, vector);
            }
        }
All Usage Examples Of UnityEditor.HandleUtility::WorldPointToSizedRect