Browse Source

Added Fade Screen command

master
chrisgregan 10 years ago
parent
commit
255970d507
  1. BIN
      Assets/Example/Scenes/Example.unity
  2. 61
      Assets/Fungus/Camera/Commands/FadeScreen.cs
  3. 8
      Assets/Fungus/Camera/Commands/FadeScreen.cs.meta
  4. 152
      Assets/Fungus/Camera/Scripts/CameraController.cs

BIN
Assets/Example/Scenes/Example.unity

Binary file not shown.

61
Assets/Fungus/Camera/Commands/FadeScreen.cs

@ -0,0 +1,61 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus
{
[CommandInfo("Camera",
"Fade Screen",
"Draws a fullscreen texture over the scene to give a fade effect. Target alpha 1 will obscure the screen, alpha 0 will reveal the screen.")]
public class FadeScreen : Command
{
public float duration = 1f;
public float targetAlpha = 1f;
public bool waitUntilFinished = true;
public Color fadeColor = Color.black;
public Texture2D fadeTexture;
public override void OnEnter()
{
CameraController cameraController = CameraController.GetInstance();
if (waitUntilFinished)
{
cameraController.waiting = true;
}
if (fadeTexture)
{
cameraController.screenFadeTexture = fadeTexture;
}
else
{
cameraController.screenFadeTexture = CameraController.CreateColorTexture(fadeColor, 32, 32);
}
cameraController.Fade(targetAlpha, duration, delegate {
if (waitUntilFinished)
{
cameraController.waiting = false;
Continue();
}
});
if (!waitUntilFinished)
{
Continue();
}
}
public override string GetSummary()
{
return "Fade to " + targetAlpha + " over " + duration + " seconds";
}
public override Color GetButtonColor()
{
return new Color32(216, 228, 170, 255);
}
}
}

8
Assets/Fungus/Camera/Commands/FadeScreen.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 207aecf668a0345388087ccf522f9957
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

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

@ -16,45 +16,45 @@ namespace Fungus
* Full screen texture used for screen fade effect. * Full screen texture used for screen fade effect.
*/ */
public Texture2D screenFadeTexture; public Texture2D screenFadeTexture;
/** /**
* Icon to display when swipe pan mode is active. * Icon to display when swipe pan mode is active.
*/ */
public Texture2D swipePanIcon; public Texture2D swipePanIcon;
/** /**
* Position of continue and swipe icons in normalized screen space coords. * Position of continue and swipe icons in normalized screen space coords.
* (0,0) = top left, (1,1) = bottom right * (0,0) = top left, (1,1) = bottom right
*/ */
public Vector2 swipeIconPosition = new Vector2(1,0); public Vector2 swipeIconPosition = new Vector2(1,0);
/** /**
* Fixed Z coordinate of main camera. * Fixed Z coordinate of main camera.
*/ */
public float cameraZ = -10f; public float cameraZ = -10f;
[HideInInspector] [HideInInspector]
public bool swipePanActive; public bool swipePanActive;
[HideInInspector] [HideInInspector]
public bool waiting; public bool waiting;
float fadeAlpha = 0f; float fadeAlpha = 0f;
// Swipe panning control // Swipe panning control
View swipePanViewA; View swipePanViewA;
View swipePanViewB; View swipePanViewB;
Vector3 previousMousePos; Vector3 previousMousePos;
class CameraView class CameraView
{ {
public Vector3 cameraPos; public Vector3 cameraPos;
public Quaternion cameraRot; public Quaternion cameraRot;
public float cameraSize; public float cameraSize;
}; };
Dictionary<string, CameraView> storedViews = new Dictionary<string, CameraView>(); Dictionary<string, CameraView> storedViews = new Dictionary<string, CameraView>();
static CameraController instance; static CameraController instance;
/** /**
@ -71,7 +71,7 @@ namespace Fungus
return instance; return instance;
} }
public static Texture2D CreateColorTexture(Color color, int width, int height) public static Texture2D CreateColorTexture(Color color, int width, int height)
{ {
Color[] pixels = new Color[width * height]; Color[] pixels = new Color[width * height];
@ -82,10 +82,10 @@ namespace Fungus
Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
texture.SetPixels(pixels); texture.SetPixels(pixels);
texture.Apply(); texture.Apply();
return texture; return texture;
} }
void OnGUI() void OnGUI()
{ {
if (swipePanActive) if (swipePanActive)
@ -107,19 +107,19 @@ namespace Fungus
GUI.DrawTexture(rect, swipePanIcon); GUI.DrawTexture(rect, swipePanIcon);
} }
} }
// Draw full screen fade texture // Draw full screen fade texture
if (fadeAlpha < 1f && if (fadeAlpha > 0f &&
screenFadeTexture != null) screenFadeTexture != null)
{ {
// 1 = scene fully visible // 1 = scene fully visible
// 0 = scene fully obscured // 0 = scene fully obscured
GUI.color = new Color(1,1,1, 1f - fadeAlpha); GUI.color = new Color(1,1,1, fadeAlpha);
GUI.depth = -1000; GUI.depth = -1000;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), screenFadeTexture); GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), screenFadeTexture);
} }
} }
/** /**
* Perform a fullscreen fade over a duration. * Perform a fullscreen fade over a duration.
*/ */
@ -127,23 +127,23 @@ namespace Fungus
{ {
StartCoroutine(FadeInternal(targetAlpha, fadeDuration, fadeAction)); StartCoroutine(FadeInternal(targetAlpha, fadeDuration, fadeAction));
} }
/** /**
* Fade out, move camera to view and then fade back in. * Fade out, move camera to view and then fade back in.
*/ */
public void FadeToView(View view, float fadeDuration, Action fadeAction) public void FadeToView(View view, float fadeDuration, Action fadeAction)
{ {
swipePanActive = false; swipePanActive = false;
fadeAlpha = 1f; fadeAlpha = 0f;
// Fade out // Fade out
Fade(0f, fadeDuration / 2f, delegate { Fade(1f, fadeDuration / 2f, delegate {
// Snap to new view // Snap to new view
PanToPosition(view.transform.position, view.transform.rotation, view.viewSize, 0f, null); PanToPosition(view.transform.position, view.transform.rotation, view.viewSize, 0f, null);
// Fade in // Fade in
Fade(1f, fadeDuration / 2f, delegate { Fade(0f, fadeDuration / 2f, delegate {
if (fadeAction != null) if (fadeAction != null)
{ {
fadeAction(); fadeAction();
@ -151,12 +151,12 @@ namespace Fungus
}); });
}); });
} }
IEnumerator FadeInternal(float targetAlpha, float fadeDuration, Action fadeAction) IEnumerator FadeInternal(float targetAlpha, float fadeDuration, Action fadeAction)
{ {
float startAlpha = fadeAlpha; float startAlpha = fadeAlpha;
float timer = 0; float timer = 0;
// If already at the target alpha then complete immediately // If already at the target alpha then complete immediately
if (startAlpha == targetAlpha) if (startAlpha == targetAlpha)
{ {
@ -168,22 +168,22 @@ namespace Fungus
{ {
float t = timer / fadeDuration; float t = timer / fadeDuration;
timer += Time.deltaTime; timer += Time.deltaTime;
t = Mathf.Clamp01(t); t = Mathf.Clamp01(t);
fadeAlpha = Mathf.Lerp(startAlpha, targetAlpha, t); fadeAlpha = Mathf.Lerp(startAlpha, targetAlpha, t);
yield return null; yield return null;
} }
} }
fadeAlpha = targetAlpha; fadeAlpha = targetAlpha;
if (fadeAction != null) if (fadeAction != null)
{ {
fadeAction(); fadeAction();
} }
} }
/** /**
* Positions camera so sprite is centered and fills the screen. * Positions camera so sprite is centered and fills the screen.
* @param spriteRenderer The sprite to center the camera on * @param spriteRenderer The sprite to center the camera on
@ -191,7 +191,7 @@ namespace Fungus
public void CenterOnSprite(SpriteRenderer spriteRenderer) public void CenterOnSprite(SpriteRenderer spriteRenderer)
{ {
swipePanActive = false; swipePanActive = false;
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;
@ -201,12 +201,12 @@ namespace Fungus
Camera.main.transform.position = new Vector3(pos.x, pos.y, 0); Camera.main.transform.position = new Vector3(pos.x, pos.y, 0);
SetCameraZ(); SetCameraZ();
} }
public void PanToView(View view, float duration, Action arriveAction) public void PanToView(View view, float duration, Action arriveAction)
{ {
PanToPosition(view.transform.position, view.transform.rotation, view.viewSize, duration, arriveAction); PanToPosition(view.transform.position, view.transform.rotation, view.viewSize, duration, arriveAction);
} }
/** /**
* Moves camera from current position to a target position over a period of time. * Moves camera from current position to a target position over a period of time.
*/ */
@ -214,7 +214,7 @@ namespace Fungus
{ {
// Stop any pan that is currently active // Stop any pan that is currently active
StopAllCoroutines(); StopAllCoroutines();
swipePanActive = false; swipePanActive = false;
if (duration == 0f) if (duration == 0f)
@ -234,7 +234,7 @@ namespace Fungus
StartCoroutine(PanInternal(targetPosition, targetRotation, targetSize, duration, arriveAction)); StartCoroutine(PanInternal(targetPosition, targetRotation, targetSize, duration, arriveAction));
} }
} }
/** /**
* Stores the current camera view using a name. * Stores the current camera view using a name.
*/ */
@ -246,7 +246,7 @@ namespace Fungus
currentView.cameraSize = Camera.main.orthographicSize; currentView.cameraSize = Camera.main.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.
*/ */
@ -261,9 +261,9 @@ namespace Fungus
} }
return; return;
} }
CameraView cameraView = storedViews[viewName]; CameraView cameraView = storedViews[viewName];
if (duration == 0f) if (duration == 0f)
{ {
// Move immediately // Move immediately
@ -291,7 +291,7 @@ namespace Fungus
Vector3 endPos = targetPos; Vector3 endPos = targetPos;
Quaternion startRot = Camera.main.transform.rotation; Quaternion startRot = Camera.main.transform.rotation;
Quaternion endRot = targetRot; Quaternion endRot = targetRot;
bool arrived = false; bool arrived = false;
while (!arrived) while (!arrived)
{ {
@ -301,78 +301,78 @@ namespace Fungus
arrived = true; arrived = true;
timer = duration; timer = duration;
} }
// 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.main.orthographicSize = Mathf.Lerp(startSize, endSize, Mathf.SmoothStep(0f, 1f, t));
Camera.main.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t)); Camera.main.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.main.transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0f, 1f, t));
SetCameraZ(); SetCameraZ();
if (arrived && if (arrived &&
arriveAction != null) arriveAction != null)
{ {
arriveAction(); arriveAction();
} }
yield return null; yield return null;
} }
} }
/** /**
* Moves camera smoothly through a sequence of Views over a period of time * Moves camera smoothly through a sequence of Views over a period of time
*/ */
public void PanToPath(View[] viewList, float duration, Action arriveAction) public void PanToPath(View[] viewList, float duration, Action arriveAction)
{ {
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.main.transform.position.x,
Camera.main.transform.position.y, Camera.main.transform.position.y,
Camera.main.orthographicSize); Camera.main.orthographicSize);
pathList.Add(startPos); pathList.Add(startPos);
for (int i = 0; i < viewList.Length; ++i) for (int i = 0; i < viewList.Length; ++i)
{ {
View view = viewList[i]; View view = viewList[i];
Vector3 viewPos = new Vector3(view.transform.position.x, Vector3 viewPos = new Vector3(view.transform.position.x,
view.transform.position.y, view.transform.position.y,
view.viewSize); view.viewSize);
pathList.Add(viewPos); pathList.Add(viewPos);
} }
StartCoroutine(PanToPathInternal(duration, arriveAction, pathList.ToArray())); StartCoroutine(PanToPathInternal(duration, arriveAction, pathList.ToArray()));
} }
IEnumerator PanToPathInternal(float duration, Action arriveAction, Vector3[] path) IEnumerator PanToPathInternal(float duration, Action arriveAction, Vector3[] path)
{ {
float timer = 0; float timer = 0;
while (timer < duration) while (timer < duration)
{ {
timer += Time.deltaTime; timer += Time.deltaTime;
timer = Mathf.Min(timer, duration); timer = Mathf.Min(timer, duration);
float percent = timer / duration; float percent = timer / duration;
Vector3 point = iTween.PointOnPath(path, percent); Vector3 point = iTween.PointOnPath(path, percent);
Camera.main.transform.position = new Vector3(point.x, point.y, 0); Camera.main.transform.position = new Vector3(point.x, point.y, 0);
Camera.main.orthographicSize = point.z; Camera.main.orthographicSize = point.z;
SetCameraZ(); SetCameraZ();
yield return null; yield return null;
} }
if (arriveAction != null) if (arriveAction != null)
{ {
arriveAction(); arriveAction();
} }
} }
/** /**
* Activates swipe panning mode. * Activates swipe panning mode.
* The player can pan the camera within the area between viewA & viewB. * The player can pan the camera within the area between viewA & viewB.
@ -381,23 +381,23 @@ namespace Fungus
{ {
swipePanViewA = viewA; swipePanViewA = viewA;
swipePanViewB = viewB; swipePanViewB = viewB;
Vector3 cameraPos = Camera.main.transform.position; Vector3 cameraPos = Camera.main.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);
PanToPosition(targetPosition, Quaternion.identity, targetSize, duration, delegate { PanToPosition(targetPosition, Quaternion.identity, targetSize, duration, delegate {
swipePanActive = true; swipePanActive = true;
if (arriveAction != null) if (arriveAction != null)
{ {
arriveAction(); arriveAction();
} }
}); });
} }
/** /**
* Deactivates swipe panning mode. * Deactivates swipe panning mode.
*/ */
@ -407,21 +407,21 @@ namespace Fungus
swipePanViewA = null; swipePanViewA = null;
swipePanViewB = null; swipePanViewB = null;
} }
void SetCameraZ() void SetCameraZ()
{ {
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, cameraZ); Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, cameraZ);
} }
void Update() void Update()
{ {
if (!swipePanActive) if (!swipePanActive)
{ {
return; return;
} }
Vector3 delta = Vector3.zero; Vector3 delta = Vector3.zero;
if (Input.touchCount > 0) if (Input.touchCount > 0)
{ {
if (Input.GetTouch(0).phase == TouchPhase.Moved) if (Input.GetTouch(0).phase == TouchPhase.Moved)
@ -429,7 +429,7 @@ namespace Fungus
delta = Input.GetTouch(0).deltaPosition; delta = Input.GetTouch(0).deltaPosition;
} }
} }
if (Input.GetMouseButtonDown(0)) if (Input.GetMouseButtonDown(0))
{ {
previousMousePos = Input.mousePosition; previousMousePos = Input.mousePosition;
@ -439,34 +439,34 @@ namespace Fungus
delta = Input.mousePosition - previousMousePos; delta = Input.mousePosition - previousMousePos;
previousMousePos = Input.mousePosition; previousMousePos = Input.mousePosition;
} }
Vector3 cameraDelta = Camera.main.ScreenToViewportPoint(delta); Vector3 cameraDelta = Camera.main.ScreenToViewportPoint(delta);
cameraDelta.x *= -2f; cameraDelta.x *= -2f;
cameraDelta.y *= -1f; cameraDelta.y *= -1f;
cameraDelta.z = 0f; cameraDelta.z = 0f;
Vector3 cameraPos = Camera.main.transform.position; Vector3 cameraPos = Camera.main.transform.position;
cameraPos += cameraDelta; cameraPos += cameraDelta;
Camera.main.transform.position = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB); Camera.main.transform.position = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB);
Camera.main.orthographicSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB); Camera.main.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
Vector3 CalcCameraPosition(Vector3 pos, View viewA, View viewB) Vector3 CalcCameraPosition(Vector3 pos, View viewA, View viewB)
{ {
Vector3 safePos = pos; Vector3 safePos = pos;
// Clamp camera position to region defined by the two views // Clamp camera position to region defined by the two views
safePos.x = Mathf.Max(safePos.x, Mathf.Min(viewA.transform.position.x, viewB.transform.position.x)); safePos.x = Mathf.Max(safePos.x, Mathf.Min(viewA.transform.position.x, viewB.transform.position.x));
safePos.x = Mathf.Min(safePos.x, Mathf.Max(viewA.transform.position.x, viewB.transform.position.x)); safePos.x = Mathf.Min(safePos.x, Mathf.Max(viewA.transform.position.x, viewB.transform.position.x));
safePos.y = Mathf.Max(safePos.y, Mathf.Min(viewA.transform.position.y, viewB.transform.position.y)); safePos.y = Mathf.Max(safePos.y, Mathf.Min(viewA.transform.position.y, viewB.transform.position.y));
safePos.y = Mathf.Min(safePos.y, Mathf.Max(viewA.transform.position.y, viewB.transform.position.y)); safePos.y = Mathf.Min(safePos.y, Mathf.Max(viewA.transform.position.y, viewB.transform.position.y));
return safePos; return safePos;
} }
// Smoothly interpolate camera orthographic size based on relative position to two views // Smoothly interpolate camera orthographic size based on relative position to two views
float CalcCameraSize(Vector3 pos, View viewA, View viewB) float CalcCameraSize(Vector3 pos, View viewA, View viewB)
{ {
@ -484,8 +484,8 @@ namespace Fungus
t = Mathf.Clamp01(t); // Not really necessary but no harm t = Mathf.Clamp01(t); // Not really necessary but no harm
float cameraSize = Mathf.Lerp(viewA.viewSize, viewB.viewSize, t); float cameraSize = Mathf.Lerp(viewA.viewSize, viewB.viewSize, t);
return cameraSize; return cameraSize;
} }
} }
} }
Loading…
Cancel
Save