Skip to content

Commit

Permalink
Add collection transition events
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvinas01 committed Sep 28, 2022
1 parent 0481da3 commit d03217b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Minor UX updates and bug fixes.
- More logging which shows that a scene is being currently loaded.
- Methods to access in `loadingCollection` and `loadedCollection` in `ScriptableSceneController`.
- Exposed `IsLoading` property in `ScriptableSceneController`.
- Events in `BaseScriptableSceneTransition`.
- Events which are fired when transitions are entered and exited.

### Changed
- Updated Scene Manager Window to support reordering and to provide more info.
Expand Down
73 changes: 73 additions & 0 deletions Runtime/Events/CollectionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ internal sealed class CollectionEventHandler : ICollectionEventHandler
private UnityEvent<CollectionUnloadEventArgs> onUnloadExited =
new UnityEvent<CollectionUnloadEventArgs>();

[SerializeField]
private UnityEvent onShowTransitionEntered = new UnityEvent();

[SerializeField]
private UnityEvent onShowTransitionExited = new UnityEvent();

[SerializeField]
private UnityEvent onHideTransitionEntered = new UnityEvent();

[SerializeField]
private UnityEvent onHideTransitionExited = new UnityEvent();

#endregion

#region Public Events
Expand All @@ -46,6 +58,14 @@ internal sealed class CollectionEventHandler : ICollectionEventHandler

public event CollectionUnloadEvent OnUnloadExited;

public event CollectionTransitionEvent OnShowTransitionEntered;

public event CollectionTransitionEvent OnShowTransitionExited;

public event CollectionTransitionEvent OnHideTransitionEntered;

public event CollectionTransitionEvent OnHideTransitionExited;

#endregion

#region Internal Methods
Expand Down Expand Up @@ -156,6 +176,59 @@ internal void RaiseUnloadExited(CollectionUnloadEventArgs args)
}
}

// TODO: these events below are not tested
internal void RaiseShowTransitionEntered()
{
try
{
onShowTransitionEntered.Invoke();
OnShowTransitionEntered?.Invoke();
}
catch (Exception exception)
{
Debug.LogError(exception);
}
}

internal void RaiseShowTransitionExited()
{
try
{
onShowTransitionExited.Invoke();
OnShowTransitionExited?.Invoke();
}
catch (Exception exception)
{
Debug.LogError(exception);
}
}

internal void RaiseHideTransitionEntered()
{
try
{
onHideTransitionEntered.Invoke();
OnHideTransitionEntered?.Invoke();
}
catch (Exception exception)
{
Debug.LogError(exception);
}
}

internal void RaiseHideTransitionExited()
{
try
{
onHideTransitionExited.Invoke();
OnHideTransitionExited?.Invoke();
}
catch (Exception exception)
{
Debug.LogError(exception);
}
}

#endregion
}
}
6 changes: 6 additions & 0 deletions Runtime/Events/CollectionEventHandlerDelegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
/// Invoked when <see cref="BaseScriptableSceneCollection"/> starts to unload or is unloaded.
/// </summary>
public delegate void CollectionUnloadEvent(CollectionUnloadEventArgs args);

/// <summary>
/// Invoked when <see cref="BaseScriptableSceneCollection"/> starts to show or hides the
/// transition.
/// </summary>
public delegate void CollectionTransitionEvent(); // TODO: add args
}
20 changes: 20 additions & 0 deletions Runtime/Events/ICollectionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public interface ICollectionEventHandler
/// </summary>
public event CollectionUnloadEvent OnUnloadExited;

/// <summary>
/// Called when transition of <see cref="BaseScriptableSceneCollection"/> starts to show.
/// </summary>
public event CollectionTransitionEvent OnShowTransitionEntered;

/// <summary>
/// Called when transition of <see cref="BaseScriptableSceneCollection"/> is shown.
/// </summary>
public event CollectionTransitionEvent OnShowTransitionExited;

/// <summary>
/// Called when transition of <see cref="BaseScriptableSceneCollection"/> starts to hide.
/// </summary>
public event CollectionTransitionEvent OnHideTransitionEntered;

/// <summary>
/// Called when transition of <see cref="BaseScriptableSceneCollection"/> is hidden.
/// </summary>
public event CollectionTransitionEvent OnHideTransitionExited;

#endregion
}
}
4 changes: 4 additions & 0 deletions Runtime/ScriptableSceneCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ public override IEnumerator ShowTransitionRoutine()
{
if (transition)
{
collectionEvents.RaiseShowTransitionEntered();
yield return transition.ShowRoutine();
collectionEvents.RaiseShowTransitionExited();
}
}

public override IEnumerator HideTransitionRoutine()
{
if (transition)
{
collectionEvents.RaiseHideTransitionEntered();
yield return transition.HideRoutine();
collectionEvents.RaiseHideTransitionExited();
}
}

Expand Down
8 changes: 8 additions & 0 deletions Runtime/Utilities/ScriptableSceneUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ CollectionEventHandler otherHandler
handler.OnLoadProgress += otherHandler.RaiseLoadProgress;
handler.OnUnloadEntered += otherHandler.RaiseUnloadEntered;
handler.OnUnloadExited += otherHandler.RaiseUnloadExited;
handler.OnShowTransitionEntered += otherHandler.RaiseShowTransitionEntered;
handler.OnShowTransitionExited += otherHandler.RaiseShowTransitionExited;
handler.OnHideTransitionEntered += otherHandler.RaiseHideTransitionEntered;
handler.OnHideTransitionExited += otherHandler.RaiseHideTransitionExited;
}

// TODO: how can we avoid this?
Expand All @@ -193,6 +197,10 @@ CollectionEventHandler otherHandler
handler.OnLoadProgress -= otherHandler.RaiseLoadProgress;
handler.OnUnloadEntered -= otherHandler.RaiseUnloadEntered;
handler.OnUnloadExited -= otherHandler.RaiseUnloadExited;
handler.OnShowTransitionEntered -= otherHandler.RaiseShowTransitionEntered;
handler.OnShowTransitionExited -= otherHandler.RaiseShowTransitionExited;
handler.OnHideTransitionEntered -= otherHandler.RaiseHideTransitionEntered;
handler.OnHideTransitionExited -= otherHandler.RaiseHideTransitionExited;
}

// TODO: how can we avoid this?
Expand Down

0 comments on commit d03217b

Please sign in to comment.