Skip to content

Commit

Permalink
- added a couple transitions that return custom Meshes
Browse files Browse the repository at this point in the history
- added some events that fire at relevant times
- removed the useless onLevelWasLoaded method from the delegate
- added xml comments
- added helper method to load up a single pixel, clear texture for transitions to a new scene
  • Loading branch information
prime31 committed Nov 8, 2014
1 parent 118149e commit da819db
Show file tree
Hide file tree
Showing 22 changed files with 518 additions and 57 deletions.
77 changes: 61 additions & 16 deletions Assets/Plugins/TransitionKit/TransitionKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@ namespace Prime31.TransitionKit
{
public class TransitionKit : MonoBehaviour
{
private static bool _isInitialized = false;
/// <summary>
/// fired when the screen has been fully obscured. You are clear to "do stuff" if need be when this fires
/// </summary>
public static event Action onScreenObscured;

/// <summary>
/// fired when the transition is complete and TransitionKit has destroyed all of its objects
/// </summary>
public static event Action onTransitionComplete;


private bool _isInitialized = false;
private const int _transitionKitLayer = 31;

private Texture2D _screenSnapshot;
private TransitionKitDelegate _transitionKitDelegate;

/// <summary>
/// provides easy access to the camera used to obscure the screen. Handy when you want to change the clear flags for example.
/// </summary>
public Camera transitionKitCamera;

/// <summary>
/// material access for delegates so they can mess with shader/material properties
/// </summary>
public Material material;


Expand All @@ -36,6 +53,7 @@ public static TransitionKit instance
var obj = new GameObject( "TransitionKit" );
obj.layer = _transitionKitLayer;
obj.transform.position = new Vector3( 99999f, 99999f, 99999f );

_instance = obj.AddComponent<TransitionKit>();
DontDestroyOnLoad( obj );
}
Expand All @@ -58,15 +76,15 @@ void initialize()
material.shader = _transitionKitDelegate.shaderForTransition() ?? Shader.Find( "Unlit/Texture" );

// snapshot the main camera before proceeding
_instance.StartCoroutine( _instance.captureScreenshotAndSetupCamera() );
_instance.StartCoroutine( _instance.setupCameraAndTexture() );

_isInitialized = true;
}


Mesh generateQuadMesh()
{
var halfHeight = 0.5f * 2f * 5f;
var halfHeight = 5f; // 5 is the camera.orthoSize which is the half height
var halfWidth = halfHeight * ( (float)Screen.width / (float)Screen.height );

var mesh = new Mesh();
Expand All @@ -86,21 +104,16 @@ Mesh generateQuadMesh()
};
mesh.triangles = new int[] { 0, 1, 2, 3, 2, 1 };


return mesh;
}


IEnumerator captureScreenshotAndSetupCamera()
IEnumerator setupCameraAndTexture()
{
yield return new WaitForEndOfFrame();

_screenSnapshot = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false, false );
_screenSnapshot.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0, false );
_screenSnapshot.Apply();

// load up the texture
material.mainTexture = _screenSnapshot;
material.mainTexture = _transitionKitDelegate.textureForDisplay() ?? getScreenshotTexture();

// create our camera to cover the screen
transitionKitCamera = gameObject.AddComponent<Camera>();
Expand All @@ -112,39 +125,71 @@ IEnumerator captureScreenshotAndSetupCamera()

if( _transitionKitDelegate != null )
StartCoroutine( _transitionKitDelegate.onScreenObscured( this ) );

if( onScreenObscured != null )
onScreenObscured();
}


void OnLevelWasLoaded( int level )
Texture2D getScreenshotTexture()
{
if( _transitionKitDelegate != null )
_transitionKitDelegate.onLevelWasLoaded( this, level );
var screenSnapshot = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false, false );
screenSnapshot.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0, false );
screenSnapshot.Apply();

return screenSnapshot;
}


#region Public

/// <summary>
/// starts up a transition with the given delegate
/// </summary>
/// <param name="transitionKitDelegate">Transition kit delegate.</param>
public void transitionWithDelegate( TransitionKitDelegate transitionKitDelegate )
{
_transitionKitDelegate = transitionKitDelegate;
initialize();
}


/// <summary>
/// makes a single pixel Texture2D with a transparent pixel. Useful for fading from obscured to a new scene. Note that of course
/// your shader must support transparency for this to be useful
/// </summary>
public void makeTextureTransparent()
{
var tex = new Texture2D( 1, 1 );
tex.SetPixel( 0, 0, Color.clear );
tex.Apply();

material.mainTexture = tex;
}


/// <summary>
/// delegates MUST call this when they are done with their transition! It signals a cleanup (duh) and notifies event listeners
/// </summary>
public void cleanup()
{
if( _instance == null )
return;

Destroy( _screenSnapshot );
_instance._screenSnapshot = null;
if( onTransitionComplete != null )
onTransitionComplete();

Destroy( gameObject );
_instance = null;
_isInitialized = false;
}


/// <summary>
/// helper for delegates that returns control back when the given level has loaded. Very handy when using async loading.
/// </summary>
/// <returns>The for level to load.</returns>
/// <param name="level">Level.</param>
public IEnumerator waitForLevelToLoad( int level )
{
while( Application.loadedLevel != level )
Expand Down
20 changes: 13 additions & 7 deletions Assets/Plugins/TransitionKit/TransitionKitDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Prime31.TransitionKit
{
/// <summary>
/// this is the interface an object must conform to for use with TransitionKit. Delegates can return custom shaders, meshes
/// and textures if needed. The only real stipulation is that at some point (preferably when the transition is complete) the
/// delegate must notifiy TransitionKit by calling transitionKit.cleanup().
/// </summary>
public interface TransitionKitDelegate
{
/// <summary>
Expand All @@ -14,22 +19,23 @@ public interface TransitionKitDelegate


/// <summary>
/// if the transition needs a custom Mesh return it here otherwise return null which will use a full screen quad
/// if the transition needs a custom Mesh return it here otherwise return null which will use a full screen quad.
/// The Mesh should be centered in screen-space. See the TransitionKit.generateQuadMesh for an example.
/// </summary>
/// <returns>The for.</returns>
/// <returns>the Mesh</returns>
Mesh meshForDisplay();


/// <summary>
/// called when the screen is fully obscured. You can now load a new scene or modify the current one and it will be fully obscured from view
/// if the transition needs a custom Texture2D return it here otherwise return null which will use a screenshot
/// </summary>
IEnumerator onScreenObscured( TransitionKit transitionKit );
/// <returns>the Texture2D.</returns>
Texture2D textureForDisplay();


/// <summary>
/// called when Unity's MonoBehaviourOnLevelWasLoaded is called. When doing a level load this is where you would want to start your transition
/// called when the screen is fully obscured. You can now load a new scene or modify the current one and it will be fully obscured from view
/// </summary>
/// <param name="transitionKit">Transition kit.</param>
void onLevelWasLoaded( TransitionKit transitionKit, int level );
IEnumerator onScreenObscured( TransitionKit transitionKit );
}
}
10 changes: 6 additions & 4 deletions Assets/Plugins/TransitionKit/transitions/BlurTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public Mesh meshForDisplay()
}


public Texture2D textureForDisplay()
{
return null;
}


public IEnumerator onScreenObscured( TransitionKit transitionKit )
{
transitionKit.transitionKitCamera.clearFlags = CameraClearFlags.Nothing;
Expand All @@ -51,10 +57,6 @@ public IEnumerator onScreenObscured( TransitionKit transitionKit )
transitionKit.cleanup();
}


public void onLevelWasLoaded( TransitionKit transitionKit, int level )
{}

#endregion

}
19 changes: 11 additions & 8 deletions Assets/Plugins/TransitionKit/transitions/FadeTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class FadeTransition : TransitionKitDelegate
{
public Color fadeToColor = Color.black;
public float duration = 0.5f;
/// <summary>
/// the effect looks best when it pauses before fading back. When not doing a scene-to-scene transition you may want
/// to pause for a breif moment before fading back.
/// </summary>
public float fadedDelay = 0f;
public int nextScene = -1;

Expand All @@ -27,6 +31,12 @@ public Mesh meshForDisplay()
}


public Texture2D textureForDisplay()
{
return null;
}


public IEnumerator onScreenObscured( TransitionKit transitionKit )
{
transitionKit.transitionKitCamera.clearFlags = CameraClearFlags.Nothing;
Expand All @@ -45,11 +55,7 @@ public IEnumerator onScreenObscured( TransitionKit transitionKit )
yield return null;
}

var tex = new Texture2D( 1, 1 );
tex.SetPixel( 0, 0, Color.clear );
tex.Apply();

transitionKit.material.mainTexture = tex;
transitionKit.makeTextureTransparent();

if( fadedDelay > 0 )
yield return new WaitForSeconds( fadedDelay );
Expand All @@ -71,9 +77,6 @@ public IEnumerator onScreenObscured( TransitionKit transitionKit )
}


public void onLevelWasLoaded( TransitionKit transitionKit, int level )
{}

#endregion

}
Expand Down
10 changes: 6 additions & 4 deletions Assets/Plugins/TransitionKit/transitions/PixelateTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public Mesh meshForDisplay()
}


public Texture2D textureForDisplay()
{
return null;
}


public IEnumerator onScreenObscured( TransitionKit transitionKit )
{
if( nextScene >= 0 )
Expand Down Expand Up @@ -89,10 +95,6 @@ public IEnumerator onScreenObscured( TransitionKit transitionKit )
transitionKit.cleanup();
}


public void onLevelWasLoaded( TransitionKit transitionKit, int level )
{}

#endregion


Expand Down
16 changes: 7 additions & 9 deletions Assets/Plugins/TransitionKit/transitions/SquaresTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public Mesh meshForDisplay()
}


public Texture2D textureForDisplay()
{
return null;
}


public IEnumerator onScreenObscured( TransitionKit transitionKit )
{
transitionKit.transitionKitCamera.clearFlags = CameraClearFlags.Nothing;
Expand All @@ -49,11 +55,7 @@ public IEnumerator onScreenObscured( TransitionKit transitionKit )
yield return null;
}

var tex = new Texture2D( 1, 1 );
tex.SetPixel( 0, 0, Color.clear );
tex.Apply();

transitionKit.material.mainTexture = tex;
transitionKit.makeTextureTransparent();

if( fadedDelay > 0 )
yield return new WaitForSeconds( fadedDelay );
Expand All @@ -74,10 +76,6 @@ public IEnumerator onScreenObscured( TransitionKit transitionKit )
transitionKit.cleanup();
}


public void onLevelWasLoaded( TransitionKit transitionKit, int level )
{}

#endregion

}
Expand Down
Loading

0 comments on commit da819db

Please sign in to comment.