性能优化终极技巧:降低分辨率
就是使用Unity函数
1 |
Screen.SetResolution(240, 320, true); |
使用这个方法,可以让你帧率飙升!
10+帧率瞬间满60帧!
言归正传,本文是做一个根据帧率动态降低分辨率,以牺牲画质,保证帧率稳定的逻辑
直接挂在即可,可以在比较卡帧的场景中打包自测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DynamicResolution : MonoBehaviour { public float showTime = 1f; private int count = 0; private float deltaTime = 0f; List<float> fpsList = new List<float>(); int[] resolutionWidths = new int[3]; private int lowestFps = 50; private void Start() { InitSolutions(); SetResolution(resolutionWidths[0]); } void Update() { count++; deltaTime += Time.deltaTime; if (deltaTime >= showTime) { float fps = count / deltaTime; float milliSecond = deltaTime * 1000 / count; count = 0; deltaTime = 0f; SetResolutionReduce(fps); } } void InitSolutions() { int currentWidth = Screen.currentResolution.width; for (int i = 0; i < resolutionWidths.Length; i++) { resolutionWidths[i] = currentWidth - (int)(currentWidth * (i + 1) * 0.1f); } } void SetResolution(int width) { int currentWidth = Screen.currentResolution.width; int currentHeight = Screen.currentResolution.height; int height = currentHeight * width / currentWidth; Screen.SetResolution(width, height, true); Debuger.Log("强制降低分辨率 目标分辨率为:" + width + " " + height + " 当前分辨率:" + currentWidth + " " + currentHeight); } void SetResolutionReduce(float fps) { if (fps < lowestFps) { fpsList.Add(fps); } else { fpsList.Clear(); } if (fpsList.Count < 5) return; fpsList.RemoveRange(0, 3); int currentWidth = Screen.currentResolution.width; int currentHeight= Screen.currentResolution.height; if(currentWidth < resolutionWidths[resolutionWidths.Length - 1]) { return; } foreach (int width in resolutionWidths) { if(width < currentWidth) { int height = currentHeight * width / currentWidth; Screen.SetResolution(width, height, true); Debuger.Log("由于帧率太低,强制降低分辨率 目标分辨率为:" + width + " " + height + " 当前分辨率:" + currentWidth + " " + currentHeight); break; } } } } |
- 本文固定链接: http://www.u3d8.com/?p=1908
- 转载请注明: 网虫虫 在 u3d8.com 发表过