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.

2 Answers

2

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.

It's a bit nutty, but I like it. :)

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?

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.

Sounds good to me. Thank you.