/// <summary> /// Begins a scaled local area. /// Returns vector to offset GUI controls with to account for zooming to the pivot. /// Using adjustGUILayout does that automatically for GUILayout rects. Theoretically can be nested! /// </summary> public static Vector2 BeginScale(ref Rect rect, Vector2 zoomPivot, float zoom, bool IsEditorWindow, bool adjustGUILayout) { isEditorWindow = IsEditorWindow; Rect screenRect; if (compabilityMode) { // In compability mode, we will assume only one top group and do everything manually, not using reflected calls (-> practically blind) GUI.EndGroup(); screenRect = rect; #if UNITY_EDITOR if (isEditorWindow) { screenRect.y += 23; } #endif } else { // If it's supported, we take the completely generic way using reflected calls GUIScaleUtility.BeginNoClip(); screenRect = GUIScaleUtility.GUIToScaledSpace(rect); } rect = Scale(screenRect, screenRect.position + zoomPivot, new Vector2(zoom, zoom)); // Now continue drawing using the new clipping group GUI.BeginGroup(rect); rect.position = Vector2.zero; // Adjust because we entered the new group // Because I currently found no way to actually scale to a custom pivot rather than (0, 0), // we'll make use of a cheat and just offset it accordingly to let it appear as if it would scroll to the center // Note, due to that, controls not adjusted are still scaled to (0, 0) Vector2 zoomPosAdjust = rect.center - screenRect.size / 2 + zoomPivot; // For GUILayout, we can make this adjustment here if desired adjustedGUILayout.Add(adjustGUILayout); if (adjustGUILayout) { GUILayout.BeginHorizontal(); GUILayout.Space(rect.center.x - screenRect.size.x + zoomPivot.x); GUILayout.BeginVertical(); GUILayout.Space(rect.center.y - screenRect.size.y + zoomPivot.y); } // Take a matrix backup to restore back later on GUIMatrices.Add(GUI.matrix); // Scale GUI.matrix. After that we have the correct clipping group again. GUIUtility.ScaleAroundPivot(new Vector2(1 / zoom, 1 / zoom), zoomPosAdjust); return(zoomPosAdjust); }
/// <summary> /// Ends a scale region previously opened with BeginScale /// </summary> public static void EndScale() { // Set last matrix and clipping group if (GUIMatrices.Count == 0 || adjustedGUILayout.Count == 0) { throw new UnityException("GUIScaleUtility: You are ending more scale regions than you are beginning!"); } GUI.matrix = GUIMatrices[GUIMatrices.Count - 1]; GUIMatrices.RemoveAt(GUIMatrices.Count - 1); // End GUILayout zoomPosAdjustment if (adjustedGUILayout[adjustedGUILayout.Count - 1]) { GUILayout.EndVertical(); GUILayout.EndHorizontal(); } adjustedGUILayout.RemoveAt(adjustedGUILayout.Count - 1); // End the scaled group GUI.EndGroup(); if (compabilityMode) { // In compability mode, we don't know the previous group rect, but as we cannot use top groups there either way, we restore the screen group if (isEditorWindow) // We're in an editor window { GUI.BeginClip(new Rect(0, 23, Screen.width, Screen.height - 23)); } else { GUI.BeginClip(new Rect(0, 0, Screen.width, Screen.height)); } } else { // Else, restore the clips (groups) GUIScaleUtility.RestoreClips(); } }