Advertisement
Guest User

Unity GameView class

a guest
Oct 28th, 2016
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.95 KB | None | 0 0
  1. namespace UnityEditor
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.     using System.Runtime.InteropServices;
  7.     using UnityEditor.AnimatedValues;
  8.     using UnityEditor.Modules;
  9.     using UnityEditor.SceneManagement;
  10.     using UnityEditorInternal;
  11.     using UnityEngine;
  12.     using UnityEngine.Events;
  13.  
  14.     [EditorWindowTitle(title="Game", useTypeNameAsIconName=true)]
  15.     internal class GameView : EditorWindow, IHasCustomMenu
  16.     {
  17.         private GUIContent gizmosContent = new GUIContent("Gizmos");
  18.         private const int kBorderSize = 5;
  19.         private const int kToolbarHeight = 0x11;
  20.         [SerializeField]
  21.         private bool m_Gizmos;
  22.         [SerializeField]
  23.         private bool m_MaximizeOnPlay;
  24.         private AnimBool m_ResolutionTooLargeWarning = new AnimBool(false);
  25.         [SerializeField]
  26.         private int[] m_SelectedSizes = new int[0];
  27.         private Vector2 m_ShownResolution = Vector2.zero;
  28.         private int m_SizeChangeID = -2147483648;
  29.         [SerializeField]
  30.         private bool m_Stats;
  31.         [SerializeField]
  32.         private int m_TargetDisplay;
  33.         private GUIContent renderdocContent;
  34.         private static List<GameView> s_GameViews = new List<GameView>();
  35.         private static GUIStyle s_GizmoButtonStyle;
  36.         private static GameView s_LastFocusedGameView = null;
  37.         private static Rect s_MainGameViewRect = new Rect(0f, 0f, 640f, 480f);
  38.         private static GUIStyle s_ResolutionWarningStyle;
  39.  
  40.         public GameView()
  41.         {
  42.             base.depthBufferBits = 0x20;
  43.             base.antiAlias = -1;
  44.             base.autoRepaintOnSceneChange = true;
  45.             this.m_TargetDisplay = 0;
  46.         }
  47.  
  48.         public virtual void AddItemsToMenu(GenericMenu menu)
  49.         {
  50.             if (RenderDoc.IsInstalled() && !RenderDoc.IsLoaded())
  51.             {
  52.                 menu.AddItem(new GUIContent("Load RenderDoc"), false, new GenericMenu.MenuFunction(this.LoadRenderDoc));
  53.             }
  54.         }
  55.  
  56.         private void AllowCursorLockAndHide(bool enable)
  57.         {
  58.             Unsupported.SetAllowCursorLock(enable);
  59.             Unsupported.SetAllowCursorHide(enable);
  60.         }
  61.  
  62.         private void DelayedGameViewChanged()
  63.         {
  64.             EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Combine(EditorApplication.update, new EditorApplication.CallbackFunction(this.DoDelayedGameViewChanged));
  65.         }
  66.  
  67.         private void DoDelayedGameViewChanged()
  68.         {
  69.             this.GameViewAspectWasChanged();
  70.             EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.DoDelayedGameViewChanged));
  71.         }
  72.  
  73.         private void DoToolbarGUI()
  74.         {
  75.             ScriptableSingleton<GameViewSizes>.instance.RefreshStandaloneAndWebplayerDefaultSizes();
  76.             if (ScriptableSingleton<GameViewSizes>.instance.GetChangeID() != this.m_SizeChangeID)
  77.             {
  78.                 this.EnsureSelectedSizeAreValid();
  79.                 this.m_SizeChangeID = ScriptableSingleton<GameViewSizes>.instance.GetChangeID();
  80.             }
  81.             GUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[0]);
  82.             if (this.ShouldShowMultiDisplayOption())
  83.             {
  84.                 GUILayoutOption[] optionArray1 = new GUILayoutOption[] { GUILayout.Width(80f) };
  85.                 int num = EditorGUILayout.Popup(this.m_TargetDisplay, DisplayUtility.GetDisplayNames(), EditorStyles.toolbarPopup, optionArray1);
  86.                 EditorGUILayout.Space();
  87.                 if (num != this.m_TargetDisplay)
  88.                 {
  89.                     this.m_TargetDisplay = num;
  90.                     this.GameViewAspectWasChanged();
  91.                 }
  92.             }
  93.             GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(160f) };
  94.             EditorGUILayout.GameViewSizePopup(currentSizeGroupType, this.selectedSizeIndex, new Action<int, object>(this.SelectionCallback), EditorStyles.toolbarDropDown, options);
  95.             if (FrameDebuggerUtility.IsLocalEnabled())
  96.             {
  97.                 GUILayout.FlexibleSpace();
  98.                 Color color = GUI.color;
  99.                 GUI.color *= AnimationMode.animatedPropertyColor;
  100.                 GUILayout.Label("Frame Debugger on", EditorStyles.miniLabel, new GUILayoutOption[0]);
  101.                 GUI.color = color;
  102.                 if (Event.current.type == EventType.Repaint)
  103.                 {
  104.                     FrameDebuggerWindow.RepaintAll();
  105.                 }
  106.             }
  107.             GUILayout.FlexibleSpace();
  108.             if (RenderDoc.IsLoaded())
  109.             {
  110.                 EditorGUI.BeginDisabledGroup(!RenderDoc.IsSupported());
  111.                 if (GUILayout.Button(this.renderdocContent, EditorStyles.toolbarButton, new GUILayoutOption[0]))
  112.                 {
  113.                     base.m_Parent.CaptureRenderDoc();
  114.                     GUIUtility.ExitGUI();
  115.                 }
  116.                 EditorGUI.EndDisabledGroup();
  117.             }
  118.             this.m_MaximizeOnPlay = GUILayout.Toggle(this.m_MaximizeOnPlay, "Maximize on Play", EditorStyles.toolbarButton, new GUILayoutOption[0]);
  119.             EditorUtility.audioMasterMute = GUILayout.Toggle(EditorUtility.audioMasterMute, "Mute audio", EditorStyles.toolbarButton, new GUILayoutOption[0]);
  120.             this.m_Stats = GUILayout.Toggle(this.m_Stats, "Stats", EditorStyles.toolbarButton, new GUILayoutOption[0]);
  121.             Rect position = GUILayoutUtility.GetRect(this.gizmosContent, s_GizmoButtonStyle);
  122.             Rect rect2 = new Rect(position.xMax - s_GizmoButtonStyle.border.right, position.y, (float) s_GizmoButtonStyle.border.right, position.height);
  123.             if (EditorGUI.ButtonMouseDown(rect2, GUIContent.none, FocusType.Passive, GUIStyle.none) && AnnotationWindow.ShowAtPosition(GUILayoutUtility.topLevel.GetLast(), true))
  124.             {
  125.                 GUIUtility.ExitGUI();
  126.             }
  127.             this.m_Gizmos = GUI.Toggle(position, this.m_Gizmos, this.gizmosContent, s_GizmoButtonStyle);
  128.             GUILayout.EndHorizontal();
  129.         }
  130.  
  131.         private void EnsureSelectedSizeAreValid()
  132.         {
  133.             int length = Enum.GetNames(typeof(GameViewSizeGroupType)).Length;
  134.             if (this.m_SelectedSizes.Length != length)
  135.             {
  136.                 Array.Resize<int>(ref this.m_SelectedSizes, length);
  137.             }
  138.             IEnumerator enumerator = Enum.GetValues(typeof(GameViewSizeGroupType)).GetEnumerator();
  139.             try
  140.             {
  141.                 while (enumerator.MoveNext())
  142.                 {
  143.                     GameViewSizeGroupType current = (GameViewSizeGroupType) ((int) enumerator.Current);
  144.                     GameViewSizeGroup group = ScriptableSingleton<GameViewSizes>.instance.GetGroup(current);
  145.                     int index = (int) current;
  146.                     this.m_SelectedSizes[index] = Mathf.Clamp(this.m_SelectedSizes[index], 0, group.GetTotalCount() - 1);
  147.                 }
  148.             }
  149.             finally
  150.             {
  151.                 IDisposable disposable = enumerator as IDisposable;
  152.                 if (disposable == null)
  153.                 {
  154.                 }
  155.                 disposable.Dispose();
  156.             }
  157.         }
  158.  
  159.         private void GameViewAspectWasChanged()
  160.         {
  161.             base.SetInternalGameViewRect(GetConstrainedGameViewRenderRect(this.gameViewRenderRect, this.selectedSizeIndex));
  162.             EditorApplication.SetSceneRepaintDirty();
  163.         }
  164.  
  165.         internal Rect GetConstrainedGameViewRenderRect()
  166.         {
  167.             if (base.m_Parent == null)
  168.             {
  169.                 return s_MainGameViewRect;
  170.             }
  171.             base.m_Pos = base.m_Parent.borderSize.Remove(base.m_Parent.position);
  172.             return EditorGUIUtility.PixelsToPoints(GetConstrainedGameViewRenderRect(EditorGUIUtility.PointsToPixels(this.gameViewRenderRect), this.selectedSizeIndex));
  173.         }
  174.  
  175.         internal static Rect GetConstrainedGameViewRenderRect(Rect renderRect, int sizeIndex)
  176.         {
  177.             bool flag;
  178.             return GetConstrainedGameViewRenderRect(renderRect, sizeIndex, out flag);
  179.         }
  180.  
  181.         internal static Rect GetConstrainedGameViewRenderRect(Rect renderRect, int sizeIndex, out bool fitsInsideRect)
  182.         {
  183.             return GameViewSizes.GetConstrainedRect(renderRect, currentSizeGroupType, sizeIndex, out fitsInsideRect);
  184.         }
  185.  
  186.         internal static GameView GetMainGameView()
  187.         {
  188.             if (((s_LastFocusedGameView == null) && (s_GameViews != null)) && (s_GameViews.Count > 0))
  189.             {
  190.                 s_LastFocusedGameView = s_GameViews[0];
  191.             }
  192.             return s_LastFocusedGameView;
  193.         }
  194.  
  195.         internal static Rect GetMainGameViewRenderRect()
  196.         {
  197.             GameView mainGameView = GetMainGameView();
  198.             if (mainGameView != null)
  199.             {
  200.                 s_MainGameViewRect = mainGameView.GetConstrainedGameViewRenderRect();
  201.             }
  202.             return s_MainGameViewRect;
  203.         }
  204.  
  205.         internal static Vector2 GetSizeOfMainGameView()
  206.         {
  207.             Rect mainGameViewRenderRect = GetMainGameViewRenderRect();
  208.             return new Vector2(mainGameViewRenderRect.width, mainGameViewRenderRect.height);
  209.         }
  210.  
  211.         public bool IsShowingGizmos()
  212.         {
  213.             return this.m_Gizmos;
  214.         }
  215.  
  216.         private void LoadRenderDoc()
  217.         {
  218.             if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
  219.             {
  220.                 RenderDoc.Load();
  221.                 ShaderUtil.RecreateGfxDevice();
  222.             }
  223.         }
  224.  
  225.         public void OnDisable()
  226.         {
  227.             s_GameViews.Remove(this);
  228.             this.m_ResolutionTooLargeWarning.valueChanged.RemoveListener(new UnityAction(this.Repaint));
  229.             EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.DoDelayedGameViewChanged));
  230.         }
  231.  
  232.         public void OnEnable()
  233.         {
  234.             base.depthBufferBits = 0x20;
  235.             base.titleContent = base.GetLocalizedTitleContent();
  236.             this.EnsureSelectedSizeAreValid();
  237.             this.renderdocContent = EditorGUIUtility.IconContent("renderdoc", "Capture|Capture the current view and open in RenderDoc");
  238.             base.dontClearBackground = true;
  239.             s_GameViews.Add(this);
  240.             this.m_ResolutionTooLargeWarning.valueChanged.AddListener(new UnityAction(this.Repaint));
  241.             this.m_ResolutionTooLargeWarning.speed = 0.3f;
  242.         }
  243.  
  244.         private void OnFocus()
  245.         {
  246.             this.AllowCursorLockAndHide(true);
  247.             s_LastFocusedGameView = this;
  248.             InternalEditorUtility.OnGameViewFocus(true);
  249.         }
  250.  
  251.         private void OnGUI()
  252.         {
  253.             bool flag;
  254.             if (s_GizmoButtonStyle == null)
  255.             {
  256.                 s_GizmoButtonStyle = "GV Gizmo DropDown";
  257.                 s_ResolutionWarningStyle = new GUIStyle("PreOverlayLabel");
  258.                 s_ResolutionWarningStyle.alignment = TextAnchor.UpperLeft;
  259.                 s_ResolutionWarningStyle.padding = new RectOffset(6, 6, 1, 1);
  260.             }
  261.             this.DoToolbarGUI();
  262.             Rect gameViewRenderRect = this.gameViewRenderRect;
  263.             Rect rect = GetConstrainedGameViewRenderRect(EditorGUIUtility.PointsToPixels(gameViewRenderRect), this.selectedSizeIndex, out flag);
  264.             Rect rect4 = EditorGUIUtility.PixelsToPoints(rect);
  265.             Rect rect5 = GUIClip.Unclip(rect4);
  266.             Rect cameraRect = EditorGUIUtility.PointsToPixels(rect5);
  267.             base.SetInternalGameViewRect(rect5);
  268.             EditorGUIUtility.AddCursorRect(rect4, MouseCursor.CustomCursor);
  269.             EventType type = Event.current.type;
  270.             if ((type == EventType.MouseDown) && gameViewRenderRect.Contains(Event.current.mousePosition))
  271.             {
  272.                 this.AllowCursorLockAndHide(true);
  273.             }
  274.             else if ((type == EventType.KeyDown) && (Event.current.keyCode == KeyCode.Escape))
  275.             {
  276.                 Unsupported.SetAllowCursorLock(false);
  277.             }
  278.             switch (type)
  279.             {
  280.                 case EventType.Layout:
  281.                 case EventType.Used:
  282.                     break;
  283.  
  284.                 case EventType.Repaint:
  285.                 {
  286.                     bool flag2 = EditorGUIUtility.IsDisplayReferencedByCameras(this.m_TargetDisplay);
  287.                     if ((!this.currentGameViewSize.isFreeAspectRatio || !InternalEditorUtility.HasFullscreenCamera()) || !flag2)
  288.                     {
  289.                         GUI.Box(gameViewRenderRect, GUIContent.none, "GameViewBackground");
  290.                         if (!InternalEditorUtility.HasFullscreenCamera())
  291.                         {
  292.                             float[] singleArray1 = new float[] { 30f, (gameViewRenderRect.height / 2f) - 10f, gameViewRenderRect.height - 10f };
  293.                             foreach (int num in singleArray1)
  294.                             {
  295.                                 GUI.Label(new Rect((gameViewRenderRect.width / 2f) - 100f, (float) num, 300f, 20f), "Scene is missing a fullscreen camera", "WhiteLargeLabel");
  296.                             }
  297.                         }
  298.                     }
  299.                     Vector2 vector = GUIUtility.s_EditorScreenPointOffset;
  300.                     GUIUtility.s_EditorScreenPointOffset = Vector2.zero;
  301.                     SavedGUIState state = SavedGUIState.Create();
  302.                     if (this.ShouldShowMultiDisplayOption())
  303.                     {
  304.                         EditorGUIUtility.RenderGameViewCamerasInternal(cameraRect, this.m_TargetDisplay, this.m_Gizmos, true);
  305.                     }
  306.                     else
  307.                     {
  308.                         EditorGUIUtility.RenderGameViewCamerasInternal(cameraRect, 0, this.m_Gizmos, true);
  309.                     }
  310.                     GL.sRGBWrite = false;
  311.                     state.ApplyAndForget();
  312.                     GUIUtility.s_EditorScreenPointOffset = vector;
  313.                     break;
  314.                 }
  315.                 default:
  316.                 {
  317.                     if (WindowLayout.s_MaximizeKey.activated && (!EditorApplication.isPlaying || EditorApplication.isPaused))
  318.                     {
  319.                         return;
  320.                     }
  321.                     bool flag3 = rect4.Contains(Event.current.mousePosition);
  322.                     if ((Event.current.rawType == EventType.MouseDown) && !flag3)
  323.                     {
  324.                         return;
  325.                     }
  326.                     Vector2 mousePosition = Event.current.mousePosition;
  327.                     Vector2 position = mousePosition - rect4.position;
  328.                     position = EditorGUIUtility.PointsToPixels(position);
  329.                     Event.current.mousePosition = position;
  330.                     Event.current.displayIndex = this.m_TargetDisplay;
  331.                     EditorGUIUtility.QueueGameViewInputEvent(Event.current);
  332.                     bool flag4 = true;
  333.                     if ((Event.current.rawType == EventType.MouseUp) && !flag3)
  334.                     {
  335.                         flag4 = false;
  336.                     }
  337.                     switch (type)
  338.                     {
  339.                         case EventType.ExecuteCommand:
  340.                         case EventType.ValidateCommand:
  341.                             flag4 = false;
  342.                             break;
  343.                     }
  344.                     if (flag4)
  345.                     {
  346.                         Event.current.Use();
  347.                     }
  348.                     else
  349.                     {
  350.                         Event.current.mousePosition = mousePosition;
  351.                     }
  352.                     break;
  353.                 }
  354.             }
  355.             this.ShowResolutionWarning(new Rect(gameViewRenderRect.x, gameViewRenderRect.y, 200f, 20f), flag, rect.size);
  356.             if (this.m_Stats)
  357.             {
  358.                 GameViewGUI.GameViewStatsGUI();
  359.             }
  360.         }
  361.  
  362.         private void OnLostFocus()
  363.         {
  364.             if (!EditorApplicationLayout.IsInitializingPlaymodeLayout())
  365.             {
  366.                 this.AllowCursorLockAndHide(false);
  367.             }
  368.             InternalEditorUtility.OnGameViewFocus(false);
  369.         }
  370.  
  371.         internal override void OnResized()
  372.         {
  373.             this.DelayedGameViewChanged();
  374.         }
  375.  
  376.         private void OnSelectionChange()
  377.         {
  378.             if (this.m_Gizmos)
  379.             {
  380.                 base.Repaint();
  381.             }
  382.         }
  383.  
  384.         public void OnValidate()
  385.         {
  386.             this.EnsureSelectedSizeAreValid();
  387.         }
  388.  
  389.         public static void RepaintAll()
  390.         {
  391.             if (s_GameViews != null)
  392.             {
  393.                 foreach (GameView view in s_GameViews)
  394.                 {
  395.                     view.Repaint();
  396.                 }
  397.             }
  398.         }
  399.  
  400.         private void SelectionCallback(int indexClicked, object objectSelected)
  401.         {
  402.             if (indexClicked != this.selectedSizeIndex)
  403.             {
  404.                 this.selectedSizeIndex = indexClicked;
  405.                 base.dontClearBackground = true;
  406.                 this.GameViewAspectWasChanged();
  407.             }
  408.         }
  409.  
  410.         private bool ShouldShowMultiDisplayOption()
  411.         {
  412.             GUIContent[] displayNames = ModuleManager.GetDisplayNames(EditorUserBuildSettings.activeBuildTarget.ToString());
  413.             return ((BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget) == BuildTargetGroup.Standalone) || (displayNames != null));
  414.         }
  415.  
  416.         private void ShowResolutionWarning(Rect position, bool fitsInsideRect, Vector2 shownSize)
  417.         {
  418.             if (!fitsInsideRect && (shownSize != this.m_ShownResolution))
  419.             {
  420.                 this.m_ShownResolution = shownSize;
  421.                 this.m_ResolutionTooLargeWarning.value = true;
  422.             }
  423.             if (fitsInsideRect && (this.m_ShownResolution != Vector2.zero))
  424.             {
  425.                 this.m_ShownResolution = Vector2.zero;
  426.                 this.m_ResolutionTooLargeWarning.value = false;
  427.             }
  428.             this.m_ResolutionTooLargeWarning.target = !fitsInsideRect && !EditorApplication.isPlaying;
  429.             if (this.m_ResolutionTooLargeWarning.faded > 0f)
  430.             {
  431.                 Color color = GUI.color;
  432.                 GUI.color = new Color(1f, 1f, 1f, Mathf.Clamp01(this.m_ResolutionTooLargeWarning.faded * 2f));
  433.                 EditorGUI.DropShadowLabel(position, string.Format("Using resolution {0}x{1}", shownSize.x, shownSize.y), s_ResolutionWarningStyle);
  434.                 GUI.color = color;
  435.             }
  436.         }
  437.  
  438.         private GameViewSize currentGameViewSize
  439.         {
  440.             get
  441.             {
  442.                 return ScriptableSingleton<GameViewSizes>.instance.currentGroup.GetGameViewSize(this.selectedSizeIndex);
  443.             }
  444.         }
  445.  
  446.         private static GameViewSizeGroupType currentSizeGroupType
  447.         {
  448.             get
  449.             {
  450.                 return ScriptableSingleton<GameViewSizes>.instance.currentGroupType;
  451.             }
  452.         }
  453.  
  454.         private Rect gameViewRenderRect
  455.         {
  456.             get
  457.             {
  458.                 return new Rect(0f, 17f, base.position.width, base.position.height - 17f);
  459.             }
  460.         }
  461.  
  462.         public bool maximizeOnPlay
  463.         {
  464.             get
  465.             {
  466.                 return this.m_MaximizeOnPlay;
  467.             }
  468.             set
  469.             {
  470.                 this.m_MaximizeOnPlay = value;
  471.             }
  472.         }
  473.  
  474.         private int selectedSizeIndex
  475.         {
  476.             get
  477.             {
  478.                 return this.m_SelectedSizes[(int) currentSizeGroupType];
  479.             }
  480.             set
  481.             {
  482.                 this.m_SelectedSizes[(int) currentSizeGroupType] = value;
  483.             }
  484.         }
  485.     }
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement