unity 优化之七

帧率低的问题

 

修改游戏的渲染分辨率,手机帧率低的时候,直接改低游戏的渲染分辨率,减少每一帧要处理的像素数量。

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);
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值