Search Unity

Turn on vsync in the editor Scene window.

Discussion in 'Editor & General Support' started by Freddy888, Oct 28, 2014.

  1. Ukounu

    Ukounu

    Joined:
    Nov 2, 2019
    Posts:
    208
    The same issue was apparently reported here (not resolved):

    https://forum.unity.com/threads/sup...r-creates-coil-whine-is-this-harmful.1161167/

    Does anybody have a script to reliably & continuously output FPS in editor scene view window (not in game/play mode window), so that I can properly replicate and report this issue?

    Code (CSharp):
    1.  
    2.  using UnityEditor;
    3.  using UnityEngine;
    4.  [InitializeOnLoad]
    5.  class MyClass
    6.  {
    7. public static float deltaTime = 0.0f;
    8. static MyClass ()
    9. {
    10.     EditorApplication.update += Update;
    11. }
    12. static void Update ()
    13. {
    14. deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
    15. float fps = 1.0f / deltaTime;
    16. Debug.Log(fps);
    17. }
    18. }
    19.  
    I tried using this code to debug Editor FPS, but by some reason it doesn't work in scene view mode (shows random values like 0.01 ~ 10). Works correctly only in play mode, which is not what I need. Any help is appreciated.
     
    Last edited: Feb 6, 2022
  2. SceneView doesn't update regularly normally. If you want to update it in fixed intervals (as opposed when something is happening), then you should check the
    Always Refresh
    option.
    screenshot.png
     
  3. Ukounu

    Ukounu

    Joined:
    Nov 2, 2019
    Posts:
    208
    I think your screenshot is from a newer version of Unity, because in 2019.4.35 there is no such option. Also, the script I posted above does force Unity editor to update every frame (as evidenced by continuous output from Debug.Log in console). But FPS values the script outputs are wrong, by some reason.
     
  4. Try something like this. Maybe this explains how the editor works. The update loop and the render loop aren't the same thing.
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [InitializeOnLoad]
    5. public static class Test
    6. {
    7.     private static double _time = 0d;
    8.     private static ulong _frames = 0;
    9.     private static ulong _renderFrames = 0;
    10.     private static double _lastTime = 0d;
    11.  
    12.     static Test()
    13.     {
    14.         _lastTime = EditorApplication.timeSinceStartup;
    15.        EditorApplication.update += OnUpdate;
    16.        Camera.onPostRender += OnRender;
    17.     }
    18.  
    19.     private static void OnRender(Camera cam)
    20.     {
    21.         if (cam == SceneView.lastActiveSceneView.camera) _renderFrames++;
    22.     }
    23.  
    24.     private static void OnUpdate()
    25.     {
    26.         var currentTime = EditorApplication.timeSinceStartup;
    27.         var deltaTime =  currentTime - _lastTime;
    28.  
    29.         _time += deltaTime;
    30.         _frames++;
    31.         _lastTime = currentTime;
    32.         if (_time < 1f) return;
    33.  
    34.         Debug.Log($"{_renderFrames} renders and {_frames} updates per second.");
    35.         _frames = 0;
    36.         _renderFrames = 0;
    37.         _time -= 1f;
    38.     }
    39. }
     
    Ukounu likes this.
  5. Ukounu

    Ukounu

    Joined:
    Nov 2, 2019
    Posts:
    208
    Thank you, it works now. Using your script I was able to reliably reproduce the issue and submitted a bug report to Unity. Here is my report, in case somebody is curious:

     
  6. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    I think THAT is the actual bug, because there is a specific setting for that in Edit -> Preferences -> General -> Ineraction Mode: Monitor Refresh Rate

    From the manual:
     
  7. Ukounu

    Ukounu

    Joined:
    Nov 2, 2019
    Posts:
    208
    Indeed. I didn't even know about that setting, since it never was mentioned in this and other similar threads about the issue. I tested it now and it solves the issue.

    Oh well, I hope that whoever from Unity team reads my report will understand what the actual bug is about. If "VSync (Game view only)" option was meant to also affect scene view, it should keep working after exiting play mode, and if it wasn't meant to affect scene view, it shouldn't work in that window at all.
     
    Last edited: Feb 7, 2022
  8. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    You can add a comment to your bug report to specify this. :)
     
    Ukounu likes this.
  9. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    @Ukounu - I'm not aware of a bug for this, so feel free to submit one. I haven't been able to test in the latest 2021.2 due to another bug that has kept me from updating. But the VSync issue is definitely still there in 2021.2 beta, and I really hope Unity will fix it.
     
  10. Ryunigia

    Ryunigia

    Joined:
    Mar 25, 2013
    Posts:
    1
  11. alti

    alti

    Joined:
    Jan 8, 2014
    Posts:
    94
    Set target frame rate, if you wanna lock the frame rate in the editor.
     
  12. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    This does not work unfortunately. Nothing works that I've found up to 2022.1 except the reflection based called to re-EnableVSync() and that only works for a while before it stops working again.
     
  13. in0finite

    in0finite

    Joined:
    Oct 23, 2017
    Posts:
    19
    After trying all suggested solutions, I still can not limit the FPS in edit-mode.

    As soon as I press a mouse inside SceneView, my CPU/GPU goes up to 100% usage, with Unity trying to render SceneView as fast as possible giving huge frame rates (probably more than 1000 FPS) for no reason. This makes it impractical to explore your scenes in SceneView.

    I don't see the point of burning computer's resources to render at such high frame rates. And this happens even for empty scenes.

    Therefore, I created a script which limits FPS of Editor during edit-mode only:
    https://github.com/in0finite/UGameC...690d959b29cf9ce641c4/FpsLimiterForEditMode.cs

    It uses
    Thread.Sleep()
    to limit FPS if it is higher than configured value (60 by default).

    This way, the entire Editor process' FPS will be limited and none of the windows will be able to render above configured FPS.
     
    MirrorIrorriM likes this.
  14. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    That's one way of doing it, yes :p
    Have you actually checked the preference option here?
    Edit -> Preferences -> General -> Ineraction Mode: Monitor Refresh Rate

    Because it's working fine for me on the scene view. (Unity 2020.3.36f1 LTS)
    If it's not, then it's definitively a regression bug of the editor (which wouldn't surprise me at this point).
     
  15. in0finite

    in0finite

    Joined:
    Oct 23, 2017
    Posts:
    19
    I remember that it didn't work correctly, but can't remember why. Also, if you put a custom refresh rate (eg 30 ms), which is what I need, it ignores it completely and renders at max FPS.

    Basically, I have large scenes, and I would like to limit the SceneView to 30 FPS so that I can explore the world without too much CPU/GPU usage.
     
  16. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Yes, you're right, I just checked and you can't actually limit the scene view to anything lower than the monitor refresh rate. I guess your script is the only solution to have 30fps.
    I guess it's a bug of the editor, but then again, the editor is so full of bugs in the latest Unity versions that I'm not even bothering with filling a bug report, they have much bigger fish to fry honestly, so thanks for the script :)
     
    MirrorIrorriM likes this.
  17. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    editor vsync should just be on by default. Whats the logic behind having it off by default?
     
    MirrorIrorriM and Lars-Steenhoff like this.
  18. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    The "more FPS is more better!" mentality I guess.
    Meanwhile I just want my GPU to be as chill as possible and don't make noise.
     
    MirrorIrorriM and enhawk like this.
  19. dot_entity

    dot_entity

    Joined:
    Nov 2, 2012
    Posts:
    86
    I must completely agree with Ukounu above. Exactly the behaviour described with 2021.3.11f1 and 2022.1.20f1 and I am surprised that this issue is that old. Maybe it does not apply on all (or newer) systems.
    For a fact I use a very old MBP laptop (512MB graphics card) and have not used Unity since when v.2018 was released I think, so I would expect all sort of performance issues. However given this odd behaviour, depending on enabling VSync in game view or not, makes me doubt that that's all there is to it. Moreover, I noticed it with a basic scene with only a default terrain and a 3d box, while back in time in old versions of Unity I had been loading rather complicated scenes with all sorts of elements, without problems. I thought it might be related to recently introduced real-time lighting features, but it does not seem so.
    I was also about to report a bug, though still investigating, but after reading this thread and realising the age of the issue, I am not sure it would help. To add to this conversation, as I said I have a basic testing scene and by having VSync disabled, I noticed that only when a 3d object is selected in the Hierarchy the slow performance (low frame rate in the scene editor) is happening while orbiting the view. In particular, when the object is selected and the mesh renderer is enabled. Either if no object is selected or one with no mesh renderer component is selected the issue is not present. I checked the Profiler and what causes the low frame rate is Semaphore.WaitForSignal process using too much power.
    Any thoughts?
     
  20. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,128
    Semaphore.WaitForSignal is just the thread waiting for a condition to be met. It's not "using too much power". It's "waiting for the rest of the editor to catch up".

    https://forum.unity.com/threads/sem...using-performance-issues.824580/#post-5463879

    Below is an unofficial performance overview someone made covering Unity 2017.2 through 2021.1 with some charts and discussions. Short version is Unity 2018.1 is one of the fastest releases and since then the editor steadily gone down in performance. Meanwhile your hardware has stayed the same. You're seeing a performance bottleneck.

    https://forum.unity.com/threads/unity-2021-1-performance-overview.993186/

    Enabling throttling will help those in this thread with hardware that is more powerful than they need it to be, but it won't help make an older machine better run a more demanding application. You need better hardware for that.
     
    Last edited: Oct 26, 2022
  21. dot_entity

    dot_entity

    Joined:
    Nov 2, 2012
    Posts:
    86
    Firstly, thank you @Ryiah for taking the time to respond. I also apologise for the late reply, but I only found the time to investigate the issue further today.
    Moreover, I'd like to add that I lack the experience to properly look into the technical stuff and I basically shoot in the dark by using trial and error approaches, combined with the information gathered in the forums and common sense. That said, please bear with me in case I write something irrational, even stupid (or in bad English).

    The good news is that I found a workaround to the problem (that is the low frame rate in the Editor due to the Semaphore.WaitForSignal). At that point I was about to write that your comment
    cannot be entirely correct. But I then found the source of the problem, which after all points to the same direction: Incapable hardware (graphics card in particular or driver more specifically).

    The solution:
    It's when I have the Selection Outline gizmo (the orange outline) "enabled" that the frame rate drops. I disable it and WaitForSignal goes away.

    TBH I was feeling the same myself, half-admitted it in my previous post, and I was about to quit looking for a solution, but now, on one hand I am happy that I found a workaround, on the other hand I am curious of what kind of shader this Selection Outline effect uses that does not play well with my graphics card. If I recall, there were some custom made shaders in the Unity v.4 era, that would achieve similar effect...

    PS:
    As a side note, during testing I found this recent post, which at first seemed to be hopeful but, after all, it was enabling the Vsync what eases the problem to some extend and less due to changing the Interaction Mode in preferences.
    As you can see in the pictures below with Vsync "on" the WaitForSignal in the Gfx.FinishRendering process, takes around 66ms, while with the Vsync "off" the WaitForSignal in the Gfx.WaitForPresentOnGfxThread process takes around 212ms. I remind that the spikes occur when moving (orbiting in that case) the camera in scene view.
    image.png VSyncOn
    VsyncOff.png VSyncOff
     
    Last edited: Nov 1, 2022
  22. RistoPaasivirta

    RistoPaasivirta

    Joined:
    Aug 25, 2017
    Posts:
    20
    I found out that when I have my second monitor plugged in the vsync doesn't work in scene view. Works fine in game tho. So make sure that your monitors have the same capabilities if this is an issue to you.
     
  23. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    @RistoPaasivirta I've got two identical monitors and it's still very flaky. Only with my custom (scene view) camera script calling EnableVSync() each time movement starts am I able to get vsync to remain enabled while panning the camera around in the editor.
     
  24. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,524
    Would be willing to share that script? I’m interested to check it out.
     
  25. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    @Lars-Steenhoff Yeah, let me finish the article I started on it a while back. It was definitely more than annoying to get to the method that needs to be called. I'll post a link to the article hopefully by tomorrow.
     
    Lars-Steenhoff likes this.
  26. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    atomicjoe and Lars-Steenhoff like this.
  27. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,524
    Thanks for writing that article, I hope it will bring attention to unity that thats is an annoying issue that could be addressed to make a lot of users happy.

    I hope you will have time to make a complete sample, currently with the two scripts in the project I get one error in SceneViewEnableVSync:

    Assets/_Vsync/SceneViewEnableVSync.cs(9,41): error CS0246: The type or namespace name 'EditorSingleton<>' could not be found (are you missing a using directive or an assembly reference?)

    And I'm not sure how to fix that
    Thanks
     
  28. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    132
    I'll try to get a complete zip this week. You should just need the EditorSingleton.cs class from the other article I referenced.
     
    Lars-Steenhoff likes this.
  29. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,524
    Ah that was easy, thanks now its working!
     
  30. drnecromant

    drnecromant

    Joined:
    Dec 8, 2022
    Posts:
    8
    You saved my life! That was very important to me. I had over 300 FPS on empty scene view when I select object and hold right mouse button to rotate. Nothing hepled me over the internet until I found that thread.

    I spent so many time to stabilize FPS for screen view.
    And yes, bug with re-checking vsync for game mode still here.

    All in all there are only two solutions to limit FPS for scene view:
    1. Re-check vsync in game mode every time unity starts || exit game mode
    2. Edit -> Preferences -> General -> Ineraction Mode: Monitor Refresh Rate
     
    bgrz likes this.
  31. mughees33

    mughees33

    Joined:
    Sep 11, 2023
    Posts:
    4