BITKit/Src/Unity/Scripts/Application/SetTargetFrameRate.cs

44 lines
1.1 KiB
C#
Raw Normal View History

2023-08-11 23:57:37 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-11-03 16:38:17 +08:00
using System.Linq;
2023-08-11 23:57:37 +08:00
using AYellowpaper.SerializedCollections;
using UnityEngine;
namespace BITKit
{
public class SetTargetFrameRate : MonoBehaviour
{
[SerializeField] private SerializedDictionary<string, int> frameRateDictionary;
2024-11-03 16:38:17 +08:00
[SerializeField] private SerializedDictionary<RuntimePlatform, bool> maxFrameRate;
2023-08-11 23:57:37 +08:00
private int currentFrameRate;
private void Start()
{
currentFrameRate = Application.targetFrameRate;
2024-11-03 16:38:17 +08:00
if(maxFrameRate.TryGetValue(Application.platform,out var max))
{
if (max)
{
var maxRate = Screen.resolutions.Max(x => x.refreshRateRatio.value);
Application.targetFrameRate = currentFrameRate = (int)maxRate;
}
}
2023-08-11 23:57:37 +08:00
}
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;
}
}
}