Skip to content

boscohyun/RxPresenter

Repository files navigation

RxPresenter

openupm

RxPresenter implement Reactive Presenter of MV(R)P pattern that can be used simply and usefully in Unity.

Dependency

How to Install

Install via OpenUPM(recommended)

$ openupm add com.boscohyun.rxpresenter

Install via Manifest

{
  "dependencies": {
    "com.boscohyun.rxpresenter": "https://github.com/boscohyun/RxPresenter.git?path=Assets/Plugins/RxPresenter",
    "com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask",
    "com.neuecc.unirx": "https://github.com/neuecc/UniRx.git?path=Assets/Plugins/UniRx/Scripts"
  }
}

How to Use

Control Presenter or ReactivePresenter

public class PresenterController : MonoBehaviour
{
    [SerializedField] Presenter presenter;
    
    // NOTE: ReactivePresenter just implement IReactivePresenter<T> based on Presenter with Presenter.Humble
    [SerializedField] ReactivePresenter reactivePresenter;
    
    public void Awake()
    {
        presenter.Humble.OnPresenterStateChange.Subscribe().AddTo(gameObject);
        presenter.Humble.OnShowAnimationBeginning.Subscribe().AddTo(gameObject);
        presenter.Humble.OnShowAnimationEnd.Subscribe().AddTo(gameObject);
        presenter.Humble.OnHideAnimationBeginning.Subscribe().AddTo(gameObject);
        presenter.Humble.OnHideAnimationEnd.Subscribe().AddTo(gameObject);
        
        reactivePresenter.OnPresenterStateChange.Subscribe().AddTo(gameObject);
        reactivePresenter.OnShowAnimationBeginning.Subscribe().AddTo(gameObject);
        reactivePresenter.OnShowAnimationEnd.Subscribe().AddTo(gameObject);
        reactivePresenter.OnHideAnimationBeginning.Subscribe().AddTo(gameObject);
        reactivePresenter.OnHideAnimationEnd.Subscribe().AddTo(gameObject);
    }
    
    public async void ShowPresenters()
    {
        // Just show
        presenter.Show();
        reactivePresenter.Show();
        
        // Show with callback
        presenter.Show(p => { }); // p: presenter
        reactivePresenter.Show(rp => { }); // rp: reactivePresenter
        
        // Show immediately whithout any animations
        presenter.Humble.Show(true, p => { }); // or (true, null);
        reactivePresenter.Show(true, rp => { }); // or (true, null);
        
        // Show as observable
        presenter.Humble.ShowAsObservable() // or (true) if you want show immediately
            .First()
            .Subscribe(p => { });
        reactivePresenter.ShowAsObservable() // or (true)  if you want show immediately
            .First()
            .Subscribe(rp => { });
        
        // Await showing task
        await presenter.Humble.ShowAsync();
        await reactivePresenter.ShowAsync();
    }
    
    public void HidePresenters()
    {
        // Just hide
        presenter.Hide();
        reactivePresenter.Hide();
        
        // Hide with callback
        presenter.Humble.Hide(p => { }); // p: presenter
        reactivePresenter.Hide(rp => { }); // rp: reactivePresenter
        
        // Hide immediately whithout any animations
        presenter.Humble.Hide(true, p => { }); // or (true, null);
        reactivePresenter.Hide(true, rp => { }); // or (true, null);
        
        // Hide as observable
        presenter.Humble.HideAsObservable() // or (true) if you want show immediately
            .First()
            .Subscribe(p => { });
        reactivePresenter.HideAsObservable() // or (true)  if you want show immediately
            .First()
            .Subscribe(rp => { });
        
        // Await hiding task
        await presenter.Humble.HideAsync();
        await reactivePresenter.HideAsync();
    }
}

Custom ViewAnimator and Presenter or ReactivePresenter

You can custom your own ViewAnimators and Presenters like below.

// Skeleton code example
[Serializable]
public class CustomViewAnimator : IViewAnimator
{
    public int AnimatorActiveDelayFrame { get; }
    
    public bool AnimatorAlwaysActive { get; }
    
    public ViewAnimatorState CurrentAnimatorState { get; }
    
    public float CurrentAnimatorStateNormalizedTime { get; }

    public void PlayAnimation(ViewAnimatorState viewAnimatorState, float normalizedTime) { }
    
    public void SetActive(bool active) { }
}

public class CustomPresenter : Presenter
// or public class CustomPresenter : ReactivePresenter<CustomPresenter>
// or public class CustomPresenter<T> : ReactivePresenter<T> where T : CustomPresenter<T>
{
    [SerializeField] private CustomViewAnimator viewAnimator;

    public override bool HasViewAnimator => /* Check viewAnimator */;

    public override IViewAnimator ViewAnimator => viewAnimator;
}

External Supports

About

A simple reactive presenter package for Unity.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages