nemszabo

PerformanceOptimizer

Aug 27th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4.  
  5. public static class PerformanceOptimizer
  6. {
  7.     // Futtatás minden indításkor (Editor Play és build)
  8.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  9.     private static void ApplyPerformanceSettings()
  10.     {
  11.         // 1) VSync off
  12.         QualitySettings.vSyncCount = 0;
  13.  
  14.         // 2) Cél fps
  15.         Application.targetFrameRate = -1;
  16.  
  17.         // 3) Leggyorsabb Quality szint megkeresése (Low/Performance)
  18.         TrySetLowestQualityLevel();
  19.  
  20.         // 4) URP asset finomhangolás
  21.         var urpAsset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
  22.         if (urpAsset != null)
  23.         {
  24.             urpAsset.renderScale = 1.0f;   // natív felbontás
  25.             urpAsset.msaaSampleCount = 1;  // MSAA off
  26.         }
  27.  
  28.         // 5) Log
  29.         string qualityName = QualitySettings.names[QualitySettings.GetQualityLevel()];
  30.         if (urpAsset != null)
  31.         {
  32.             string msaaText = urpAsset.msaaSampleCount <= 1 ? "Off" : (urpAsset.msaaSampleCount + "x");
  33.             Debug.Log("[PerformanceOptimizer] Beállítva: " +
  34.                       $"VSync off, targetFrameRate = {Application.targetFrameRate} fps, " +
  35.                       $"Quality = {qualityName}, URP RenderScale = {urpAsset.renderScale}, MSAA = {msaaText}");
  36.         }
  37.         else
  38.         {
  39.             Debug.Log("[PerformanceOptimizer] Beállítva: " +
  40.                       $"VSync off, targetFrameRate = {Application.targetFrameRate} fps, " +
  41.                       $"Quality = {qualityName}");
  42.         }
  43.     }
  44.  
  45.     // Segédfüggvény: a leggyorsabb quality szint kiválasztása
  46.     private static void TrySetLowestQualityLevel()
  47.     {
  48.         int currentLevel = QualitySettings.GetQualityLevel();
  49.         int chosen = currentLevel;
  50.  
  51.         string[] levels = QualitySettings.names;
  52.         for (int i = 0; i < levels.Length; i++)
  53.         {
  54.             string q = levels[i].ToLower();
  55.             if (q.Contains("low") || q.Contains("performance"))
  56.             {
  57.                 chosen = i;
  58.                 break; // első találat elég
  59.             }
  60.         }
  61.  
  62.         if (chosen != currentLevel)
  63.             QualitySettings.SetQualityLevel(chosen);
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment