Tanoda
CameraPlaneController.cs
Go to the documentation of this file.
1
4
5using UnityEngine;
6
7public class CameraPlaneController : MonoBehaviour
8{
9 public Camera _targetCam;
10
11 ScreenOrientation orientation;
12 float height;
13 float width;
14 float screenRatio = 1.0f;
15 Vector3 and_pRot, and_puRot, and_lRot, and_lrRot;
16
17 // Use this for initialization
18 void Start()
19 {
20 init();
22 }
23
24 public void init()
25 {
26 var Screenheight = _targetCam.orthographicSize * 2.0f;
27 var Screenwidth = Screenheight * Screen.width / Screen.height;
28 height = Screenheight;
29 width = Screenwidth;
30 transform.localPosition = new Vector3(0, 0, 91.6f);
31
32#if UNITY_EDITOR|| UNITY_STANDALONE||UNITY_WEBGL||UNITY_WEBPLAYER
33 transform.localEulerAngles = new Vector3(90, 180, 0);
34 transform.localScale = new Vector3(width / 10, 1.0f, height / 10);
35#elif UNITY_WEBPLAYER
36 transform.localEulerAngles = new Vector3(90,180,0);
37 transform.localScale = new Vector3(width/10, 1.0f, height/10);
38 #endif
39 assignAngleVectors(); // get the rotation by device model
40
41 orientation = Screen.orientation;
42#if (UNITY_ANDROID||UNITY_IOS)&& !UNITY_EDITOR
43 updateRotationAndScale();
44 #endif
45 }
46
47 // Update is called once per frame
48 void Update()
49 {
50 if (orientation != Screen.orientation)
51 {
52 updateRotationAndScale();
53 orientation = Screen.orientation;
54 correctPlaneScale(screenRatio);
55 }
56 }
57
58 void assignAngleVectors()
59 {
60 //"Nexus"
61 var devceName = SystemInfo.deviceModel;
62 if (devceName.ToLower().Contains("nexus") && devceName.ToLower().Contains("5x"))
63 {
64#if UNITY_ANDROID
65 and_lrRot = new Vector3(90,180,0);
66 and_lRot = new Vector3(-90,0,0);
67 and_pRot = new Vector3(0,90,270);
68 and_puRot = new Vector3(0,270,90);
69 #elif UNITY_IOS
70 and_lrRot = new Vector3(-90,0,0);
71 and_lRot = new Vector3(90,180,0);
72 and_pRot = new Vector3(0,270,90);
73 and_puRot = new Vector3(0,270,90);
74 #endif
75 }
76 else
77 {
78#if UNITY_ANDROID
79 and_lrRot = new Vector3(-90,0,0);
80 and_lRot = new Vector3(90,180,0);
81 and_pRot = new Vector3(0,270,90);
82 and_puRot = new Vector3(0,90,270);
83 #elif UNITY_IOS
84 and_lrRot = new Vector3(90,180,0);
85 and_lRot = new Vector3(-90,0,0);
86 and_pRot = new Vector3(0,90,270);
87 and_puRot = new Vector3(0,270,90);
88 #endif
89 }
90 }
91
92 public void correctPlaneScale(float size)
93 {
94 screenRatio = size;
95
96 var orgVec = transform.localScale;
97 if (screenRatio > 1f)
98 transform.localScale = new Vector3(orgVec.x, 1.0f, orgVec.z * screenRatio);
99 else if (screenRatio < 1 && screenRatio > 0)
100 transform.localScale = new Vector3(orgVec.x / screenRatio, 1.0f, orgVec.z);
101 }
102
103
104 void updateRotationAndScale()
105 {
106 var screenHeight_1 = Screen.height;
107 var screenWidth_1 = Screen.width;
108 if (Screen.orientation == ScreenOrientation.Portrait ||
109 Screen.orientation == ScreenOrientation.PortraitUpsideDown)
110 {
111 if (screenHeight_1 < screenWidth_1)
112 {
113 var tempvalue = screenWidth_1;
114 screenWidth_1 = screenHeight_1;
115 screenHeight_1 = tempvalue;
116 }
117
118 var Screenheight = _targetCam.orthographicSize * 2.0f;
119 var Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
120 height = Screenheight;
121 width = Screenwidth;
122#if UNITY_ANDROID
123 if( Screen.orientation == ScreenOrientation.PortraitUpsideDown)
124 {
125 transform.localEulerAngles = and_puRot;
126 }
127 else
128 {
129 transform.localEulerAngles = and_pRot;
130 }
131
132 transform.localScale = new Vector3(height/10, 1.0f, width/10);
133 #elif UNITY_IOS
134 if( Screen.orientation == ScreenOrientation.PortraitUpsideDown)
135 {
136 transform.localEulerAngles = and_puRot;
137 }
138 else
139 {
140 transform.localEulerAngles = and_pRot;
141 }
142
143 transform.localScale = new Vector3(-1*height/10, 1.0f, width/10);
144 #endif
145 }
146 else if (Screen.orientation == ScreenOrientation.Landscape ||
147 Screen.orientation == ScreenOrientation.LandscapeLeft)
148 {
149 if (screenHeight_1 > screenWidth_1)
150 {
151 var tempvalue = screenWidth_1;
152 screenWidth_1 = screenHeight_1;
153 screenHeight_1 = tempvalue;
154 }
155
156 var Screenheight = _targetCam.orthographicSize * 2.0f;
157 var Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
158 height = Screenheight;
159 width = Screenwidth;
160
161#if UNITY_ANDROID
162 transform.localEulerAngles = and_lRot;
163 transform.localScale = new Vector3(width/10, 1.0f, height/10);
164 #elif UNITY_IOS
165 transform.localEulerAngles = and_lRot;
166 transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
167 #endif
168 }
169 else if (Screen.orientation == ScreenOrientation.LandscapeRight)
170 {
171 if (screenHeight_1 > screenWidth_1)
172 {
173 var tempvalue = screenWidth_1;
174 screenWidth_1 = screenHeight_1;
175 screenHeight_1 = tempvalue;
176 }
177
178 var Screenheight = _targetCam.orthographicSize * 2.0f;
179 var Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
180 height = Screenheight;
181 width = Screenwidth;
182#if UNITY_ANDROID
183 transform.localEulerAngles = and_lrRot;
184 transform.localScale = new Vector3(width/10, 1.0f, height/10);
185 #elif UNITY_IOS
186 transform.localEulerAngles = and_lrRot;
187 transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
188 #endif
189 }
190 }
191}
void correctPlaneScale(float size)