1+ using DaggerfallWorkshop ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
4+ using UnityEngine ;
5+
6+ namespace DaggerfallWorkshop . Game
7+ {
8+
9+ /// <summary>
10+ /// Handles Android's Screen stuff like resolution setting and orientation
11+ /// </summary>
12+ public class AndroidScreenManager : MonoBehaviour
13+ {
14+ public static event System . Action < Resolution > ScreenResolutionChanged ;
15+ private Resolution lastResolution ;
16+
17+ private void Awake ( )
18+ {
19+ // Set resolution to the current resolution, just so that it's not using a stale setting from when
20+ // another Simulator was being used.
21+ if ( AndroidUtils . IsRunningInSimulator )
22+ {
23+ DaggerfallUnity . Settings . ResolutionWidth = Screen . currentResolution . width ;
24+ DaggerfallUnity . Settings . ResolutionHeight = Screen . currentResolution . height ;
25+ lastResolution = Screen . currentResolution ;
26+ }
27+ else
28+ {
29+ DaggerfallUnity . Settings . ResolutionWidth = Screen . width ;
30+ DaggerfallUnity . Settings . ResolutionHeight = Screen . height ;
31+ lastResolution = new Resolution ( ) { width = Screen . width , height = Screen . height } ;
32+ }
33+ }
34+ // private void OnGUI()
35+ // {
36+ // if (GUI.Button(new Rect(10, 70, 50, 30), "Any"))
37+ // SetOrientationToAny();
38+ // if (GUI.Button(new Rect(10, 170, 50, 30), "Landscape"))
39+ // SetOrientationToLandscape();
40+ // if (GUI.Button(new Rect(10, 270, 50, 30), "Portrait"))
41+ // SetOrientationToPortrait();
42+ // }
43+ private void Update ( )
44+ {
45+ int x = Screen . width ;
46+ int y = Screen . height ;
47+ if ( x != lastResolution . width || y != lastResolution . height ) {
48+ // looks like the resolution changed. Let's update the daggerfall unity resolution
49+ SetResolution ( x , y ) ;
50+ lastResolution = new Resolution ( ) { width = x , height = y } ;
51+ }
52+ }
53+ public static void SetResolution ( int x , int y )
54+ {
55+ Debug . Log ( "AndroidSimulationManager: Current screen updated to new resolution" ) ;
56+ DaggerfallUnity . Settings . ResolutionWidth = x ;
57+ DaggerfallUnity . Settings . ResolutionHeight = y ;
58+
59+ SettingsManager . SetScreenResolution ( x , y , true ) ;
60+ var allCams = FindObjectsOfType < Camera > ( ) ;
61+ foreach ( var cam in allCams )
62+ cam . ResetAspect ( ) ;
63+ if ( TouchscreenInputManager . Instance )
64+ TouchscreenInputManager . Instance . SetupUIRenderTexture ( ) ;
65+
66+ ScreenResolutionChanged ? . Invoke ( new Resolution ( ) { width = x , height = y } ) ;
67+ }
68+ public static void SetOrientationToAny ( )
69+ {
70+ Debug . Log ( "AndroidScreenManager: Setting oritentation to Any" ) ;
71+ Screen . autorotateToLandscapeLeft = true ;
72+ Screen . autorotateToLandscapeRight = true ;
73+ Screen . autorotateToPortrait = true ;
74+ Screen . autorotateToPortraitUpsideDown = true ;
75+ Screen . orientation = ScreenOrientation . AutoRotation ;
76+ }
77+ public static void SetOrientationToPortrait ( )
78+ {
79+ Debug . Log ( "AndroidScreenManager: Setting oritentation to Portrait" ) ;
80+ Screen . autorotateToLandscapeLeft = false ;
81+ Screen . autorotateToLandscapeRight = false ;
82+ Screen . autorotateToPortrait = true ;
83+ Screen . autorotateToPortraitUpsideDown = true ;
84+ Screen . orientation = ScreenOrientation . AutoRotation ;
85+ }
86+ public static void SetOrientationToLandscape ( )
87+ {
88+ Debug . Log ( "AndroidScreenManager: Setting oritentation to Landscape" ) ;
89+ Screen . autorotateToLandscapeLeft = true ;
90+ Screen . autorotateToLandscapeRight = true ;
91+ Screen . autorotateToPortrait = false ;
92+ Screen . autorotateToPortraitUpsideDown = false ;
93+ Screen . orientation = ScreenOrientation . AutoRotation ;
94+ // DeviceOrientationManager.ForceOrientation(ScreenOrientation.AutoRotation);
95+ }
96+ }
97+ }
0 commit comments