///<summary>calc a rect from given element width/height, gui/design size and GUIElementTransform</summary> public static Rect CalcRect(float guiW, float guiH, GUIElementTransform tr) { float eleW = tr.size.x; float eleH = tr.size.y; float x = tr.offset.x; float y = tr.offset.y; if (tr.stretch == Stretch.Width) eleW = guiW; if (tr.stretch == Stretch.Height) eleH = guiH; if (tr.stretch == Stretch.Both) { eleW = guiW; eleH = guiH; } else if (tr.stretch == Stretch.BothAspectRatio) { float aspect = eleW / eleH; if (aspect < (guiW / guiH)) { // image is tall eleW = (guiH / eleH) * eleW; eleH = guiH; } else { // image is wide eleH = (guiW / eleW) * eleH; eleW = guiW; } } if (tr.xAlign == XAlign.Right) x = guiW - eleW - tr.offset.x; else if (tr.xAlign == XAlign.Center) x = (guiW - eleW) * 0.5f + tr.offset.x; if (tr.yAlign == YAlign.Bottom) y = guiH - eleH - tr.offset.y; else if (tr.yAlign == YAlign.Middle) y = (guiH - eleH) * 0.5f + tr.offset.y; return new Rect(x, y, eleW, eleH); }
public static GUIElementTransform ElementTransformField(GUIElementTransform tr, string label=null, bool stretch=true, bool xalign=true, bool yalign=true, bool offset=true, bool size=true) { if (!string.IsNullOrEmpty(label)) { GUILayout.Label(label, EditorStyles.boldLabel); EditorGUILayout.Space(); } if (stretch) tr.stretch = (GUIElementTransform.Stretch)EditorGUILayout.Popup("Stretch", (int)tr.stretch, System.Enum.GetNames(typeof(GUIElementTransform.Stretch))); if (xalign) tr.xAlign = (GUIElementTransform.XAlign)EditorGUILayout.Popup("X-Align", (int)tr.xAlign, System.Enum.GetNames(typeof(GUIElementTransform.XAlign))); if (yalign) tr.yAlign = (GUIElementTransform.YAlign)EditorGUILayout.Popup("Y-Align", (int)tr.yAlign, System.Enum.GetNames(typeof(GUIElementTransform.YAlign))); if (offset) tr.offset = EditorGUILayout.Vector2Field("Offset", tr.offset); if (size) tr.size = UniRPGEdGui.Vector2Field("Size", "width", "height", tr.size, 200); return tr; }
// ================================================================================================================ #region Helpers public static Texture2D TextureWithTransformCheck(string label, Texture2D texture, GUIElementTransform tr, params GUILayoutOption[] options) { Texture2D prev = texture; if (!string.IsNullOrEmpty(label)) texture = (Texture2D)EditorGUILayout.ObjectField(label, texture, typeof(Texture2D), false); else texture = (Texture2D)EditorGUILayout.ObjectField(texture, typeof(Texture2D), false, options); if (prev != texture) { if (texture) { tr.size.x = texture.width; tr.size.y = texture.height; } else { tr.size.x = 0; tr.size.y = 0; } } return texture; }