/// <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 adjustGUILayout) { Rect screenRect; 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; // 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) { 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 the scaled group GUI.EndGroup(); GUIScaleUtility.RestoreClips(); }