Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/Application/SetTargetFrameRate.cs

44 lines
1.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AYellowpaper.SerializedCollections;
using UnityEngine;
namespace BITKit
{
public class SetTargetFrameRate : MonoBehaviour
{
[SerializeField] private SerializedDictionary<string, int> frameRateDictionary;
[SerializeField] private SerializedDictionary<RuntimePlatform, bool> maxFrameRate;
private int currentFrameRate;
private void Start()
{
currentFrameRate = Application.targetFrameRate;
if(maxFrameRate.TryGetValue(Application.platform,out var max))
{
if (max)
{
var maxRate = Screen.resolutions.Max(x => x.refreshRateRatio.value);
Application.targetFrameRate = currentFrameRate = (int)maxRate;
}
}
}
public void SetFrameRate(string key)
{
if (frameRateDictionary.TryGetValue(key, out var frameRate))
{
SetFrameRate(frameRate);
}else if (int.TryParse(key, out frameRate))
{
SetFrameRate(frameRate);
}
}
public void SetFrameRate(int frameRate)
{
Application.targetFrameRate =currentFrameRate = frameRate;
}
}
}