帧率低的问题
修改游戏的渲染分辨率,手机帧率低的时候,直接改低游戏的渲染分辨率,减少每一帧要处理的像素数量。
unity 提供了一个方法设置渲染分辨率
Screen.SetResolution(designWidth,designHeight,true);
这里的做法是简单的把渲染分辨率设置成设计分辨率。如果手机本身的分辨率低于设计分辨率就不要再修改了。
int scWidth = Screen.width;
int scHeight = Screen.height;
int designWidth=768; //这个是设计分辨率
int designHeight=1024;
if( scWidth<=designWidth || scHeight<=designHeight)
return;
Screen.SetResolution(designWidth,designHeight,true);
监控帧率
using System.Collections;
using UnityEngine;
public class FrameManager : MonoBehaviour {
public Color textColor = Color.red;
public int guiFontSize = 50;
private string label = string.Empty;
private GUIStyle style = new GUIStyle();
private float count;
private float m_LastUpdateShowTime = 0f; //上一次更新帧率的时间;
private float m_UpdateShowDeltaTime = 0.1f;//更新帧率的时间间隔;
private int m_FrameUpdate = 0;//帧数;
private float m_FPS = 0;
void Awake() {
Application.targetFrameRate = 100;
}
void Start() {
m_LastUpdateShowTime = Time.realtimeSinceStartup;
}
void Update() {
m_FrameUpdate++;
if (Time.realtimeSinceStartup - m_LastUpdateShowTime >= m_UpdateShowDeltaTime) {
m_FPS = m_FrameUpdate / (Time.realtimeSinceStartup - m_LastUpdateShowTime);
m_FrameUpdate = 0;
m_LastUpdateShowTime = Time.realtimeSinceStartup;
}
}
void OnGUI() {
style.fontSize = guiFontSize;
style.normal.textColor = textColor;
GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 500, 300), "FPS: " + m_FPS, style);
}
}