Advertisement
Guest User

AutoHideUILayer.cs

a guest
Sep 5th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.67 KB | None | 0 0
  1. // Copyright 2017 by Astral Byte Ltd.
  2. // Source: http://www.astralbyte.co.nz/code/AutoHideUILayer.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
  7. // persons to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. // Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  14. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16.  
  17. using UnityEngine;
  18. using UnityEditor;
  19.  
  20. namespace AstralByte.Editor
  21. {
  22.     /// <summary>
  23.     /// Automatically hides the UI layer inside the Editor so it doesn't get in the way of 3D objects in scene view.
  24.     ///<para></para>
  25.     /// Installation: put in any folder named "Editor". Requires Unity 5.2 or later (for Selection.selectionChanged).
  26.     ///<para></para>
  27.     /// When any object is selected that is on the UI layer, the layer will be shown and the camera changed to 2D orthographic and zoomed to the current selection.
  28.     ///<para></para>
  29.     /// When any object on another layer is selected, the UI layer will be hidden and the camera changed back to the previous state.
  30.     /// </summary>
  31.     [InitializeOnLoad]
  32.     internal static class AutoHideUILayer
  33.     {
  34.         /************************************************************************************************************************/
  35.  
  36.         private const string
  37.             EnabledToggleMenuPath = "Edit/Auto Hide UI",
  38.             Previous2DModePref = EnabledToggleMenuPath + "_Previous2dMode",
  39.             PreviousOrthographicModePref = EnabledToggleMenuPath + "_PreviousOrthographicMode",
  40.             PreviousPivotPrefX = EnabledToggleMenuPath + "_PreviousPivot.x",
  41.             PreviousPivotPrefY = EnabledToggleMenuPath + "_PreviousPivot.y",
  42.             PreviousPivotPrefZ = EnabledToggleMenuPath + "_PreviousPivot.z",
  43.             PreviousRotationPrefX = EnabledToggleMenuPath + "_PreviousRotation.x",
  44.             PreviousRotationPrefY = EnabledToggleMenuPath + "_PreviousRotation.y",
  45.             PreviousRotationPrefZ = EnabledToggleMenuPath + "_PreviousRotation.z",
  46.             PreviousRotationPrefW = EnabledToggleMenuPath + "_PreviousRotation.w",
  47.             PreviousSizePref = EnabledToggleMenuPath + "_PreviousSize";
  48.  
  49.         private const int UiLayer = 5;
  50.  
  51.         private static bool _IsEnabled;
  52.         private static bool _IsShowingUI;
  53.         private static bool _Previous2dMode;
  54.         private static bool _PreviousOrthographicMode;
  55.         private static Vector3 _PreviousPivot;
  56.         private static Quaternion _PreviousRotation;
  57.         private static float _PreviousSize;
  58.  
  59.         /************************************************************************************************************************/
  60.  
  61.         static AutoHideUILayer()
  62.         {
  63.             EditorApplication.delayCall += () =>
  64.             {
  65.                 _IsEnabled = EditorPrefs.GetBool(EnabledToggleMenuPath, true);
  66.  
  67.                 if (_IsEnabled)
  68.                 {
  69.                     if (SceneView.lastActiveSceneView != null)
  70.                         StoreCurrentSceneViewState();
  71.  
  72.                     _Previous2dMode = EditorPrefs.GetBool(Previous2DModePref, _Previous2dMode);
  73.                     _PreviousOrthographicMode = EditorPrefs.GetBool(PreviousOrthographicModePref, _PreviousOrthographicMode);
  74.                     _PreviousPivot.x = EditorPrefs.GetFloat(PreviousPivotPrefX, _PreviousPivot.x);
  75.                     _PreviousPivot.y = EditorPrefs.GetFloat(PreviousPivotPrefY, _PreviousPivot.y);
  76.                     _PreviousPivot.z = EditorPrefs.GetFloat(PreviousPivotPrefZ, _PreviousPivot.z);
  77.                     _PreviousRotation.x = EditorPrefs.GetFloat(PreviousRotationPrefX, _PreviousRotation.x);
  78.                     _PreviousRotation.y = EditorPrefs.GetFloat(PreviousRotationPrefY, _PreviousRotation.y);
  79.                     _PreviousRotation.z = EditorPrefs.GetFloat(PreviousRotationPrefZ, _PreviousRotation.z);
  80.                     _PreviousRotation.w = EditorPrefs.GetFloat(PreviousRotationPrefW, _PreviousRotation.w);
  81.                     _PreviousSize = EditorPrefs.GetFloat(PreviousSizePref, _PreviousSize);
  82.  
  83.                     var activeGameObject = Selection.activeGameObject;
  84.                     _IsShowingUI = activeGameObject != null && activeGameObject.layer == UiLayer;
  85.  
  86.                     Selection.selectionChanged += OnSelectionChanged;
  87.                     EditorApplication.playmodeStateChanged += OnSelectionChanged;
  88.                 }
  89.             };
  90.         }
  91.  
  92.         /************************************************************************************************************************/
  93.  
  94.         private static void StoreCurrentSceneViewState()
  95.         {
  96.             var sceneView = SceneView.lastActiveSceneView;
  97.  
  98.             _Previous2dMode = sceneView.in2DMode;
  99.             _PreviousOrthographicMode = sceneView.orthographic;
  100.             _PreviousPivot = sceneView.pivot;
  101.             _PreviousRotation = sceneView.rotation;
  102.             _PreviousSize = sceneView.size;
  103.         }
  104.  
  105.         /************************************************************************************************************************/
  106.  
  107.         [MenuItem(EnabledToggleMenuPath, validate = true)]
  108.         private static bool ValidateToggleEnabled()
  109.         {
  110.             Menu.SetChecked(EnabledToggleMenuPath, _IsEnabled);
  111.             return true;
  112.         }
  113.  
  114.         [MenuItem(EnabledToggleMenuPath, priority = 200)]
  115.         private static void ToggleEnabled()
  116.         {
  117.             _IsEnabled = !_IsEnabled;
  118.             EditorPrefs.SetBool(EnabledToggleMenuPath, _IsEnabled);
  119.  
  120.             if (_IsEnabled)
  121.             {
  122.                 Selection.selectionChanged += OnSelectionChanged;
  123.                 EditorApplication.playmodeStateChanged += OnSelectionChanged;
  124.                 OnSelectionChanged();
  125.             }
  126.             else
  127.             {
  128.                 Selection.selectionChanged -= OnSelectionChanged;
  129.                 EditorApplication.playmodeStateChanged -= OnSelectionChanged;
  130.                 HideUI();
  131.             }
  132.         }
  133.  
  134.         /************************************************************************************************************************/
  135.  
  136.         private static void OnSelectionChanged()
  137.         {
  138.             var activeGameObject = Selection.activeGameObject;
  139.             if (activeGameObject != null && activeGameObject.layer == UiLayer)
  140.                 ShowUI();
  141.             else
  142.                 HideUI();
  143.         }
  144.  
  145.         /************************************************************************************************************************/
  146.  
  147.         private static void ShowUI()
  148.         {
  149.             if (_IsShowingUI)
  150.                 return;
  151.  
  152.             var sceneView = SceneView.lastActiveSceneView;
  153.             if (sceneView == null)
  154.                 return;
  155.  
  156.             StoreCurrentSceneViewState();
  157.             SetCurrentSceneViewStatePrefs();
  158.  
  159.             // Apply UI mode and show the UI layer.
  160.             sceneView.in2DMode = true;
  161.             sceneView.orthographic = true;
  162.             sceneView.FrameSelected();
  163.             Tools.visibleLayers |= 1 << UiLayer;
  164.             _IsShowingUI = true;
  165.         }
  166.  
  167.         /************************************************************************************************************************/
  168.  
  169.         private static void SetCurrentSceneViewStatePrefs()
  170.         {
  171.             EditorPrefs.SetBool(Previous2DModePref, _Previous2dMode);
  172.             EditorPrefs.SetBool(PreviousOrthographicModePref, _PreviousOrthographicMode);
  173.             EditorPrefs.SetFloat(PreviousPivotPrefX, _PreviousPivot.x);
  174.             EditorPrefs.SetFloat(PreviousPivotPrefY, _PreviousPivot.y);
  175.             EditorPrefs.SetFloat(PreviousPivotPrefZ, _PreviousPivot.z);
  176.             EditorPrefs.SetFloat(PreviousRotationPrefX, _PreviousRotation.x);
  177.             EditorPrefs.SetFloat(PreviousRotationPrefY, _PreviousRotation.y);
  178.             EditorPrefs.SetFloat(PreviousRotationPrefZ, _PreviousRotation.z);
  179.             EditorPrefs.SetFloat(PreviousRotationPrefW, _PreviousRotation.w);
  180.             EditorPrefs.SetFloat(PreviousSizePref, _PreviousSize);
  181.         }
  182.  
  183.         /************************************************************************************************************************/
  184.  
  185.         private static void HideUI()
  186.         {
  187.             if (!_IsShowingUI)
  188.                 return;
  189.  
  190.             var sceneView = SceneView.lastActiveSceneView;
  191.             if (sceneView == null)
  192.                 return;
  193.  
  194.             // Return to the stored scene view state and hide the UI layer.
  195.             sceneView.in2DMode = _Previous2dMode;
  196.             sceneView.LookAt(_PreviousPivot, _PreviousRotation, _PreviousSize, _PreviousOrthographicMode);
  197.             Tools.visibleLayers &= ~(1 << UiLayer);
  198.             _IsShowingUI = false;
  199.         }
  200.  
  201.         /************************************************************************************************************************/
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement