7/27/2015 9:20:04 PM

To get the pixel height and width of a game object in Unity, you need to get the RectTransform and then Rect object. The following code will return the Rect object and then return the values of either width or height.

public static Rect Get_Rect(GameObject gameObject) { if (gameObject != null) { RectTransform rectTransform = gameObject.GetComponent<RectTransform>(); if (rectTransform != null) { return rectTransform.rect; } } else { Debug.Log("Game object is null."); } return new Rect(); } public static float Get_Width(Component component) { if (component != null) { return Get_Width(component.gameObject); } return 0; } public static float Get_Width(GameObject gameObject) { if (gameObject != null) { var rect = Get_Rect(gameObject); if (rect != null) { return rect.width; } } return 0; } public static float Get_Height(Component component) { if (component != null) { return Get_Height(component.gameObject); } return 0; } public static float Get_Height(GameObject gameObject) { if (gameObject != null) { var rect = Get_Rect(gameObject); if (rect != null) { return rect.height; } } return 0; }