Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CAFU v3 完全に理解した / Updates for CAFU v3

7b13f432547b92d04c79b749adb535e0?s=47 Tetsuya Mori
October 02, 2018

CAFU v3 完全に理解した / Updates for CAFU v3

2018/10/02 (Tue) に開催した、キッズスター開発合宿 vol.6 での資料です。
CAFU: Clean Architecture for Unity の v3 に関するアップデート情報などを軸に Zenject についても解説しています。

スクリーンキャプチャ動画も撮影しているので以下にリンクします。

- [Changes](https://youtu.be/snavhGhm6cc)
- [Zenject](https://youtu.be/mO2BN6foSMg)
- [処理の流れ](https://youtu.be/B4_wlM3KEXs)
- [v2 と v3 の移行・共存](https://youtu.be/w_RNPdJVJ3w)

7b13f432547b92d04c79b749adb535e0?s=128

Tetsuya Mori

October 02, 2018
Tweet

Transcript

  1. גࣜձࣾΩοζελʔ CTO ৿ɹ఩࠸ CAFU v3 ׬શʹཧղͨ͠ - Zenject Λఴ͑ͯ -

  2. ͜Μʹͪ͸ʂ 2

  3. w$"'6Wʹ͍ͭͯ w;FOKFDUʹ͍ͭͯ wॲཧͷྲྀΕΛ௥͏ wWˠWͷҠߦɾڞଘ 3 ͓͠ͳ͕͖

  4. CAFU v3 ʹ͍ͭͯ 4

  5. w׬શͳΔ$MFBO"SDIJUFDUVSFͷ࣮ݱ w $MFBO"SDIJUFDUVSFୡਓʹֶͿιϑτ΢ΣΞͷߏ଄ͱઃܭ
 3PCFSU$.BSUJO ֯੐య ߴ໦ਖ਼߂cຊc௨ൢ
 5 ίϯηϓτ About CAFU

    v3 > Changes
  6. w1SFTFOUBUJPOϨΠϠͷґଘͷ޲͖Λ ٯస wਖ਼͍͠%*1ͷ࣮ݱ w&OUJUZͱ%BUB4USVDUVSFͷ෼཭ w.PEFMϨΠϠͷഇࢭ 6 Changes from v2 to

    v3 About CAFU v3
  7. 7 Presentation ͷґଘํ޲ٯస About CAFU v3 > Changes

  8. 8 Before About CAFU v3 > Changes > Dependency Inversion

    for Presentation
  9. 9 After About CAFU v3 > Changes > Dependency Inversion

    for Presentation
  10. w্ҐͷϨΠϠଆʹJOUFSGBDFΛ࡞੒ w͜ΕͰϨΠϠΛލ͍ͩґଘΛճආ wԼҐϨΠϠ͸্ҐϨΠϠͷJOUFSGBDF Λ࣮૷ w6.-తͳݴ༿Ͱݴ͑͹࣮ݱ 10 ਖ਼͍͠ DIP ͷ࣮ݱ About

    CAFU v3 > Changes > Dependency Inversion for Presentation
  11. w&OUJUZΛ࠷্ҐϨΠϠͱͯ͠࠶ఆٛ w੹຿ʹด࣮ͨ͡૷ w੹຿ʹԙ͚Δঢ়ଶ w%BUB4USVDUVSFͱͯ͠ɺ֤຤୺ϨΠϠͰ༻͍Δσʔλߏ଄Λ ఆٛ wͦΕͧΕ5SBOTMBUPSΛ௨ͯ͡&OUJUZ͔Β৘ใΛ΋Β͏ w%BUB4USVDUVSF͔ΒಡΈग़ͨ͠৘ใΛ&OUJUZʹ໭͢ͷ΋ 5SBOTMBUPSͷ੹຿ 11 Entity

    ͱ Data Structure ͷ෼཭ About CAFU v3 > Changes
  12. wOBNFTQBDFϨϕϧͰ෼͚Δ͔Ͳ͏͔ ͸ࣗ͝༝ʹ 12 Entity ͱ Data Structure ͷ෼཭ About CAFU

    v3 > Changes > Split Entity and DataStructure W 㱺 W .PEFM 㱺 %BUB4USVDUVSF
 GPS1SFTFOUBUJPO-BZFS &OUJUZ 㱺 %BUB4USVDUVSF
 GPS%BUB-BZFS
  13. wઌड़ͷ௨Γɺ.PEFMϨΠϠΛ
 %BUB4USVDUVSFͱͯ͠࠶ఆٛ 13 Model ϨΠϠͷഇࢭ About CAFU v3 > Changes

    > Split Entity and DataStructure
  14. Zenject ʹ͍ͭͯ 14

  15. 15 Zenject ͱ͸ About Zenject w%*'SBNFXPSL w*OTUBMMFSʹܕͷੜ੒ํ๏Λఆٛ ʢ#JOEʣ w֤ΫϥεͰ͸஫ೖʢ*OKFDUʣͯ͠ཉ͠ ͍ܕΛએݴ

  16. w;FOKFDU͸͡ΊͷҰา w;FOKFDU׬શʹཧղͨ͠6OJUZઋਓ w;FOKFDUͰષͷ৺ΛखʹೖΕΔ w͔ͤϊʔτ͔ͤ wͦͷଞ 16 ࢿྉ About Zenject

  17. ॲཧͷྲྀΕ 17

  18. wIUUQTHJUIVCDPNNPOSZDBGV@TBNQMF wHJUQVMMZBSO͓͍͍ͯͯͩ͘͠͞ 18 લஔ͖ Process flow

  19. w*OTUBMMFSʹͯ;FOKFDU**OJUJBMJ[BCMFΛ࣮૷ͨ͠ 6TF$BTFΛ#JOE w6TF$BTF͔ΒҰ࿈ͷґଘΫϥεୡ͕*OKFDU͞Ε͍ͯ͘ w*OJUJBMJ[F ಺Ͱ֤छॳظԽΛߦ͏ wྫ(BNF4UBUF6TF$BTFͷ**OJUJBMJ[BCMF*OJUJBMJ[F  ಺Ͱ։࢝༻4VCKFDUʹ0O/FYU  w6TF$BTF͕ঢ়ଶΛ؅ཧ͢Δ&OUJUZΛૢ࡞͢Δ

    19 γʔϯॳظԽ Process flow
  20. w*OTUBMMFSʹ4FSJBMJ[F'JFME࡞ͬͯɺͦ ΕΛͦͷ··'SPN*OTUBODF ʹ౉͢ 20 γʔϯ্ͷཁૉΛ Bind ηΫγϣϯλΠτϧ [SerializeField] private ButtonStart

    buttonStart; private ButtonStart ButtonStart => buttonStart; Container.Bind<ITrigger>() .WithId(Constant.InjectId.ButtonStart) .FromInstance(ButtonStart) .AsCached();
  21. wϘλϯͱ͔ wDBGV@TBNQMFͰ͸ɺ໘౗ͩͬͨͷͰઐ༻ ͷJOUFSGBDF*5SJHHFSΛ࡞ͬͨ wݸผʹܕΛ༻ҙ͢Δ͔#JOE 8JUI*E Ͱ ΍Δ͔͸೰·͍͠ͱ͜Ζʜɻ wԿ͔ྑ͍ΞΠσΟΞ͋Γ·͔͢Ͷʁ 21 γʔϯ্ͷཁૉΛ

    Bind Process flow
  22. 22 Prefab ͔Βͷ Spawn Process flow [SerializeField] private Mole molePrefab;

    private Mole MolePrefab => molePrefab; [SerializeField] private Transform moleParent; private Transform MoleParent => moleParent; public override void InstallBindings() { Container // IMoleView の Factory を登録 .BindIFactory<int, IMole, IMoleView>() // Mole 型として .To<Mole>() // MolePrefab 内のコンポーネントを用いて .FromComponentInNewPrefab(MolePrefab) // MoleParent の配下に .UnderTransform(MoleParent);
 }
  23. 23 Prefab ͔Βͷ Spawn Process flow [Inject] private IFactory<int, IMole,

    IMoleView> MoleViewFactory { get; } private List<IMoleView> MoleViewList { get; } = new List<IMoleView>(); public void Instantiate(int index, IMole mole) { var moleView = MoleViewFactory.Create(index, mole); MoleViewList.Add(moleView); } w͜ͷྫͷ৔߹ɺ1SFTFOUFSͰ4QBXO ͍ͯ͠Δײ͡
  24. 24 Prefab ͔Βͷ Spawn Process flow wύϥϝʔλ౉ͯ͠ॳظԽ͍ͨ͠৔߹ʹ 'BDUPSZ࢖͏ wγϯϓϧʹ*OTUBOUJBUF͢Δ͚ͩͳΒ 'BDUPSZφγͰ΋0,

    w6OEFS5SBOTGPSN Ͱ਌΋ࢦఆՄೳ
  25. 25 ը໘ભҠ Process flow w$"'6WUJNFT;FOKFDUͷͨΊͷը໘ભҠઐ ༻ͷVNNΛ࣮૷ࡁ w!VNNDBGV@TDFOF wݱࡏ"TTFU#VOEMFඇରԠ wͦΜͳʹֻ͚࣌ؒͣʹରԠͰ͖Δʜͱʜࢥʜʜ
 ʢ͜͜Ͱϝοηʔδ͸్੾Ε͍ͯΔʣ

  26. 26 ը໘ભҠ Process flow w!VNNDBGV@TDFOFͷػೳͱͯ͠
 ґଘγʔϯͷࣗಈಡࠐΛ࣮૷ࡁ w&EJUPSͰࣗಈతʹ1BSFOU$POUSBDUT ΛಡΈࠐΉͱ͔΋ରԠࡁ w4DFOF4USBUFHZ-JTUBTTFUʹ
 ґଘؔ܎Λఆٛ

  27. w͜Ε·Ͱ͸֤γʔϯʹ6*༻ͷΧϝϥΛ༻ҙ͍ͯͨ͠ ͕ɺඳըॱͷ໰୊Λճආ͢ΔͨΊͷڞ௨γʔϯΛ࡞੒ w!VNN[FOKFDU@VJTDFOF w6*$BNFSB$PPSEJOBUPSίϯϙʔωϯτΛ$BOWBT ʹ෇͚Ε͹ɺϤγφʹ3FOEFS$BNFSBΛมߋ͢Δ w;FOKFDUͷ1BSFOU$POUSBDUͱͯ͠6*Λઃఆ͢ Δඞཁ͋Γ 27 UI γʔϯ

    ηΫγϣϯλΠτϧ
  28. 28 UI γʔϯ ηΫγϣϯλΠτϧ [SerializeField] private SceneStrategyList sceneStrategyList; private ISceneStrategyList

    SceneStrategyList => sceneStrategyList; public override void InstallBindings() { // シーン読み込みに関する処理群を Install // Generics ナシの SceneInstall の場合 SimpleLoaderUseCase が Bind される // PreLoad/PostUnload の設定を流し込みたいので StrategicLoaderUseCase を利用 SceneInstaller<StrategicLoaderUseCase>.Install(Container); Container.QueueForInject(SceneStrategyList); // シーン読み込み戦略データを Bind // PreLoad/PostUnload の設定を仕込む Container .Bind<IDictionary<string, ISceneStrategy>>() .WithId(CAFU.Scene.Application.Constant.InjectId.UseCase.SceneStrategyMap) .FromInstance(SceneStrategyList.AsDictionary()); }
  29. 29 σʔλಡΈॻ͖ Process flow w΋ͷੌ͘γϯϓϧͳσʔλಡΈॻ͖༻ͷVNNΛ࣮૷ࡁ w!VNNDBGV@EBUB w*&OVNFSBCMFCZUFΛΰχϣΰχϣ͢Δ܅ wݱࡏɺϩʔΧϧετϨʔδͱͷର࿩ͷΈ w*$SFBUPSͱ͔*8SJUFSͱ͔ͷ$36%38ͳJOUFSGBDFΛ
 ੾ͬͯ͋ΔͷͰɺ͜ͷVNNΛ֦ு͍͚ͯ͠͹ׂͱࣗ༝ʹ


    σʔλૢ࡞͕Ͱ͖ΔͷͰ͸
  30. 30 σʔλಡΈॻ͖ Process flow w!VNNDBGV@EBUBΛ࢖͏ͱ͖͸ɺ *OTUBMMFSͰҎԼΛ#JOE wͲͷ3FQPTJUPSZΛ࢖͏ͷ͔ʁ wͲͷ%BUB4UPSFΛ࢖͏ͷ͔ʁ

  31. 31 σʔλಡΈॻ͖ Process flow // Repositories Container .BindInterfacesTo<AsyncCRUDRepository>() .AsCached(); //

    DataStores Container .BindInterfacesTo<AsyncLocalStorageDataStore>() .AsCached();
  32. 32 σʔλಡΈॻ͖ Process flow [Inject(Id = Constant.InjectId.ResultListFileUri)] private Uri ResultListFileUri

    { get; } [Inject] private IAsyncCRUDHandler AsyncCRUDHandler { get; } private async void Read() { var bytes = await AsyncCRUDHandler.ReadAsync(ResultListFileUri); RankingEntitySubject.OnNext( ResultListEntityTranslator.Translate( bytes.FromByteArray<DataResultList>() ) ); RankingEntitySubject.OnCompleted(); } private async void Write() { // 念のため ResultListEntity の生成準備を待つ await RankingEntitySubject; await AsyncCRUDHandler.UpdateAsync( ResultListFileUri, DataResultListStructureTranslator.Translate( RankingEntitySubject.Value ).ToByteArray() ); }
  33. v2 → v3 ͷҠߦɾڞଘ 33

  34. 34 v2 → v3 ͷҠߦ Migration & Coexistence w!VNNDBGV@DPSFͷ.BKPS7FSTJPO Λʹͯ͠ZBSOJOTUBMM

    wར༻͢Δ"TTFNCMZ%FpOJUJPO'JMFT ΛVNN!DBGV@DPSF!Wʹ͢Δ
  35. 35 v2 ɾ v3 ͷڞଘ Migration & Coexistence w"TTFNCMZ%FpOJUJPO'JMFTΛ෼͚ͯ ͋ΔͷͰ҆৺҆શ

    wಉ࣌ʹ࢖͏৔߹JOUFSGBDFͷީิ͕
 ྆ํͷόʔδϣϯͷιϨ͕ग़ͯ͘Δ
 ͷͰ*%&্ͰΠϥοͱ͢Δ͔΋
  36. Thank you for your listening !! 36