Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
for 4.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed May 26, 2015
1 parent e4cf579 commit fc0b7e6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Assets/UniRx/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UniRx - Reactive Extensions for Unity / ver.4.8
UniRx - Reactive Extensions for Unity / ver.4.8.1
===
Created by Yoshifumi Kawai(neuecc)

Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,68 @@ eventTrigger.OnBeginDragAsObservable()
.Subscribe(x => Debug.Log(x));
```

PresenterBase
---
UI has hierarchy and maybe contains a few presenters. But Unity's script execution order is indeterminate in default, so you can't touch child presenter's property before child has been initialized. And sometimes ReactiveProperty requires initial value but Unity doesn't have constructor. `PresenterBase` solves there two problems.

* Resolve initialize dependency of multiple presenters chain
* Passing initial argument like constructor

```csharp
// If Presenter receive argument inherit PresenterBase<T> otherwise inherit PresenterBase
public class CharacterPresenter : PresenterBase<int>
{
// attach from inspector
public WeaponPresenter WeaponPresenter;
public StatusPresenter StatusPresenter;

// model field
private Character character;

// indicate children dependency
protected override IPresenter[] Children
{
get
{
// If children is empty, you can write `return EmptyChildren;`
return new IPresenter[] { WeaponPresenter, StatusPresenter };
}
}

// This Phase is Parent -> Child
// You can pass argument to children, but you can't touch child's property
protected override void BeforeInitialize(int argument)
{
var characterId = argument;
character = new Character(characterId); // set up character...
// Pass argument to children, call PropagateArgument method
WeaponPresenter.PropagateArgument(character.Weapon);
StatusPresenter.PropagateArgument(character.Status);
}

// This Phase is Child -> Parent
// You can touch child's property safety
protected override void Initialize(int argument)
{
StatusPresenter.StatusChanged.Subscribe(x =>
{
WeaponPresenter.Weapon.Power.Fix(x.power);
});
}
}
```

PresenterBase has three phases.

1. In Awake - Resolve parent-child dependency using Children proeperty.
2. In Start - Perent to Children, propagete value phase.
3. In Start - Children to Parent, initialize phase.

![](StoreDocument/presenterbase_steps.gif)

Yellow is `Awake`, order is indeterminate. Green is `BeforeInitialize` phase, its parent -> child. Red is `Initialize` phase, its child -> parent. This sample, you can see `Sample14_PresenterBase`.

Visual Studio Analyzer
---
For Visual Studio 2015 users, a custom analyzer, UniRxAnalyzer, is provided. It can, for example, detect when streams aren't subscribed to.
Expand Down
Binary file added StoreDocument/presenterbase_steps.gif
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 fc0b7e6

Please sign in to comment.