Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"MainCamera.cs"
 * 
 *	This is attached to the Main Camera, and must be tagged as "MainCamera" to work.
 *	Only one Main Camera should ever exist in the scene.
 *
 *	Shake code adapted from Mike Jasper's code: http://www.mikedoesweb.com/2012/camera-shake-in-unity/
 *
 *  Aspect-rattio code adapated from Eric Haines' code: http://wiki.unity3d.com/index.php?title=AspectRatioEnforcer
 * 
 */

using UnityEngine;
using System.Collections;
using AC;

public class MainCamera : MonoBehaviour
{
	
	public Texture2D fadeTexture;
	public _Camera attachedCamera;

	[HideInInspector] public _Camera lastNavCamera;
	[HideInInspector] public bool isSmoothChanging;
	
	private bool cursorAffectsRotation;
	
	[HideInInspector] public Vector2 perspectiveOffset = new Vector2 (0f, 0f);
	private Vector2 startPerspectiveOffset = new Vector2 (0f, 0f);

	private float timeToFade = 0f;
	private int drawDepth = -1000;
	private float alpha = 0f; 
	private FadeType fadeType;
	private float fadeStartTime;
	
	private MoveMethod moveMethod;
	private float changeTime;
	
	private	Vector3 startPosition;
	private	Quaternion startRotation;
	private float startFOV;
	private float startOrtho;
	private	float startTime;
	
	private Transform LookAtPos;
	private Vector2 lookAtAmount;
	private float LookAtZ;
	private Vector3 lookAtTarget;
	
	private SettingsManager settingsManager;
	private StateHandler stateHandler;
	private PlayerInput playerInput;
	
	private float shakeDecay;
	private bool shakeMove;
	private float shakeIntensity;
	private Vector3 shakePosition;
	private Vector3 shakeRotation;

	// Aspect ratio
	private Camera borderCam;
	public static MainCamera mainCam;

	
	private void Awake()
	{
		if (this.transform.parent && this.transform.parent.name != "_Cameras")
		{
			if (GameObject.Find ("_Cameras"))
			{
				this.transform.parent = GameObject.Find ("_Cameras").transform;
			}
			else
			{
				this.transform.parent = null;
			}
		}
		
		if (GameObject.FindWithTag (Tags.gameEngine) && GameObject.FindWithTag (Tags.gameEngine).GetComponent <PlayerInput>())
		{
			playerInput = GameObject.FindWithTag (Tags.gameEngine).GetComponent <PlayerInput>();
		}
		
		if (AdvGame.GetReferences () && AdvGame.GetReferences ().settingsManager)
		{
			settingsManager = AdvGame.GetReferences ().settingsManager;
		}

		if (!camera)
		{
			Debug.LogError ("The MainCamera script requires a Camera component.");
			return;
		}
		
		if (settingsManager.forceAspectRatio)
		{
			#if !UNITY_IPHONE
			settingsManager.landscapeModeOnly = false;
			#endif
			CreateBorderCamera ();
		}
		
	}	

	
	private void Start()
	{
		if (GameObject.FindWithTag (Tags.persistentEngine) && GameObject.FindWithTag (Tags.persistentEngine).GetComponent <StateHandler>())
		{
			stateHandler = GameObject.FindWithTag (Tags.persistentEngine).GetComponent <StateHandler>();
		}
	
		foreach (Transform child in transform)
		{
			LookAtPos = child;
		}

		if (LookAtPos)
		{
			LookAtZ = LookAtPos.localPosition.z;
		}
	}
	
	
	public void Shake (float _shakeDecay, bool _shakeMove)
	{
		shakePosition = Vector3.zero;
		shakeRotation = Vector3.zero;
		
		shakeMove = _shakeMove;
		shakeDecay = _shakeDecay;
		shakeIntensity = shakeDecay * 150f;
	}
	
	
	public bool IsShaking ()
	{
		if (shakeIntensity > 0f)
		{
			return true;
		}
		
		return false;
	}
	
	
	public void StopShaking ()
	{
		shakeIntensity = 0f;
		shakePosition = Vector3.zero;
		shakeRotation = Vector3.zero;
	}
	
	
	private void FixedUpdate ()
	{
		if (shakeIntensity > 0f)
		{
			if (shakeMove)
			{
				shakePosition = Random.insideUnitSphere * shakeIntensity * 0.5f;
			}
			
			shakeRotation = new Vector3
			(
				Random.Range (-shakeIntensity, shakeIntensity) * 0.2f,
				Random.Range (-shakeIntensity, shakeIntensity) * 0.2f,
				Random.Range (-shakeIntensity, shakeIntensity) * 0.2f
			);
			
			shakeIntensity -= shakeDecay;
		}
		
		else if (shakeIntensity < 0f)
		{
			StopShaking ();
		}
	}
	
	
	private void Update ()
	{
		if (stateHandler)
		{
			if (stateHandler.gameState == GameState.Normal)
			{
				SetFirstPerson ();
			}
			
			if (this.GetComponent <AudioListener>())
			{
				if (stateHandler.gameState == GameState.Paused)
				{
					AudioListener.pause = true;
				}
				else
				{
					AudioListener.pause = false;
				}
			}
		}
	}
	
	
	public void PrepareForBackground ()
	{
		if (AdvGame.GetReferences () && AdvGame.GetReferences ().settingsManager)
		{
			settingsManager = AdvGame.GetReferences ().settingsManager;

			camera.clearFlags = CameraClearFlags.Depth;
			
			if (LayerMask.NameToLayer (settingsManager.backgroundImageLayer) != -1)
			{
				camera.cullingMask = ~(1 << LayerMask.NameToLayer (settingsManager.backgroundImageLayer));
			}
		}
		else
		{
			Debug.LogError ("Could not find a Settings Manager - please set one using the main Adventure Creator window.");
		}
	}
	
	
	private void RemoveBackground ()
	{
		if (AdvGame.GetReferences () && AdvGame.GetReferences ().settingsManager)
		{
			settingsManager = AdvGame.GetReferences ().settingsManager;
			
			camera.clearFlags = CameraClearFlags.Skybox;
			
			if (LayerMask.NameToLayer (settingsManager.backgroundImageLayer) != -1)
			{
				camera.cullingMask = ~(1 << LayerMask.NameToLayer (settingsManager.backgroundImageLayer));
			}
		}
		else
		{
			Debug.LogError ("Could not find a Settings Manager - please set one using the main Adventure Creator window.");
		}
	}

	
	public void SetFirstPerson ()
	{
		if (settingsManager)
		{
			if (settingsManager.movementMethod == MovementMethod.FirstPerson)
			{
				SetGameCamera (GameObject.FindWithTag (Tags.firstPersonCamera).GetComponent <_Camera>());
				SnapToAttached ();
			}
		}

		if (attachedCamera)
		{
			lastNavCamera = attachedCamera;
		}
	}
	
	
	private void OnGUI()
	{
		if (timeToFade > 0f)
		{
			alpha = (Time.time - fadeStartTime) / timeToFade;

			if (fadeType == FadeType.fadeIn)
			{
				alpha = 1 - alpha;
			}

			alpha = Mathf.Clamp01 (alpha);
		

			if (Time.time > (fadeStartTime + timeToFade))
			{
				timeToFade = 0f;
			}
		}

		if (alpha > 0f)
		{
			Color tempColor = GUI.color;
			tempColor.a = alpha;
			GUI.color = tempColor;
			GUI.depth = drawDepth;
			GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), fadeTexture);
		}
	}
	
	
	public void ResetProjection ()
	{
		if (camera)
		{
			perspectiveOffset = Vector2.zero;
			camera.projectionMatrix = AdvGame.SetVanishingPoint (camera, perspectiveOffset);
			camera.ResetProjectionMatrix ();
		}
	}


	public void ResetMoving ()
	{
		isSmoothChanging = false;
		startTime = 0f;
		changeTime = 0f;
	}

	
	private void LateUpdate ()
	{
		if (attachedCamera && (!(attachedCamera is GameCamera25D)))
		{
			if (!isSmoothChanging)
			{
				if (attachedCamera is GameCamera2D)
				{
					GameCamera2D cam2D = (GameCamera2D) attachedCamera;
					perspectiveOffset = cam2D.perspectiveOffset;
					camera.projectionMatrix = AdvGame.SetVanishingPoint (camera, perspectiveOffset);
				}
				
				else
				{
					GetComponent <Camera>().fieldOfView = attachedCamera.GetComponent <Camera>().fieldOfView;
					transform.rotation = attachedCamera.transform.rotation;
					transform.position = attachedCamera.transform.position;

					if (cursorAffectsRotation)
					{
						SetLookAtPosition ();
						transform.LookAt (LookAtPos);
					}
				}
			}
			else
			{
				// Move from one GameCamera to another
				if (Time.time < startTime + changeTime)
				{
					if (attachedCamera is GameCamera2D)
					{
						GameCamera2D cam2D = (GameCamera2D) attachedCamera;
						
						if (moveMethod == MoveMethod.Linear)
						{
							perspectiveOffset.x = Mathf.Lerp (startPerspectiveOffset.x, cam2D.perspectiveOffset.x, AdvGame.LinearTimeFactor (startTime, changeTime));
							perspectiveOffset.y = Mathf.Lerp (startPerspectiveOffset.y, cam2D.perspectiveOffset.y, AdvGame.LinearTimeFactor (startTime, changeTime));
						}
						else
						{
							perspectiveOffset.x = Mathf.Lerp (startPerspectiveOffset.x, cam2D.perspectiveOffset.x, AdvGame.SmoothTimeFactor (startTime, changeTime));
							perspectiveOffset.y = Mathf.Lerp (startPerspectiveOffset.y, cam2D.perspectiveOffset.y, AdvGame.SmoothTimeFactor (startTime, changeTime));
						}
						
						camera.ResetProjectionMatrix ();
					}
					
					if (moveMethod == MoveMethod.Linear)
					{
						transform.position = Vector3.Lerp (startPosition, attachedCamera.transform.position, AdvGame.LinearTimeFactor (startTime, changeTime)); 
						transform.rotation = Quaternion.Lerp (startRotation, attachedCamera.transform.rotation, AdvGame.LinearTimeFactor (startTime, changeTime));
						GetComponent <Camera>().fieldOfView = Mathf.Lerp (startFOV, attachedCamera.GetComponent <Camera>().fieldOfView, AdvGame.LinearTimeFactor (startTime, changeTime));
						GetComponent <Camera>().orthographicSize = Mathf.Lerp (startOrtho, attachedCamera.GetComponent <Camera>().orthographicSize, AdvGame.LinearTimeFactor (startTime, changeTime));
					}
					else if (moveMethod == MoveMethod.Smooth)
					{
						transform.position = Vector3.Lerp (startPosition, attachedCamera.transform.position, AdvGame.SmoothTimeFactor (startTime, changeTime)); 
						transform.rotation = Quaternion.Lerp (startRotation, attachedCamera.transform.rotation, AdvGame.SmoothTimeFactor (startTime, changeTime));
						GetComponent <Camera>().fieldOfView = Mathf.Lerp (startFOV, attachedCamera.GetComponent <Camera>().fieldOfView, AdvGame.SmoothTimeFactor (startTime, changeTime));
						GetComponent <Camera>().orthographicSize = Mathf.Lerp (startOrtho, attachedCamera.GetComponent <Camera>().orthographicSize, AdvGame.SmoothTimeFactor (startTime, changeTime));
					}
					else
					{
						// Don't slerp y position as this will create a "bump" effect
						Vector3 newPosition = Vector3.Slerp (startPosition, attachedCamera.transform.position, AdvGame.SmoothTimeFactor (startTime, changeTime)); 
						newPosition.y = Mathf.Lerp (startPosition.y, attachedCamera.transform.position.y, AdvGame.SmoothTimeFactor (startTime, changeTime));
						transform.position = newPosition;
						
						transform.rotation = Quaternion.Slerp (startRotation, attachedCamera.transform.rotation, AdvGame.SmoothTimeFactor (startTime, changeTime));
						GetComponent <Camera>().fieldOfView = Mathf.Lerp (startFOV, attachedCamera.GetComponent <Camera>().fieldOfView, AdvGame.SmoothTimeFactor (startTime, changeTime));
						GetComponent <Camera>().orthographicSize = Mathf.Lerp (startOrtho, attachedCamera.GetComponent <Camera>().orthographicSize, AdvGame.SmoothTimeFactor (startTime, changeTime));
					}

					if (attachedCamera is GameCamera2D)
					{
						camera.projectionMatrix = AdvGame.SetVanishingPoint (camera, perspectiveOffset);
					}
				}
				else
				{
					LookAtCentre ();
					isSmoothChanging = false;
				}
			}
			
			if (cursorAffectsRotation)
			{
				LookAtPos.localPosition = Vector3.Lerp (LookAtPos.localPosition, lookAtTarget, Time.deltaTime * 3f);	
			}
		}
		
		else if (attachedCamera && (attachedCamera is GameCamera25D))
		{
			transform.position = attachedCamera.transform.position;
			transform.rotation = attachedCamera.transform.rotation;
		}
		
		transform.position += shakePosition;
		transform.localEulerAngles += shakeRotation;
		
	}

	
	private void LookAtCentre ()
	{
		if (LookAtPos)
		{
			lookAtTarget = new Vector3 (0, 0, LookAtZ);
		}
	}
	

	private void SetLookAtPosition ()
	{
		if (stateHandler.gameState == GameState.Normal)
		{
			Vector2 mouseOffset = new Vector2 (playerInput.mousePosition.x / (Screen.width / 2) - 1, playerInput.mousePosition.y / (Screen.height / 2) - 1);
			float distFromCentre = mouseOffset.magnitude;
	
			if (distFromCentre < 1.4f)
			{
				lookAtTarget = new Vector3 (mouseOffset.x * lookAtAmount.x, mouseOffset.y * lookAtAmount.y, LookAtZ);
			}
		}
	}
	
	
	public void SnapToAttached ()
	{
		if (attachedCamera && attachedCamera.GetComponent <Camera>())
		{
			LookAtCentre ();
			isSmoothChanging = false;
			
			GetComponent <Camera>().isOrthoGraphic = attachedCamera.GetComponent <Camera>().isOrthoGraphic;
			GetComponent <Camera>().fieldOfView = attachedCamera.GetComponent <Camera>().fieldOfView;
			transform.position = attachedCamera.transform.position;
			transform.rotation = attachedCamera.transform.rotation;
			
			if (attachedCamera is GameCamera2D)
			{
				GameCamera2D cam2D = (GameCamera2D) attachedCamera;
				perspectiveOffset = cam2D.perspectiveOffset;
			}
			else
			{
				perspectiveOffset = new Vector2 (0f, 0f);
			}
		}
	}
	
	
	public void SmoothChange (float _changeTime, MoveMethod method)
	{
		LookAtCentre ();
		moveMethod = method;
		isSmoothChanging = true;
		
		startTime = Time.time;
		changeTime = _changeTime;
		
		startPosition = transform.position;
		startRotation = transform.rotation;
		startFOV = GetComponent <Camera>().fieldOfView;
		startOrtho = GetComponent <Camera>().orthographicSize;
		
		startPerspectiveOffset = perspectiveOffset;
	}
	
	
	public void SetGameCamera (_Camera _camera)
	{
		if (attachedCamera != null && attachedCamera is GameCamera25D)
		{
			if (_camera is GameCamera25D)
			{ }
			else
			{
				RemoveBackground ();
			}
		}

		camera.ResetProjectionMatrix ();
		attachedCamera = _camera;
		
		if (attachedCamera && attachedCamera.GetComponent <Camera>())
		{
			this.GetComponent <Camera>().farClipPlane = attachedCamera.GetComponent <Camera>().farClipPlane;
			this.GetComponent <Camera>().nearClipPlane = attachedCamera.GetComponent <Camera>().nearClipPlane;
			
			// Set projection
			if (this.GetComponent <Camera>().orthographic != attachedCamera.GetComponent <Camera>().orthographic)
			{
				this.GetComponent <Camera>().fieldOfView = attachedCamera.GetComponent <Camera>().fieldOfView;
				this.GetComponent <Camera>().orthographicSize = attachedCamera.GetComponent <Camera>().orthographicSize;
				this.GetComponent <Camera>().orthographic = attachedCamera.GetComponent <Camera>().orthographic;
			}
		}
		
		// Set LookAt
		if (attachedCamera is GameCamera)
		{
			GameCamera gameCam = (GameCamera) attachedCamera;
			cursorAffectsRotation = gameCam.followCursor;
			lookAtAmount = gameCam.cursorInfluence;
		}
		else
		{
			cursorAffectsRotation = false;
		}
		
		// Set background
		if (attachedCamera is GameCamera25D)
		{
			GameCamera25D cam25D = (GameCamera25D) attachedCamera;
			cam25D.SetActiveBackground ();
		}
		
		// TransparencySortMode
		if (attachedCamera is GameCamera2D)
		{
			this.GetComponent <Camera>().transparencySortMode = TransparencySortMode.Orthographic;
		}
		else if (attachedCamera)
		{
			if (attachedCamera.GetComponent <Camera>().orthographic)
			{
				this.GetComponent <Camera>().transparencySortMode = TransparencySortMode.Orthographic;
			}
			else
			{
				this.GetComponent <Camera>().transparencySortMode = TransparencySortMode.Perspective;
			}
		}
	}
	
	
	public void FadeIn (float _timeToFade)
	{
		if (_timeToFade > 0f)
		{
			timeToFade = _timeToFade;
			alpha = 1f;
			fadeType = FadeType.fadeIn;
			fadeStartTime = Time.time;
		}
		else
		{
			alpha = 0f;
			timeToFade = 0f;
		}
	}

	
	public void FadeOut (float _timeToFade)
	{
		if (_timeToFade > 0f)
		{
			timeToFade = _timeToFade;
			alpha = 0f;
			fadeType = FadeType.fadeOut;
			fadeStartTime = Time.time;
		}
		else
		{
			alpha = 1f;
			timeToFade = 0f;
		}
	}
	
	
	public bool isFading ()
	{
		if (fadeType == FadeType.fadeOut && alpha < 1f)
		{
			return true;
		}
		else if (fadeType == FadeType.fadeIn && alpha > 0f)
		{
			return true;
		}

		return false;
	}

	
	public void OnDeserializing ()
	{
		FadeIn (0.5f);
	}
	
	
	public Vector3 PositionRelativeToCamera (Vector3 _position)
	{
		return (_position.x * ForwardVector ()) + (_position.z * RightVector ());
	}
	
	
	public Vector3 RightVector ()
	{
		return (transform.right);
	}
	
	
	public Vector3 ForwardVector ()
	{
		Vector3 camForward;
		
		camForward = transform.forward;
		camForward.y = 0;
		
		return (camForward);
	}


	private void CreateBorderCamera ()
	{
		float currentAspectRatio = 0f;
		
		if (Screen.orientation == ScreenOrientation.LandscapeRight || Screen.orientation == ScreenOrientation.LandscapeLeft)
		{
			currentAspectRatio = (float) Screen.width / Screen.height;
		}
		else
		{
			if (Screen.height  > Screen.width && settingsManager.landscapeModeOnly)
			{
				currentAspectRatio = (float) Screen.height / Screen.width;
			}
			else
			{
				currentAspectRatio = (float) Screen.width / Screen.height;
			}
		}
		// If the current aspect ratio is already approximately equal to the desired aspect ratio, use a full-screen Rect (in case it was set to something else previously)
		
		if ((int) (currentAspectRatio * 100) / 100f == (int) (settingsManager.wantedAspectRatio * 100) / 100f)
		{
			camera.rect = new Rect (0f, 0f, 1f, 1f);
			if (borderCam) 
			{
				Destroy(borderCam.gameObject);
			}
			return;
		}
		
		// Pillarbox
		if (currentAspectRatio > settingsManager.wantedAspectRatio)
		{
			float inset = 1f - settingsManager.wantedAspectRatio / currentAspectRatio;
			camera.rect = new Rect (inset/2, 0f, 1f-inset, 1f);
		}
		// Letterbox
		else
		{
			float inset = 1.0f - currentAspectRatio / settingsManager.wantedAspectRatio;
			camera.rect = new Rect (0f, inset/2, 1f, 1f-inset);
		}
		if (!borderCam)
		{
			// Make a new camera behind the normal camera which displays black; otherwise the unused space is undefined
			borderCam = new GameObject ("BorderCamera", typeof (Camera)).camera;
			borderCam.transform.parent = this.transform;
			borderCam.depth = int.MinValue;
			borderCam.clearFlags = CameraClearFlags.SolidColor;
			borderCam.backgroundColor = Color.black;
			borderCam.cullingMask = 0;
		}
	}
	

	public Vector2 LimitMouseToAspect (Vector2 mousePosition)
	{
		if (settingsManager == null || !settingsManager.forceAspectRatio)
		{
			return mousePosition;
		}

		if (settingsManager.wantedAspectRatio > 1f)
		{
			// Letterbox
			int screenHeight = (int) (Screen.height * camera.rect.height);
			int yOffset = (int) (Screen.height * camera.rect.y);

			if (mousePosition.y < yOffset)
			{
				mousePosition.y = yOffset;
			}
			else if (mousePosition.y > screenHeight + yOffset)
			{
				mousePosition.y = screenHeight + yOffset;
			}
		}
		else if (settingsManager.wantedAspectRatio < 1f)
		{
			// Pillarbox
			int screenWidth = (int) (Screen.width * camera.rect.width);
			int xOffset = (int) (Screen.width * camera.rect.x);

			if (mousePosition.x < xOffset)
			{
				mousePosition.x = xOffset;
			}
			else if (mousePosition.x > screenWidth + xOffset)
			{
				mousePosition.x = screenWidth + xOffset;
			}
		}

		return mousePosition;
	}


	public static Rect _LimitMenuToAspect (Rect rect)
	{
		if (mainCam == null)
		{
			mainCam = GameObject.FindWithTag (Tags.mainCamera).GetComponent <MainCamera>();
		}

		return mainCam.LimitMenuToAspect (rect);
	}


	public Rect LimitMenuToAspect (Rect rect)
	{
		if (settingsManager == null || !settingsManager.forceAspectRatio)
		{
			return rect;
		}
		
		if (settingsManager.wantedAspectRatio > 1f)
		{
			// Letterbox
			int screenHeight = (int) (Screen.height * camera.rect.height);
			int yOffset = (int) (Screen.height * camera.rect.y);

			if (rect.y < yOffset)
			{
				rect.y = yOffset;
			}
			else if (rect.y + rect.height > screenHeight + yOffset)
			{
				rect.y = screenHeight + yOffset - rect.height;
			}
		}
		else if (settingsManager.wantedAspectRatio < 1f)
		{
			// Pillarbox
			int screenWidth = (int) (Screen.width * camera.rect.width);
			int xOffset = (int) (Screen.width * camera.rect.x);

			if (rect.x < xOffset)
			{
				rect.x = xOffset;
			}
			else if (rect.x + rect.width > screenWidth + xOffset)
			{
				rect.x = screenWidth + xOffset - rect.width;
			}
		}
		
		return rect;
	}
	
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Camera/MainCamera.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000