/// <summary> /// Locates the transform element in the screen according the layout value. /// It depends on the way the transform originally was z-located and xy-scaled to act as a GUI element. /// </summary> /// <param name='tr'> /// Transform /// </param> /// <param name='guiElem'> /// Is the GUI element containing the info about size in pixels or as a proportion /// </param> /// <param name='offsetInPixels'> /// Offset measured in pixels. /// </param> /// <param name='layout'> /// Layout. Base screen positions /// </param> public static void adjustPos(Transform tr, GUICustomElement guiElem, Vector2 offsetInPixels, EnumScreenLayout layout) { Vector3 p = tr.localPosition; // assume the GUI element has its size as pixels Vector2 size = guiElem.virtualSize.x != 0 ? guiElem.virtualSize : guiElem.size; // convert to pixels if GUI element's size is set as a proportion if (!guiElem.sizeAsPixels) { size.x *= Screen.width; size.y *= Screen.height; } switch (layout) { case EnumScreenLayout.NONE: { Vector2 temp = screenToGUI(offsetInPixels.x, offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.TOP_LEFT: { Vector2 temp = screenToGUI(offsetInPixels.x + size.x / 2f, Screen.height - size.y / 2f + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.TOP: { Vector2 temp = screenToGUI(Screen.width / 2 + offsetInPixels.x, Screen.height - size.y / 2f + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.TOP_RIGHT: { Vector2 temp = screenToGUI(Screen.width + offsetInPixels.x - size.x / 2f, Screen.height - size.y / 2f + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.CENTER_LEFT: { Vector2 temp = screenToGUI(offsetInPixels.x + size.x / 2f, Screen.height / 2 + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.CENTER: { p.x = Screen.width / 2 - size.x / 2 + offsetInPixels.x; p.y = Screen.height / 2 - size.y / 2 + offsetInPixels.y; Vector2 temp = screenToGUI(Screen.width / 2 + offsetInPixels.x, Screen.height / 2 + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.CENTER_RIGHT: { Vector2 temp = screenToGUI(Screen.width + offsetInPixels.x - size.x / 2f, Screen.height / 2 + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.BOTTOM_LEFT: { Vector2 temp = screenToGUI(offsetInPixels.x + size.x / 2f, size.y / 2f + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.BOTTOM: { Vector2 temp = screenToGUI(Screen.width / 2 + offsetInPixels.x, size.y / 2f + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } case EnumScreenLayout.BOTTOM_RIGHT: { Vector2 temp = screenToGUI(Screen.width + offsetInPixels.x - size.x / 2f, size.y / 2f + offsetInPixels.y); p.x = temp.x; p.y = temp.y; break; } default: break; } #if UNITY_EDITOR if (float.IsNegativeInfinity(p.x) || float.IsInfinity(p.x) || float.IsNaN(p.x)) { p.x = 0f; } if (float.IsNegativeInfinity(p.y) || float.IsInfinity(p.y) || float.IsNaN(p.y)) { p.y = 0f; } #endif tr.localPosition = p; }
/// <summary> /// Locates the GUITexture element in the screen according the layout value. /// It modifies the GUITexture's pixel inset. /// </summary> /// <param name='gt'> /// Gt. Unity'sGUITexture /// </param> /// <param name='offset'> /// Offset /// </param> /// <param name='layout'> /// Layout /// </param> public static void adjustPos(GUITexture gt, Vector2 offset, EnumScreenLayout layout) { Rect p = gt.pixelInset; switch (layout) { case EnumScreenLayout.TOP_LEFT: { p.x = offset.x; p.y = Screen.height - p.height + offset.y; break; } case EnumScreenLayout.TOP: { p.x = Screen.width / 2 - p.width / 2 + offset.x; p.y = Screen.height - p.height + offset.y; break; } case EnumScreenLayout.TOP_RIGHT: { p.x = Screen.width - p.width + offset.x; p.y = Screen.height - p.height + offset.y; break; } case EnumScreenLayout.CENTER_LEFT: { p.x = offset.x; p.y = Screen.height / 2 - p.height / 2 + offset.y; break; } case EnumScreenLayout.CENTER: { p.x = Screen.width / 2 - p.width / 2 + offset.x; p.y = Screen.height / 2 - p.height / 2 + offset.y; break; } case EnumScreenLayout.CENTER_RIGHT: { p.x = Screen.width - p.width + offset.x; p.y = Screen.height / 2 - p.height / 2 + offset.y; break; } case EnumScreenLayout.BOTTOM_LEFT: { p.x = offset.x; p.y = offset.y; break; } case EnumScreenLayout.BOTTOM: { p.x = Screen.width / 2 - p.width + offset.x; p.y = offset.y; break; } case EnumScreenLayout.BOTTOM_RIGHT: { p.x = Screen.width - p.width + offset.x; p.y = offset.y; break; } default: break; } #if UNITY_EDITOR if (float.IsNegativeInfinity(p.x) || float.IsInfinity(p.x) || float.IsNaN(p.x)) { p.x = 0f; } if (float.IsNegativeInfinity(p.y) || float.IsInfinity(p.y) || float.IsNaN(p.y)) { p.y = 0f; } #endif gt.pixelInset = p; }