Unity3D FPS帧数修改

FPS就是游戏运行的帧数,下面讲解一下如何修改Unity的FPS

步骤

1、在Edit/Project Settings/Quality 质量设置里把帧数设定关闭,关闭之后才能在代码中修改游戏运行的帧数。

image

 

2、在Unity中创建新脚本UpdateFrame.cs ,代码

using UnityEngine;

using System.Collections;



/// <summary>

/// 功能:修改游戏FPS

/// </summary>

public class UpdateFrame : MonoBehaviour

{

    //游戏的FPS,可在属性窗口中修改

    public int targetFrameRate = 300;

    

    //当程序唤醒时

    void Awake ()

    {

        //修改当前的FPS

        Application.targetFrameRate = targetFrameRate;

    }

    

}


3、把该代码及ShowFPS.js绑定在层次视图的任一GameObject上Unity3D FPS帧数修改,运行游戏,即可以Game视图中看到当前的FPS,Unity3D FPS帧数修改同时可修改targetFrameRate变量来观看结果Unity3D FPS帧数修改

ShowFPS.js代码

@script ExecuteInEditMode



private var gui : GUIText;



private var updateInterval = 1.0;

private var lastInterval : double; // Last interval end time

private var frames = 0; // Frames over current interval



function Start()

{

    lastInterval = Time.realtimeSinceStartup;

    frames = 0;

}



function OnDisable ()

{

	if (gui)

		DestroyImmediate (gui.gameObject);

}



function Update()

{

#if !UNITY_FLASH

    ++frames;

    var timeNow = Time.realtimeSinceStartup;

    if (timeNow > lastInterval + updateInterval)

    {

		if (!gui)

		{

			var go : GameObject = new GameObject("FPS Display", GUIText);

			go.hideFlags = HideFlags.HideAndDontSave;

			go.transform.position = Vector3(0,0,0);

			gui = go.guiText;

			gui.pixelOffset = Vector2(5,55);

		}

        var fps : float = frames / (timeNow - lastInterval);

		var ms : float = 1000.0f / Mathf.Max (fps, 0.00001);

		gui.text = ms.ToString("f1") + "ms " + fps.ToString("f2") + "FPS";

        frames = 0;

        lastInterval = timeNow;

    }

#endif

}

  

你可能感兴趣的:(unity3d)