示例#1
0
文件: Game.cs 项目: nolovelost/le-mat
    public void Update()
    {
#if UNITY_EDITOR
        // Ugly hack to force focus to game view when using scriptable renderloops.
        if (Time.frameCount < 4)
        {
            try
            {
                var gameViewType = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView");
                var gameView     = (EditorWindow)Resources.FindObjectsOfTypeAll(gameViewType)[0];
                gameView.Focus();
            }
            catch (System.Exception) { /* too bad */ }
        }
#endif

        frameTime = (double)m_Clock.ElapsedTicks / m_StopwatchFrequency;

        // Switch game loop if needed
        if (m_RequestedGameLoopTypes.Count > 0)
        {
            bool initSucceeded = false;
            for (int i = 0; i < m_RequestedGameLoopTypes.Count; i++)
            {
                try
                {
                    IGameLoop gameLoop = (IGameLoop)System.Activator.CreateInstance(m_RequestedGameLoopTypes[i]);
                    initSucceeded = gameLoop.Init(m_RequestedGameLoopArguments[i]);
                    if (!initSucceeded)
                    {
                        break;
                    }

                    m_gameLoops.Add(gameLoop);
                }
                catch (System.Exception e)
                {
                    GameDebug.Log(string.Format("Game loop initialization threw exception : ({0})\n{1}", e.Message, e.StackTrace));
                }
            }


            if (!initSucceeded)
            {
                ShutdownGameLoops();

                GameDebug.Log("Game loop initialization failed ... reverting to boot loop");
            }

            m_RequestedGameLoopTypes.Clear();
            m_RequestedGameLoopArguments.Clear();
        }

        try
        {
            if (!m_ErrorState)
            {
                foreach (var gameLoop in m_gameLoops)
                {
                    gameLoop.Update();
                }
            }
        }
        catch (System.Exception e)
        {
            HandleGameloopException(e);
            throw;
        }

        Console.ConsoleUpdate();

        WindowFocusUpdate();

        UpdateCPUStats();

        endUpdateEvent?.Invoke();
    }
示例#2
0
    public void Update()
    {
        if (!m_isHeadless)
        {
            RenderSettings.Update();
        }

        // TODO (petera) remove this hack once we know exactly when renderer is available...
        if (!pipeSetup)
        {
            var hdpipe = RenderPipelineManager.currentPipeline as HDRenderPipeline;
            if (hdpipe != null)
            {
                var layer = LayerMask.NameToLayer("PostProcess Volumes");
                if (layer == -1)
                {
                    GameDebug.LogWarning("Unable to find layer mask for camera fader");
                }
                else
                {
                    var gameObject = new GameObject()
                    {
                        name      = "Game Quick Volume",
                        layer     = layer,
                        hideFlags = HideFlags.HideAndDontSave
                    };

                    m_ExposureVolume          = gameObject.AddComponent <Volume>();
                    m_ExposureVolume.priority = 100.0f;
                    m_ExposureVolume.isGlobal = true;
                    var profile = m_ExposureVolume.profile;

                    m_Exposure        = profile.Add <Exposure>();
                    m_Exposure.active = false;
                    m_Exposure.mode.Override(ExposureMode.Automatic);
                    m_Exposure.compensation.Override(0);
                }

                pipeSetup = true;
            }
        }
        if (m_ExposureReleaseCount > 0)
        {
            m_ExposureReleaseCount--;
            if (m_ExposureReleaseCount == 0)
            {
                BlackFade(false);
            }
        }

        GameApp.CameraStack.Update();


#if UNITY_EDITOR
        // Ugly hack to force focus to game view when using scriptable renderloops.
        if (Time.frameCount < 4)
        {
            try
            {
                var gameViewType = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView");
                var gameView     = (EditorWindow)Resources.FindObjectsOfTypeAll(gameViewType)[0];
                gameView.Focus();
            }
            catch (System.Exception) { /* too bad */ }
        }
#endif

        frameTime  = (double)m_Clock.ElapsedTicks / m_StopwatchFrequency;
        frameCount = Time.frameCount;

        GameDebug.SetFrameCount(frameCount);
        Console.SetFrameTime(frameTime);;

        // Switch game loop if needed
        if (m_RequestedGameLoopTypes.Count > 0)
        {
            // Multiple running gameloops only allowed in editor
#if !UNITY_EDITOR
            ShutdownGameLoops();
#endif
            bool initSucceeded = false;
            for (int i = 0; i < m_RequestedGameLoopTypes.Count; i++)
            {
                try
                {
                    IGameLoop gameLoop = (IGameLoop)System.Activator.CreateInstance(m_RequestedGameLoopTypes[i]);
                    initSucceeded = gameLoop.Init(m_RequestedGameLoopArguments[i]);
                    Debug.Log("Game initialization succeeded: " + initSucceeded);
                    if (!initSucceeded)
                    {
                        break;
                    }

                    m_gameLoops.Add(gameLoop);
                }
                catch (System.Exception e)
                {
                    GameDebug.LogError(string.Format("Game loop initialization threw exception : ({0})\n{1}", e.Message, e.StackTrace));
                }
            }


            if (!initSucceeded)
            {
                ShutdownGameLoops();

                GameDebug.LogError("Game loop initialization failed ... reverting to boot loop");
            }

            m_RequestedGameLoopTypes.Clear();
            m_RequestedGameLoopArguments.Clear();
        }

        try
        {
            if (!m_ErrorState)
            {
                foreach (var gameLoop in m_gameLoops)
                {
                    gameLoop.Update();
                }
                levelManager.Update();
            }
        }
        catch (System.Exception e)
        {
            HandleGameloopException(e);
            throw;
        }

        if (SoundSystem.Instance != null)
        {
            SoundSystem.Instance.Update();
        }

        if (clientFrontend != null)
        {
            clientFrontend.UpdateGame();
        }

        Console.ConsoleUpdate();

        bool menusShowing = (clientFrontend != null && clientFrontend.menuShowing != ClientFrontend.MenuShowing.None);
        InputSystem.WindowFocusUpdate(menusShowing);

        UpdateCPUStats();
        UpdateEntityStats();

        sqpClient.Update();

        endUpdateEvent?.Invoke();

        /* HACKY WAY TO LOOK AT MONOBEHAVIOURS LEFT
         * if (Time.frameCount % 1000 == 100)
         * {
         *  var mbs = FindObjectsOfType(typeof(MonoBehaviour));
         *  numMBS = mbs.Length;
         *  var h = new HashSet<Type>();
         *  foreach (var mb in mbs)
         *  {
         *      h.Add(mb.GetType());
         *  }
         *  foreach(var t in h)
         *      GameDebug.Log(":" + t);
         *  numUMBS = h.Count();
         * }
         * Overlay.Managed.Write(2, 4, "Monobehaviours left {0} ({1})", numMBS, numUMBS);
         */
    }
示例#3
0
    public void Update()
    {
        if (!m_isHeadless)
        {
            RenderSettings.Update();
        }

        // TODO (petera) remove this hack once we know exactly when renderer is available...
        if (!pipeSetup)
        {
            var hdpipe = RenderPipelineManager.currentPipeline as HDRenderPipeline;
            if (hdpipe != null)
            {
//                hdpipe.DebugLayer2DCallback = DebugOverlay.Render;
//                hdpipe.DebugLayer3DCallback = DebugOverlay.Render3D;

                var layer = LayerMask.NameToLayer("PostProcess Volumes");
                if (layer == -1)
                {
                    GameDebug.LogWarning("Unable to find layer mask for camera fader");
                }
                else
                {
                    m_Exposure        = ScriptableObject.CreateInstance <AutoExposure>();
                    m_Exposure.active = false;
                    m_Exposure.enabled.Override(true);
                    m_Exposure.keyValue.Override(0);
                    m_ExposureVolume = PostProcessManager.instance.QuickVolume(layer, 100.0f, m_Exposure);
                }

                pipeSetup = true;
            }
        }
        if (m_ExposureReleaseCount > 0)
        {
            m_ExposureReleaseCount--;
            if (m_ExposureReleaseCount == 0)
            {
                BlackFade(false);
            }
        }

        // Verify if camera was somehow destroyed and pop it
        if (m_CameraStack.Count > 1 && m_CameraStack[m_CameraStack.Count - 1] == null)
        {
            PopCamera(null);
        }

#if UNITY_EDITOR
        // Ugly hack to force focus to game view when using scriptable renderloops.
        if (Time.frameCount < 4)
        {
            try
            {
                var gameViewType = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView");
                var gameView     = (EditorWindow)Resources.FindObjectsOfTypeAll(gameViewType)[0];
                gameView.Focus();
            }
            catch (System.Exception) { /* too bad */ }
        }
#endif

        frameTime = (double)m_Clock.ElapsedTicks / m_StopwatchFrequency;

        // Switch game loop if needed
        if (m_RequestedGameLoopTypes.Count > 0)
        {
            // Multiple running gameloops only allowed in editor
#if !UNITY_EDITOR
            ShutdownGameLoops();
#endif
            bool initSucceeded = false;
            for (int i = 0; i < m_RequestedGameLoopTypes.Count; i++)
            {
                try
                {
                    IGameLoop gameLoop = (IGameLoop)System.Activator.CreateInstance(m_RequestedGameLoopTypes[i]);
                    initSucceeded = gameLoop.Init(m_RequestedGameLoopArguments[i]);
                    if (!initSucceeded)
                    {
                        break;
                    }

                    m_gameLoops.Add(gameLoop);
                }
                catch (System.Exception e)
                {
                    GameDebug.Log(string.Format("Game loop initialization threw exception : ({0})\n{1}", e.Message, e.StackTrace));
                }
            }


            if (!initSucceeded)
            {
                ShutdownGameLoops();

                GameDebug.Log("Game loop initialization failed ... reverting to boot loop");
            }

            m_RequestedGameLoopTypes.Clear();
            m_RequestedGameLoopArguments.Clear();
        }

        try
        {
            if (!m_ErrorState)
            {
                foreach (var gameLoop in m_gameLoops)
                {
                    gameLoop.Update();
                }
                levelManager.Update();
            }
        }
        catch (System.Exception e)
        {
            HandleGameloopException(e);
            throw;
        }

        if (m_SoundSystem != null)
        {
            m_SoundSystem.Update();
        }

        if (clientFrontend != null)
        {
            clientFrontend.UpdateGame();
        }

        Console.ConsoleUpdate();

        WindowFocusUpdate();

        UpdateCPUStats();

        sqpClient.Update();

        endUpdateEvent?.Invoke();
    }
示例#4
0
    private void Update()
    {
        if (_requestedGameLoopTypes.Count > 0)
        {
            // Multiple running gameloops only allowed in editor
#if !UNITY_EDITOR
            ShutdownGameLoops();
#endif
            bool initSucceeded = true;
            for (int i = 0; i < _requestedGameLoopTypes.Count; i++)
            {
                try {
                    IGameLoop gameLoop = (IGameLoop)System.Activator.CreateInstance(_requestedGameLoopTypes[i]);
                    initSucceeded = gameLoop.Init(_requestedGameLoopArgs[i]);
                    if (!initSucceeded)
                    {
                        break;
                    }

                    _gameLoops.Add(gameLoop);
                }catch (System.Exception e) {
                    GameDebug.Log(string.Format("Game loop initialization threw exception : ({0})\n{1}", e.Message, e.StackTrace));
                }
            }

            if (!initSucceeded)
            {
                ShutdownGameLoops();

                GameDebug.Log("Game loop initialization failed ... reverting to boot loop");
            }

            _requestedGameLoopArgs.Clear();
            _requestedGameLoopTypes.Clear();
        }

        // Verify if camera was somehow destroyed and pop it
        if (m_CameraStack.Count > 1 && m_CameraStack[m_CameraStack.Count - 1] == null)
        {
            PopCamera(null);
        }

        frameTime = (double)Clock.ElapsedTicks / m_StopwatchFrequency;

        try {
            if (!_errorState)
            {
                foreach (var gameLoop in _gameLoops)
                {
                    gameLoop.Update();
                }
                levelManager.Update();
            }
        } catch (System.Exception e) {
            HandleGameloopException(e);
        }

        Console.ConsoleUpdate();

        WindowFocusUpdate();
    }