Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question URP lags in build but runs smoothly in Unity?

Discussion in 'Universal Render Pipeline' started by Spruss76, Jul 30, 2023.

  1. Spruss76

    Spruss76

    Joined:
    Nov 6, 2022
    Posts:
    2
    In my 2D game, no matter which settings I use in URP my game gets really laggy, it even lags slightly if I turn off all post-processing effects in a scene, so basicly just by having URP installed as renderer.
    The strange thing is the game runs perfectly inside Unity, with all post-effects on there is no lagging! BUT, when I run a build it's lagging bigtime! Does anyone know why this is happening?

    I'm running Unity 2022.3.4f1
     
  2. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    257
    This is rather strange in my opinion. I think the opposite of what you describe is happening most of the time. In my project for example the performance in the editor is laggy while in the build it runs smoothly then. That's probably because of the many additional thrings that are happening in the Unity Editor when being in play mode.

    You probably have to dig into that on your own. One thing you can analyze is the frame rate in your build. If the FPS in your game are not constantly 60 you should performance analyze whats happening in your build via profiler. When you have 60 FPS try to exlucde camera movement issues, for example not using late update etc. You might have additional ideas on what to explore further in your project. When you are out of ideas one of the last things I would do is try reproducing the issue in a minimalistic separate project. Use as few parts of your game as possible for the reproduction. Then you are either able to reproduce or not. When not you can try figure out on the differences and may find the cause. When reproducable you have hopefully omitted parts of your game and can now experiment with the repro project to avoid/circumvent the effects you mentioned.

    I also used the repdocution approach in the past and was able to figure out solutions for URP-related issues even with much time spent on it. But especially in this sub-forum of Unity you don't get much help or not an answer with a working fix.
     
    Last edited: Jul 30, 2023
  3. Spruss76

    Spruss76

    Joined:
    Nov 6, 2022
    Posts:
    2
    How do I see FPS in the build? In the editor the game runs at 250+ FPS with URP post-effects, so it's really strange that the game is unplayable in a build due to lagging. The profiler, can I do that on a build or only on the editor? Again, it's only the build that has issues. I'm on a single PC both for the editor and the build, not even changing devices.
     
    Last edited: Jul 30, 2023
  4. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    257
    AFAIK Unity don't offer a possibility to show the FPS in a standalone build out of the box. Developers have to write their own. I personally prefer a counter which really counts FPS and does not do any approximations like commonly found in code snippets. It updates twice per second, which I found reasonable. Here is the MonoBehavior: Eventually you have to change possition and color:
    Code (CSharp):
    1. using System;
    2.  
    3. using UnityEngine;
    4.  
    5. namespace My.Game.Scripts.Graphics
    6. {
    7.   public class FpsDisplay : MonoBehaviour
    8.   {
    9.     private int _1stGradeUpdateCounter;
    10.     private int _2ndGradeUpdateCounter;
    11.     private int _fpsCounter;
    12.     private float _nextFpsTime;
    13.  
    14.     private void OnEnable()
    15.     {
    16.       this._nextFpsTime = Time.unscaledTime;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.       var unscaledTime = Time.unscaledTime;
    22.  
    23.       if (unscaledTime > this._nextFpsTime)
    24.       {
    25.         this._fpsCounter = (this._1stGradeUpdateCounter + this._2ndGradeUpdateCounter);
    26.  
    27.         this._2ndGradeUpdateCounter = this._1stGradeUpdateCounter;
    28.         this._1stGradeUpdateCounter = 1; // The current update always belongs to the next FPS counter label update
    29.  
    30.         this._nextFpsTime += 0.5f;
    31.       }
    32.       else
    33.       {
    34.         this._1stGradeUpdateCounter++;
    35.       }
    36.     }
    37.  
    38.     void OnGUI()
    39.     {
    40.       int w = Screen.width, h = Screen.height;
    41.  
    42.       GUIStyle style = new GUIStyle();
    43.  
    44.       // Change position when necessary
    45.       Rect rect = new Rect(0, 0, w, h * 2 / 100);
    46.       style.alignment = TextAnchor.UpperRight;
    47.       style.fontSize = h * 2 / 100;
    48.       // Change color when necessary
    49.       style.normal.textColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
    50.       GUI.Label(rect, this._fpsCounter.ToString(), style);
    51.     }
    52.   }
    53. }
    But it is very likely not an FPS issue in your case, but just to be sure you should check it.
     
  5. Skiriki

    Skiriki

    Joined:
    Aug 30, 2013
    Posts:
    66
    You'll probably want to use the FrameTimingManager API or (Profiler Recorder API recording it's metrics) to get even more precise stats than just FPS, as that will give you not just the coarse frame rate (which is based on "CPU Total Frame Time"/.cpuFrameTime, which includes time waited for Render Threads & GPU frame flip) but CPU Main Thread and Render Thread time as well as GPU Frame Time.

    Because I have a suspicion that you're GPU bound, or more specifically Fill Rate Bound. That is, if the Game View in the editor is not at the same resolution as the standalone App, it has less pixels to draw.

    Other things to check:
    • is script debugging enabled in the build settings
    • Different quality settings between build and Editor
    • Is the profiler attached?
      • If it is, what does it say takes up time
    • AssetBundle/Addressables used in Build which are simulated in the Editor?
    • Is the Editor running alongside the build on the same machine?
    All this, of course assuming the build you mean is a Standalone build on the same platform and desktop device as the Editor