Skip to content

Commit 550b380

Browse files
committed
Add option for resize to either smaller edge or larger edge. Also cleanup the code.
1 parent 6a9ee3c commit 550b380

File tree

1 file changed

+54
-25
lines changed

1 file changed

+54
-25
lines changed

Assets/JCSUnity/Scripts/Settings/JCS_ScreenSettings.cs

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,20 @@ public class JCS_ScreenSettings
6464
[Tooltip("Previous screen height.")]
6565
public float PREV_SCREEN_HEIGHT = 0.0f;
6666

67+
[Tooltip("Target aspect ratio screen width.")]
68+
[SerializeField]
69+
public int ASPECT_RATIO_SCREEN_WIDTH = 0;
70+
71+
[Tooltip("Target aspect ratio screen height.")]
72+
[SerializeField]
73+
public int ASPECT_RATIO_SCREEN_HEIGHT = 0;
74+
6775

6876
[Header("** Initialize Variables (JCS_ScreenSettings) **")]
6977

78+
[Tooltip("Type of the screen handle.")]
79+
public JCS_ScreenType SCREEN_TYPE = JCS_ScreenType.RESIZABLE;
80+
7081
[Tooltip("Resize the screen/window to certain aspect when " +
7182
"the application starts. Aspect ratio can be set at " +
7283
"'JCS_ScreenManager'.")]
@@ -79,16 +90,9 @@ public class JCS_ScreenSettings
7990
[Tooltip("Resize the screen/window everytime a scene are loaded.")]
8091
public bool RESIZE_TO_ASPECT_EVERYTIME_SCENE_LOADED = false;
8192

82-
[Tooltip("Type of the screen handle.")]
83-
public JCS_ScreenType SCREEN_TYPE = JCS_ScreenType.RESIZABLE;
84-
85-
[Tooltip("Target aspect ratio screen width.")]
86-
[SerializeField]
87-
public int ASPECT_RATIO_SCREEN_WIDTH = 0;
88-
89-
[Tooltip("Target aspect ratio screen height.")]
90-
[SerializeField]
91-
public int ASPECT_RATIO_SCREEN_HEIGHT = 0;
93+
[Tooltip("When resize, resize to the smaller edge, if not true " +
94+
"will resize to larger edge.")]
95+
public bool RESIZE_TO_SMALLER_EDGE = true;
9296

9397

9498
[Header("** Runtime Variables (JCS_ScreenSettings) **")]
@@ -137,16 +141,27 @@ private void Awake()
137141
{
138142
// Force resize screen/window to certain aspect
139143
// ratio once.
140-
ForceAspectScreenOnce();
144+
ForceAspectScreenOnce(true);
141145
}
142146

143147
if (RESIZE_TO_STANDARD_WHEN_APP_STARTS)
144148
{
145149
// Force resize screen/window to standard
146150
// resolution once.
147-
ForceStandardScreenOnce();
151+
ForceStandardScreenOnce(true);
148152
}
149153

154+
/*
155+
* NOTE(jenchieh): This is really weird, that even we
156+
* use 'Screen.SetResolution' function, the 'Screen.width'
157+
* and 'Screen.height' will not change immediately.
158+
* We just have to get it ourselves in all resize event
159+
* function like above these functions.
160+
*
161+
* -> ForceAspectScreenOnce
162+
* -> ForceStandardScreenOnce
163+
*
164+
*/
150165
// Record down the starting screen width and screen height.
151166
//STARTING_SCREEN_WIDTH = Screen.width;
152167
//STARTING_SCREEN_HEIGHT = Screen.height;
@@ -158,9 +173,6 @@ private void Awake()
158173
if (RESIZE_TO_ASPECT_EVERYTIME_SCENE_LOADED)
159174
ForceAspectScreenOnce();
160175
}
161-
162-
Debug.Log("Starting: " + new Vector2(STARTING_SCREEN_WIDTH, STARTING_SCREEN_HEIGHT));
163-
Debug.Log("Screen: " + new Vector2(Screen.width, Screen.height));
164176
}
165177

166178
private void Start()
@@ -234,40 +246,57 @@ public float VisibleScreenHeight()
234246
/// <summary>
235247
/// Make the screen in certain aspect ratio.
236248
/// </summary>
237-
public void ForceAspectScreenOnce()
249+
/// <param name="starting"> Change the starting screen as well? </param>
250+
public void ForceAspectScreenOnce(bool starting = false)
238251
{
239252
int width = Screen.width;
240253
int height = Screen.height;
241254

242-
if (width > height)
255+
bool smaller = width > height;
256+
257+
// Reverse it if resize to larger edge.
258+
if (!RESIZE_TO_SMALLER_EDGE)
259+
smaller = !smaller;
260+
261+
if (smaller)
243262
{
244263
// update the height
245264
float heightAccordingToWidth = width / ASPECT_RATIO_SCREEN_WIDTH * ASPECT_RATIO_SCREEN_HEIGHT;
246265
Screen.SetResolution(width, (int)Mathf.Round(heightAccordingToWidth), false, 0);
247266

248-
STARTING_SCREEN_WIDTH = width;
249-
STARTING_SCREEN_HEIGHT = (int)heightAccordingToWidth;
267+
if (starting)
268+
{
269+
STARTING_SCREEN_WIDTH = width;
270+
STARTING_SCREEN_HEIGHT = (int)heightAccordingToWidth;
271+
}
250272
}
251273
else
252274
{
253275
// update the width
254276
float widthAccordingToHeight = height / ASPECT_RATIO_SCREEN_HEIGHT * ASPECT_RATIO_SCREEN_WIDTH;
255277
Screen.SetResolution((int)Mathf.Round(widthAccordingToHeight), height, false, 0);
256278

257-
STARTING_SCREEN_WIDTH = (int)widthAccordingToHeight;
258-
STARTING_SCREEN_HEIGHT = height;
279+
if (starting)
280+
{
281+
STARTING_SCREEN_WIDTH = (int)widthAccordingToHeight;
282+
STARTING_SCREEN_HEIGHT = height;
283+
}
259284
}
260285
}
261-
286+
262287
/// <summary>
263288
/// Resize the screen resolution to standard resolution once.
264289
/// </summary>
265-
public void ForceStandardScreenOnce()
290+
/// <param name="starting"> Change the starting screen as well? </param>
291+
public void ForceStandardScreenOnce(bool starting = false)
266292
{
267293
Screen.SetResolution(STANDARD_SCREEN_WIDTH, STANDARD_SCREEN_HEIGHT, false, 0);
268294

269-
STARTING_SCREEN_WIDTH = STANDARD_SCREEN_WIDTH;
270-
STARTING_SCREEN_HEIGHT = STANDARD_SCREEN_HEIGHT;
295+
if (starting)
296+
{
297+
STARTING_SCREEN_WIDTH = STANDARD_SCREEN_WIDTH;
298+
STARTING_SCREEN_HEIGHT = STANDARD_SCREEN_HEIGHT;
299+
}
271300
}
272301

273302
//----------------------

0 commit comments

Comments
 (0)