refreshRate obsolete, use refreshRateRatio instead + SetResolution fourth argument

'Resolution.refreshRate' is obsolete: 'Resolution.refreshRate is obsolete. Use refreshRateRatio instead.'

'Screen.SetResolution(int, int, FullScreenMode, int)' is obsolete: 'SetResolution(int, int, FullScreenMode, int) is obsolete. Use SetResolution(int, int, FullScreenMode, RefreshRate) instead.'

So I had these two issues, just warnings about obsolescence that I could ignore of course. But I wanted to solve it. But I couldn't find anything online. Which is to say... I'm actually kind of proud of myself for solving this, as I'm an almost complete beginner, just started recently. Only able to do this at all thanks to ChatGPT, I'm disabled and learning is a painfully stressful thing.

Anyway, so, I had this (resolutions is a Resolutions[ ] array set to Screen.resolutions):

        for (int i = 0; i < resolutions.Length; i++)
        {
            if (resolutions[i].refreshRate == refreshRate)
            {
                filteredResolutions.Add(resolutions[i]);
            }
        }

And worked out I can replace refreshRate with refreshRateRatio.value. So, this:

        for(int i = 0; i < resolutions.Length; i++)
        {
            if (resolutions[i].refreshRateRatio.value == refreshRate)
            {
                filteredResolutions.Add(resolutions[i]);
            }
        }

But the next one was a lot more difficult, everything I tried wasn't working and ChatGPT was giving wrong answers that didn't work. But I eventually worked out how to properly set the fourth value of SetResolution which expects a RefreshRate type (rRate is my refresh rate value here, an int set to "60"):

        // Create a new RefreshRate object with the given refresh rate
        RefreshRate refreshRate = new RefreshRate();
        refreshRate.numerator = (uint)rRate;
        refreshRate.denominator = 1;
        // Call Screen.SetResolution to set the resolution and refresh rate
        Screen.SetResolution(width, height, FullScreenMode.ExclusiveFullScreen, refreshRate);

I'm not actually posting this to boast (I'm sure it's nothing special, like I said I'm an almost complete beginner), but because I couldn't find this stuff anywhere (searching by the warning messages). But I'm sure other people know and I could've asked about it. It's stressful to do that though, hence why I'm using ChatGPT a lot for help. Anyway I hope this'll help people who also don't like just ignoring warnings. :p
If I got anything wrong, please correct me!

7 Likes

Yo, good work on converting the obsolete code. BTW today I’m also going through this and the conversion is pretty simple they even have them docs: Unity - Scripting API: RefreshRate

1 Like


It might be easy for you, but for me at the time (almost complete beginner) that was not enough info to know how to convert it in code. But, in any case, let me have the tiny victory, I don't have many of them. :p

5 Likes

{
var numerator = (uint) Math.Round(frameRate,6) * 1000000;
uint denominator = 1000000;
var refreshRate = new RefreshRate() {numerator = numerator, denominator = denominator};
return new Resolution(){height = height, width=width, refreshRateRatio = refreshRate};
}

Thought I'd post this in case anyone else doesn't understand the new RefreshRate struct... Seems silly to me to have to do this instead of just being able to set a RefreshRate() using a double but okay Unity. I don't anticipate any refresh rates being upwards of 6 decimal place precision so this should be good enough for most

1 Like

Actually, if you don’t plan to change screen refresh rate from current one to some custom than you can do:

Screen.SetResolution(Screen.width, Screen.height, FullScreenMode.FullScreenWindow, Screen.currentResolution.refreshRateRatio);

Just set target frame rate beforehand.