Skip to content

Commit

Permalink
added support for loop groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Rellac-Rellac committed Feb 19, 2021
1 parent 84ee7cb commit 3914710
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 13 deletions.
8 changes: 8 additions & 0 deletions Assets/UIManager/Scripts/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions Assets/UIManager/Scripts/Editor/UIManagerEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace Rellac.UI.Editor
{
[CustomEditor(typeof(UIManager))]
[CanEditMultipleObjects]
public class UIManagerEditor : UnityEditor.Editor
{
SerializedProperty containsLoopGroup;
SerializedProperty initialPanel;
SerializedProperty loopTransitionSpeed;
SerializedProperty prevTransition;
SerializedProperty nextTransition;
SerializedProperty loopGroup;

void OnEnable()
{
containsLoopGroup = serializedObject.FindProperty("containsLoopGroup");
initialPanel = serializedObject.FindProperty("initialPanel");
loopTransitionSpeed = serializedObject.FindProperty("loopTransitionSpeed");
prevTransition = serializedObject.FindProperty("prevTransition");
nextTransition = serializedObject.FindProperty("nextTransition");
loopGroup = serializedObject.FindProperty("loopGroup");
}

public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(containsLoopGroup);
if (containsLoopGroup.boolValue)
{
EditorGUILayout.PropertyField(loopTransitionSpeed);
EditorGUILayout.PropertyField(prevTransition);
EditorGUILayout.PropertyField(nextTransition);
EditorGUILayout.PropertyField(loopGroup);
} else
{
EditorGUILayout.PropertyField(initialPanel);
}
serializedObject.ApplyModifiedProperties();
}
}
}
11 changes: 11 additions & 0 deletions Assets/UIManager/Scripts/Editor/UIManagerEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 109 additions & 7 deletions Assets/UIManager/Scripts/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,36 @@ namespace Rellac.UI
[CreateAssetMenu(fileName = "New UI Manager", menuName = "UI/Manager", order = 1)]
public class UIManager : ScriptableObject
{
/// <summary>
/// Manager loops between a selection of UIPanels
/// </summary>
[Tooltip("")]
[SerializeField] private bool containsLoopGroup = false;
/// <summary>
/// Panel to start when manager is initialised - no transition will be made
/// </summary>
[Tooltip("Panel to start when manager is initialised - no transition will be made")]
[SerializeField] private UIPanel initialPanel = null;
/// <summary>
/// Speed to play transitions across a loop group
/// </summary>
[Tooltip("Speed to play transitions across a loop group")]
[SerializeField] private float loopTransitionSpeed = 1;
/// <summary>
/// Animation to play when transitioning to the previous panel in a loop group
/// </summary>
[Tooltip("Animation to play when transitioning to the previous panel in a loop group")]
[SerializeField] private UITransition prevTransition = null;
/// <summary>
/// Animation to play when transitioning to the next panel in a loop group
/// </summary>
[Tooltip("Animation to play when transitioning to the next panel in a loop group")]
[SerializeField] private UITransition nextTransition = null;
/// <summary>
/// Group of UIPanels to transition between is manager contains a loop group
/// </summary>
[Tooltip("Group of UIPanels to transition between is manager contains a loop group")]
[SerializeField] private UIPanel[] loopGroup = null;

RectTransform root_;
/// <summary>
Expand Down Expand Up @@ -45,6 +70,27 @@ public Animator animator
private bool transitioning = false;
private UIPanel currentPanel;

private int loopIdx_;
private int loopIdx
{
get
{
return loopIdx_;
}
set
{
loopIdx_ = value;
if (loopIdx_ >= loopGroup.Length)
{
loopIdx_ = 0;
}
if (loopIdx < 0)
{
loopIdx = loopGroup.Length - 1;
}
}
}

/// <summary>
/// Initialise manager in scene
/// </summary>
Expand All @@ -63,11 +109,18 @@ public void Initialise(RectTransform parent)
fitter.Fit();
}

RectTransform panelRoot = (RectTransform)Instantiate(initialPanel.panelPrefab, inParent).transform;
UIPanel panel = initialPanel;
if (containsLoopGroup)
{
panel = loopGroup[0];
loopIdx = 0;
}

RectTransform panelRoot = (RectTransform)Instantiate(panel.panelPrefab, inParent).transform;
animator.Play("Instant");
initialPanel.Initialise(panelRoot);
root.GetComponent<MonoBehaviour>().StartCoroutine(initialPanel.WaitForTransitionIn(panelRoot));
currentPanel = initialPanel;
panel.Initialise(panelRoot);
root.GetComponent<MonoBehaviour>().StartCoroutine(panel.WaitForTransitionIn(panelRoot, panel.animationSpeed));
currentPanel = panel;
transitioning = false;
}

Expand All @@ -76,11 +129,61 @@ public void Initialise(RectTransform parent)
/// </summary>
/// <param name="input">UIPanel to transition to</param>
public void SetPanel(UIPanel input)
{
if (TryPreparePanel(input))
{
input.PlayTransition(this, input.transition, containsLoopGroup ? loopTransitionSpeed : input.animationSpeed);
transitioning = true;
}
}

public void NextPanel()
{
if (CheckIsValidLoopGroup())
{
loopIdx++;
if (TryPreparePanel(loopGroup[loopIdx]))
{
loopGroup[loopIdx].PlayTransition(this, nextTransition, containsLoopGroup ? loopTransitionSpeed : loopGroup[loopIdx].animationSpeed);
transitioning = true;
}
}
}

public void PreviousPanel()
{
if (CheckIsValidLoopGroup())
{
loopIdx--;
if (TryPreparePanel(loopGroup[loopIdx]))
{
loopGroup[loopIdx].PlayTransition(this, prevTransition, containsLoopGroup ? loopTransitionSpeed : loopGroup[loopIdx].animationSpeed);
transitioning = true;
}
}
}

private bool CheckIsValidLoopGroup()
{
if (!containsLoopGroup)
{
Debug.LogError("Trying to Call NextPanel() on a UIManager that doesn't contain a loop group");
return false;
}
if (loopGroup.Length == 0)
{
Debug.LogError("No panels listed in loop group of " + name);
return false;
}
return true;
}

private bool TryPreparePanel(UIPanel input)
{
if (transitioning)
{
Debug.LogError("Wait for transition to end before setting new panel");
return;
return false;
}
if (currentPanel != null)
{
Expand All @@ -102,8 +205,7 @@ public void SetPanel(UIPanel input)
}
root.GetComponent<MonoBehaviour>().StartCoroutine(WaitForAnimationEnd(input));
input.Initialise((RectTransform)Instantiate(input.panelPrefab, inParent).transform);
input.PlayTransition(this);
transitioning = true;
return true;
}

private IEnumerator WaitForAnimationEnd(UIPanel panel)
Expand Down
12 changes: 6 additions & 6 deletions Assets/UIManager/Scripts/UIPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public void Initialise(RectTransform input)
/// Play set animation transition for this panel
/// </summary>
/// <param name="manager">UIManager handling Animation</param>
public void PlayTransition(UIManager manager)
public void PlayTransition(UIManager manager, UITransition transition_, float speed)
{
manager.animator.speed = 1f / animationSpeed;
manager.animator.Play(transition.inAnimation.name);
manager.root.GetComponent<MonoBehaviour>().StartCoroutine(WaitForTransitionIn(instantiation));
manager.animator.speed = 1f / speed;
manager.animator.Play(transition_.inAnimation.name);
manager.root.GetComponent<MonoBehaviour>().StartCoroutine(WaitForTransitionIn(instantiation, speed));
}

public IEnumerator WaitForTransitionIn(RectTransform parent)
public IEnumerator WaitForTransitionIn(RectTransform parent, float speed)
{
yield return new WaitForSeconds(transition.inAnimation.length * animationSpeed);
yield return new WaitForSeconds(transition.inAnimation.length * speed);
onPanelTransitionedIn.Invoke(parent);
}

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ An initialised Manager can swap between panels via SetPanel(UIPanel)

![](SetPanel-example.png)

UI Managers can be set to have "loop groups" - if so, you will specify here a previous/next transition to override what is specified in the UIPanels listed on the UIManager. Panels will logically transition by calling NextPanel() and PreviousPanel() in the UIManager, the index will reset automatically. A Manager with a loop group will override the speed of the animation transition. That value is set here.

![](UIManagers-example.png)

# UI Panel
----------
**Create > UI > Panel**
Expand Down
Binary file added UIManagers-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3914710

Please sign in to comment.