Skip to content

View Controllers

Daniel Colina edited this page Jul 15, 2020 · 7 revisions

Overview

View controllers are specialized entity controllers that control a UIView component.

The default implementation of a view controller is AViewController<TViewProperties>. It inherits from AEntityController<TEntityProperties>, and as such maintains all behaviour detailed in entity controllers.

AViewController<TViewProperties> exposes a few methods that sub-classes can override:

  • OnShowStarted(): Handler for behaviour when the controlled UIView has started its show animation.
  • OnShowFinished(): Handler for behaviour when the controlled UIView has finished its show animation.
  • OnHideStarted(): Handler for behaviour when the controlled UIView has started its hide animation.
  • OnHideFinished(): Handler for behaviour when the controlled UIView has finished its hide animation.

These methods are called whenever the controlled UIView component's DOTween animations start and finish, which can cause unexpected behaviour when using the Progressor, Progress Target Animator, and Unity animator to do more elaborate UI animations.

Examples

For an example, see the ExampleViewController below:

Example View Controller

[System.Serializable]
public class ExampleViewProperties
{
    public string title;
    public string description;
    public ExampleViewProperties(string title, string description)
    {
        this.title = title;
        this.description = description;
    }
}

public class ExampleViewController : AViewController<ExampleViewProperties>
{
    [SerializeField] private TextMeshProUGUI titleLabel;
    [SerializeField] private TextMeshProUGUI descriptionLabel;

    protected override void OnPropertiesSet()
    {
        // Initialize components based on properties here.
        base.OnPropertiesSet();
        titleLabel.text = Properties.title;
        descriptionLabel.text = Properties.description;
    }

    protected override void OnShowStarted()
    {
        base.OnShowStarted();
        Debug.Log($"My name is {gameObject.name} and I started showing!");
    }

    protected override void OnShowFinished()
    {
        base.OnShowFinished();
        Debug.Log($"My name is {gameObject.name} and I finished showing!");
    }

    protected override void OnHideStarted()
    {
        base.OnHideStarted();
        Debug.Log($"My name is {gameObject.name} and I started hiding!");
    }

    protected override void OnHideFinished()
    {
        base.OnHideFinished();
        Debug.Log($"My name is {gameObject.name} and I finished hiding!");
    }
}