Browse Source

Fix #64 Null camera bug in CameraController

master
chrisgregan 10 years ago
parent
commit
c6db4cc0ad
  1. 116
      Assets/Fungus/Camera/Scripts/CameraController.cs

116
Assets/Fungus/Camera/Scripts/CameraController.cs

@ -195,12 +195,16 @@ namespace Fungus
Sprite sprite = spriteRenderer.sprite; Sprite sprite = spriteRenderer.sprite;
Vector3 extents = sprite.bounds.extents; Vector3 extents = sprite.bounds.extents;
float localScaleY = spriteRenderer.transform.localScale.y; float localScaleY = spriteRenderer.transform.localScale.y;
Camera.main.orthographicSize = extents.y * localScaleY;
Camera camera = GetCamera();
if (camera != null)
{
camera.orthographicSize = extents.y * localScaleY;
Vector3 pos = spriteRenderer.transform.position; Vector3 pos = spriteRenderer.transform.position;
Camera.main.transform.position = new Vector3(pos.x, pos.y, 0); camera.transform.position = new Vector3(pos.x, pos.y, 0);
SetCameraZ(); SetCameraZ();
} }
}
public virtual void PanToView(View view, float duration, Action arriveAction) public virtual void PanToView(View view, float duration, Action arriveAction)
{ {
@ -220,9 +224,13 @@ namespace Fungus
if (duration == 0f) if (duration == 0f)
{ {
// Move immediately // Move immediately
Camera.main.orthographicSize = targetSize; Camera camera = GetCamera();
Camera.main.transform.position = targetPosition; if (camera != null)
Camera.main.transform.rotation = targetRotation; {
camera.orthographicSize = targetSize;
camera.transform.position = targetPosition;
camera.transform.rotation = targetRotation;
}
SetCameraZ(); SetCameraZ();
if (arriveAction != null) if (arriveAction != null)
{ {
@ -239,13 +247,17 @@ namespace Fungus
* Stores the current camera view using a name. * Stores the current camera view using a name.
*/ */
public virtual void StoreView(string viewName) public virtual void StoreView(string viewName)
{
Camera camera = GetCamera();
if (camera != null)
{ {
CameraView currentView = new CameraView(); CameraView currentView = new CameraView();
currentView.cameraPos = Camera.main.transform.position; currentView.cameraPos = camera.transform.position;
currentView.cameraRot = Camera.main.transform.rotation; currentView.cameraRot = camera.transform.rotation;
currentView.cameraSize = Camera.main.orthographicSize; currentView.cameraSize = camera.orthographicSize;
storedViews[viewName] = currentView; storedViews[viewName] = currentView;
} }
}
/** /**
* Moves the camera to a previously stored camera view over a period of time. * Moves the camera to a previously stored camera view over a period of time.
@ -267,9 +279,14 @@ namespace Fungus
if (duration == 0f) if (duration == 0f)
{ {
// Move immediately // Move immediately
Camera.main.transform.position = cameraView.cameraPos; Camera camera = GetCamera();
Camera.main.transform.rotation = cameraView.cameraRot; if (camera != null)
Camera.main.orthographicSize = cameraView.cameraSize; {
camera.transform.position = cameraView.cameraPos;
camera.transform.rotation = cameraView.cameraRot;
camera.orthographicSize = cameraView.cameraSize;
}
SetCameraZ(); SetCameraZ();
if (arriveAction != null) if (arriveAction != null)
{ {
@ -284,12 +301,18 @@ namespace Fungus
protected virtual IEnumerator PanInternal(Vector3 targetPos, Quaternion targetRot, float targetSize, float duration, Action arriveAction) protected virtual IEnumerator PanInternal(Vector3 targetPos, Quaternion targetRot, float targetSize, float duration, Action arriveAction)
{ {
Camera camera = GetCamera();
if (camera == null)
{
yield break;
}
float timer = 0; float timer = 0;
float startSize = Camera.main.orthographicSize; float startSize = camera.orthographicSize;
float endSize = targetSize; float endSize = targetSize;
Vector3 startPos = Camera.main.transform.position; Vector3 startPos = camera.transform.position;
Vector3 endPos = targetPos; Vector3 endPos = targetPos;
Quaternion startRot = Camera.main.transform.rotation; Quaternion startRot = camera.transform.rotation;
Quaternion endRot = targetRot; Quaternion endRot = targetRot;
bool arrived = false; bool arrived = false;
@ -304,9 +327,9 @@ namespace Fungus
// Apply smoothed lerp to camera position and orthographic size // Apply smoothed lerp to camera position and orthographic size
float t = timer / duration; float t = timer / duration;
Camera.main.orthographicSize = Mathf.Lerp(startSize, endSize, Mathf.SmoothStep(0f, 1f, t)); camera.orthographicSize = Mathf.Lerp(startSize, endSize, Mathf.SmoothStep(0f, 1f, t));
Camera.main.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t)); camera.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t));
Camera.main.transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0f, 1f, t)); camera.transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0f, 1f, t));
SetCameraZ(); SetCameraZ();
if (arrived && if (arrived &&
@ -324,15 +347,21 @@ namespace Fungus
*/ */
public virtual void PanToPath(View[] viewList, float duration, Action arriveAction) public virtual void PanToPath(View[] viewList, float duration, Action arriveAction)
{ {
Camera camera = GetCamera();
if (camera == null)
{
return;
}
swipePanActive = false; swipePanActive = false;
List<Vector3> pathList = new List<Vector3>(); List<Vector3> pathList = new List<Vector3>();
// Add current camera position as first point in path // Add current camera position as first point in path
// Note: We use the z coord to tween the camera orthographic size // Note: We use the z coord to tween the camera orthographic size
Vector3 startPos = new Vector3(Camera.main.transform.position.x, Vector3 startPos = new Vector3(camera.transform.position.x,
Camera.main.transform.position.y, camera.transform.position.y,
Camera.main.orthographicSize); camera.orthographicSize);
pathList.Add(startPos); pathList.Add(startPos);
for (int i = 0; i < viewList.Length; ++i) for (int i = 0; i < viewList.Length; ++i)
@ -350,6 +379,12 @@ namespace Fungus
protected virtual IEnumerator PanToPathInternal(float duration, Action arriveAction, Vector3[] path) protected virtual IEnumerator PanToPathInternal(float duration, Action arriveAction, Vector3[] path)
{ {
Camera camera = GetCamera();
if (camera == null)
{
yield break;
}
float timer = 0; float timer = 0;
while (timer < duration) while (timer < duration)
@ -360,8 +395,8 @@ namespace Fungus
Vector3 point = iTween.PointOnPath(path, percent); Vector3 point = iTween.PointOnPath(path, percent);
Camera.main.transform.position = new Vector3(point.x, point.y, 0); camera.transform.position = new Vector3(point.x, point.y, 0);
Camera.main.orthographicSize = point.z; camera.orthographicSize = point.z;
SetCameraZ(); SetCameraZ();
yield return null; yield return null;
@ -379,10 +414,16 @@ namespace Fungus
*/ */
public virtual void StartSwipePan(View viewA, View viewB, float duration, Action arriveAction) public virtual void StartSwipePan(View viewA, View viewB, float duration, Action arriveAction)
{ {
Camera camera = GetCamera();
if (camera == null)
{
return;
}
swipePanViewA = viewA; swipePanViewA = viewA;
swipePanViewB = viewB; swipePanViewB = viewB;
Vector3 cameraPos = Camera.main.transform.position; Vector3 cameraPos = camera.transform.position;
Vector3 targetPosition = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB); Vector3 targetPosition = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB);
float targetSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB); float targetSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB);
@ -410,7 +451,11 @@ namespace Fungus
protected virtual void SetCameraZ() protected virtual void SetCameraZ()
{ {
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, cameraZ); Camera camera = GetCamera();
if (camera)
{
camera.transform.position = new Vector3(camera.transform.position.x, camera.transform.position.y, cameraZ);
}
} }
protected virtual void Update() protected virtual void Update()
@ -440,17 +485,21 @@ namespace Fungus
previousMousePos = Input.mousePosition; previousMousePos = Input.mousePosition;
} }
Vector3 cameraDelta = Camera.main.ScreenToViewportPoint(delta); Camera camera = GetCamera();
if (camera != null)
{
Vector3 cameraDelta = camera.ScreenToViewportPoint(delta);
cameraDelta.x *= -2f; cameraDelta.x *= -2f;
cameraDelta.y *= -2f; cameraDelta.y *= -2f;
cameraDelta.z = 0f; cameraDelta.z = 0f;
Vector3 cameraPos = Camera.main.transform.position; Vector3 cameraPos = camera.transform.position;
cameraPos += cameraDelta; cameraPos += cameraDelta;
Camera.main.transform.position = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB); camera.transform.position = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB);
Camera.main.orthographicSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB); camera.orthographicSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB);
}
} }
// Clamp camera position to region defined by the two views // Clamp camera position to region defined by the two views
@ -487,5 +536,16 @@ namespace Fungus
return cameraSize; return cameraSize;
} }
protected Camera GetCamera()
{
Camera camera = Camera.main;
if (camera == null)
{
camera = GameObject.FindObjectOfType<Camera>() as Camera;
}
return camera;
}
} }
} }
Loading…
Cancel
Save