1010 * Manage game "resolution" - runs in play mode AND editor
1111 */
1212
13-
14- //[ExecuteAlways]
13+ [ ExecuteAlways ]
1514public class ResolutionManager : MonoBehaviour {
1615
1716
@@ -32,9 +31,6 @@ public class ResolutionManager : MonoBehaviour {
3231
3332
3433
35-
36-
37-
3834 [ Space ( 10 ) ]
3935 [ Header ( "UNITY PLAYER" ) ]
4036 // states of the Unity 'player' or 'game view' window
@@ -44,9 +40,9 @@ public class ResolutionManager : MonoBehaviour {
4440 // - unfortunately named using "Screen" https://docs.unity3d.com/ScriptReference/Screen-height.html
4541 public Vector2 playerResolution ;
4642
47- [ Tooltip ( "Resolution of the game view" ) ]
48- // - set in Unity, stored in Application Support (Mac) (may be same as above)
49- public Vector2 gameViewResolution ;
43+ // [Tooltip ("Resolution of the game view")]
44+ //// - set in Unity, stored in Application Support (Mac) (seems to always be same as above)
45+ // public Vector2 gameViewResolution;
5046
5147 [ Tooltip ( "The size of the player view (we will set) in unity units" ) ]
5248 // depends on camera size
@@ -62,7 +58,6 @@ public class ResolutionManager : MonoBehaviour {
6258
6359
6460
65-
6661 [ Space ( 10 ) ]
6762 [ Header ( "----- DEVICE -----" ) ]
6863 // states of the screen / device display
@@ -80,122 +75,63 @@ public class ResolutionManager : MonoBehaviour {
8075
8176
8277
83- // OBJECTS TO UPDATE WHEN ABOVE PARAMETERS CHANGE
78+ // OBJECTS - UPDATE IF PARAMETERS CHANGE
8479
8580 [ Space ( 10 ) ]
86- [ Header ( "OBJECTS REFERENCES" ) ]
87-
88-
81+ [ Header ( "OBJECT REFERENCES" ) ]
8982
9083 [ Tooltip ( "Collider that defines the volume of the visible game world" ) ]
9184 public BoxCollider worldContainerCollider ;
92-
9385 public TMP_Text resolutionReportText ;
9486
9587
9688
9789
9890
99- //[SerializeField]
100- //private int framesSinceSizeUpdated = 0;
101-
102- //bool updateWorld = true;
103-
104-
10591 private void Awake ( )
10692 {
107-
10893 // for some reason using the collider on this object caused the editor to crash every time
10994 worldContainerCollider = GetComponent < BoxCollider > ( ) ;
110- // this is the other one
111- //worldContainerCollider = GameObject.Find ("WorldContainer").GetComponent<BoxCollider> ();
11295
113- UpdateResolutionParams ( ) ;
96+ StartCoroutine ( SendResolutionUpdatedEvent ( ) ) ;
11497 }
11598
99+
116100 private void Update ( )
117101 {
118- // if application is playing
119- if ( Application . IsPlaying ( gameObject ) ) {
120- // and if player resolution has changed
121- if ( playerResolution . x != Screen . width || playerResolution . y != Screen . height ) {
122- // update the parameters
123- UpdateResolutionParams ( ) ;
124- // update collider
125- UpdateColliderSize ( ) ;
126102
103+ // and if player resolution has changed
104+ if ( playerResolution . x != Screen . width || playerResolution . y != Screen . height ) {
105+ //Debug.Log ("ResolutionManager.Update() - resolution has changed to " + playerResolution.ToString ());
127106
128- // trigger data updated event
129- EventManager . TriggerEvent ( "ResolutionUpdated" ) ;
130-
131- }
107+ StartCoroutine ( SendResolutionUpdatedEvent ( ) ) ;
132108 }
109+ }
133110
111+ IEnumerator SendResolutionUpdatedEvent ( )
112+ {
113+ Debug . Log ( "ResolutionManager.SendResolutionUpdatedEvent() - resolution has changed to " + playerResolution . ToString ( ) ) ;
134114
115+ // update the parameters
116+ UpdateResolutionParams ( ) ;
135117
118+ // update collider
119+ UpdateColliderSize ( ) ;
136120
137- //if (Application.IsPlaying (gameObject)) {
138-
139-
140-
141- // // if player resolution has changed
142- // if (playerResolution.x != Screen.width || playerResolution.y != Screen.height) {
143- // // update the parameters
144- // UpdateResolutionParams ();
145- // //// reset counter
146- // //framesSinceSizeUpdated++;
147-
148- // updateWorld = true;
149- // }
150-
151-
152-
153-
154-
155- // //if (framesSinceSizeUpdated > 0) framesSinceSizeUpdated++;
156-
157- // //// wait a second after a resolution update
158- // //if (framesSinceSizeUpdated > 10) {
159- // // updateWorld = true;
160- // //}
161-
162-
163- //}
164-
165-
166-
167-
168-
169-
170- //if (updateWorld) {
171- // UpdateReport ();
172- // UpdateColliderSize ();
121+ UpdateReport ( ) ;
173122
123+ yield return new WaitForSeconds ( .2f ) ;
174124
175- // // reset counter
176- // framesSinceSizeUpdated = 0;
177- //}
178125
126+ // if application is playing
127+ if ( Application . IsPlaying ( gameObject ) ) {
179128
180- //// reset
181- //updateWorld = false;
129+ // trigger data updated event
130+ EventManager . TriggerEvent ( "ResolutionUpdated" ) ;
131+ }
182132 }
183133
184134
185- void UpdateColliderSize ( )
186- {
187- // make sure there isn't a negative number
188- if ( playerViewSize . x > 0 && playerViewSize . y > 0 ) {
189- worldContainerCollider . size = new Vector3 ( playerViewSize . x , playerViewSize . y , worldContainerCollider . size . z ) ;
190-
191-
192- // debugging
193- Debug . Log ( "ResolutionManager.UpdateColliderSize() playerViewSize = " + playerViewSize . ToString ( ) ) ;
194- Debug . Log ( "ResolutionManager.UpdateColliderSize() worldContainerCollider.size = " + worldContainerCollider . size . ToString ( ) ) ;
195- Debug . Log ( "ResolutionManager.UpdateColliderSize() worldContainerCollider.bounds = " + worldContainerCollider . bounds . ToString ( ) ) ;
196- }
197-
198- }
199135
200136 /**
201137 * Update all the resolution parameters
@@ -204,13 +140,14 @@ public void UpdateResolutionParams ()
204140 {
205141 //Debug.Log ("ResolutionManager.UpdateResolutionParams()");
206142
143+
207144 // CAMERA / CANVAS
208145 cameraSize = Camera . main . orthographicSize ;
209146 canvasResolution = new Vector2 ( canvasRect . sizeDelta . x , canvasRect . sizeDelta . y ) ;
210147
211148 // PLAYER PARAMS
212149 playerResolution = new Vector2 ( Screen . width , Screen . height ) ;
213- gameViewResolution = GetMainGameViewSize ( ) ;
150+ // gameViewResolution = GetMainGameViewSize ();
214151 playerViewSize = GetPlayerViewSize ( ) ;
215152 playerAspectRatio = playerResolution . x / playerResolution . y ;
216153 playerFullScreen = Screen . fullScreen ;
@@ -220,11 +157,45 @@ public void UpdateResolutionParams ()
220157 deviceAspectRatio = deviceResolution . x / deviceResolution . y ;
221158 }
222159
160+ /**
161+ * Update the text in the UI
162+ */
163+ void UpdateReport ( )
164+ {
165+ // update text in control panel
166+ string report =
167+ //"canvasResolution (px): " + canvasResolution.ToString () + "\n" +
168+ "playerResolution (px): " + playerResolution . ToString ( ) + "\n " +
169+ //"gameViewResolution (px): " + gameViewResolution.ToString () +
170+ "playerViewSize (units): " + playerViewSize . ToString ( ) + "\n " +
171+ "playerAspectRatio: " + playerAspectRatio . ToString ( ) + "\n " +
172+ "playerFullScreen: " + playerFullScreen . ToString ( ) + "\n " +
173+ "deviceResolution: " + deviceResolution . ToString ( ) ;
174+ resolutionReportText . text = report ;
175+ }
176+
177+ void UpdateColliderSize ( )
178+ {
179+ //Debug.Log ("ResolutionManager.UpdateColliderSize() [1]");
180+
181+ if ( playerViewSize . x > 0 && playerViewSize . y > 0 ) {
182+ // set new size, making sure there isn't a negative number
183+ worldContainerCollider . size = new Vector3 ( playerViewSize . x , playerViewSize . y , worldContainerCollider . size . z ) ;
184+
185+ //Debug.Log ("ResolutionManager.UpdateColliderSize() [2]");
186+ }
187+ //Debug.Log ("ResolutionManager.UpdateColliderSize() playerViewSize = " + playerViewSize.ToString ());
188+ //Debug.Log ("ResolutionManager.UpdateColliderSize() worldContainerCollider.size = " + worldContainerCollider.size.ToString ());
189+ //Debug.Log ("ResolutionManager.UpdateColliderSize() worldContainerCollider.bounds = " + worldContainerCollider.bounds.ToString ());
190+ }
191+
223192 /**
224193 * Return the player viewing volume size (in Unity units)
225194 */
226195 public static Vector3 GetPlayerViewSize ( )
227196 {
197+ //Debug.Log ("ResolutionManager.GetPlayerViewSize()");
198+
228199 // The orthographicSize is half of the vertical viewing volume size
229200 float height = Camera . main . orthographicSize * 2 ;
230201 // The horizontal size of the viewing volume depends on the aspect ratio so...
@@ -237,7 +208,7 @@ public static Vector3 GetPlayerViewSize ()
237208 /**
238209 * Return the editor Game View window size - works in editor
239210 */
240- public static Vector2 GetMainGameViewSize ( )
211+ static Vector2 GetMainGameViewSize ( )
241212 {
242213 System . Type T = System . Type . GetType ( "UnityEditor.GameView,UnityEditor" ) ;
243214 System . Reflection . MethodInfo GetSizeOfMainGameView = T . GetMethod ( "GetSizeOfMainGameView" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Static ) ;
@@ -246,22 +217,8 @@ public static Vector2 GetMainGameViewSize ()
246217 }
247218
248219
249- /**
250- * Update the text in the UI
251- */
252- void UpdateReport ( )
253- {
254- // update text in control panel
255- string report =
256- "playerResolution: " + playerResolution . ToString ( ) +
257- "\n " + "gameViewResolution: " + gameViewResolution . ToString ( ) +
258- "\n " + "playerViewSize: " + playerViewSize . ToString ( ) +
259- "\n " + "playerAspectRatio: " + playerAspectRatio . ToString ( ) +
260- "\n " + "playerFullScreen: " + playerFullScreen . ToString ( ) +
261- "\n " + "deviceResolution: " + deviceResolution . ToString ( ) +
262- "\n " + "deviceResolution: " + deviceResolution . ToString ( ) ;
263- resolutionReportText . text = report ;
264- }
220+
221+
265222
266223
267224
0 commit comments