- Home /
Is there a callback function or event for a resolution change?
Is there an event that is raised or a function that is called when the screen resolution changes?
I do not wish to poll for Screen parameters.
Answer by Jamora · Jan 21, 2014 at 05:20 PM
Nothing built-in that I know of. You will most likely have to raise an event yourself whenever your ChangeResolution(Screen size)
or whatever method you use gets called.
Answer by Jessy · Jan 22, 2014 at 02:26 AM
I'm sick of trying to get this to format properly on this horrible site. PM me on the forum if you want better copy pasta.
ScreenExtensions.ResolutionChanged += resolution => Debug.Log(resolution.width);
new Resolution { width = 800, height = 500 }
.ApplyToScreen(true);
namespace UnityEngine
{
using System;
public static class ScreenExtensions
{
public static event Action<Resolution> ResolutionChanged;
public static void ApplyToScreen(this Resolution resolution, bool fullscreen)
{
Screen.SetResolution(resolution.width, resolution.height, fullscreen, resolution.refreshRate);
if (ResolutionChanged != null)
( (Action)(() => ResolutionChanged(resolution)) )
.PerformAfterCoroutine<WaitForEndOfFrame>();
}
}
}
namespace System
{
using System.Collections;
using UnityEngine;
public static class ActionExtensions
{
public static void PerformAfterCoroutine<T>(this Action action)
where T : YieldInstruction, new()
{
CoroutineBehaviour.StartCoroutine<T>(action);
}
}
}
namespace UnityEngine
{
using System;
using System.Collections;
public static class MonoBehaviourExtensions
{
/// <summary>
/// Performs an Action after a YieldInstruction.
/// </summary>
public static void StartCoroutine<T>(this MonoBehaviour monoBehaviour, Action action)
where T : YieldInstruction, new()
{
monoBehaviour.StartCoroutine(Coroutine<T>(action));
}
static IEnumerator Coroutine<T>(Action action) where T : YieldInstruction, new()
{
yield return new T();
action();
}
}
public class CoroutineBehaviour : MonoBehaviour
{
static MonoBehaviour Instance = new GameObject { hideFlags = HideFlags.HideAndDontSave }
.AddComponent<CoroutineBehaviour>();
public static void StartCoroutine<T>(Action action) where T : YieldInstruction, new()
{
Instance.StartCoroutine<T>(action);
}
}
}
Crazy, but big +1 for taking end of the frame into account.
Screen.Resolution should be a property that does what I offer here. Because you can't make a static extension method, or extension property, an extension method of Resolution is the cleanest thing I can think of. You of course need some back-end for this.
@Jessy I have no idea how this works... and the formatting doesn't make things easier... :/ But can I make just some randomly named script and fill in everything you wrote, except:
ScreenExtensions.ResolutionChanged += resolution => Debug.Log(resolution.width);
new Resolution { width = 800, height = 500 }
.ApplyToScreen(true);
...and then use something like this?:
public void ResolutionChanged() {
doSomething();
}
That was what I tried.. Didn't get it to work... How in the world does this work?
How works this? you sad you could give better copy pasta? whats about sourceb.in?