ã¯ããã«
ã©ãããåéã§ãã
ãã®è¨äºã¯Iwaken Lab.ã®éçºå宿ããã°ãªã¬ã¼11æ¥ç®ã®è¨äºã§ãã
9æã«è¡ãããéçºå宿ã§åãã¦è§¦ã£ãExtenjectã«ã¤ãã¦æ¸ãã¾ããï¼
- ã¯ããã«
- Extenjectã¨ã¯ï¼
- ççµåãªè¨è¨
- Extenjectã®å©ç¨
- å°å ¥æ¹æ³
- ä»åã®å®è£
- ã¾ã¨ã
- åè
Extenjectã¨ã¯ï¼
DI(ä¾åæ§ã®æ³¨å ¥)ãã¬ã¼ã ã¯ã¼ã¯ã§ããã¨ãã¨Zenjectã¨ããååã§ããããéçºè ã®æ¹ãä¼ç¤¾ã¨ããããããã£ã¦ååãå¤ãã£ãããã§ãã å½¹å²ã¨ãã¦ã¯ããççµåããã¹ãã®ãã¨ãèãã¦è¨è¨ããã¨ãã«çºçãã¦ãã¾ãåé¡ã解決ãã¦ããããã¨ãã£ãæãã§ãã
Extenjectã使ãã«ã¯ãççµåãªè¨è¨ã«ãªã£ã¦ããªãã¨ãã®å¹åãçºæ®ããªãã
ççµåãªè¨è¨
ããã§ã¾ããççµåãªè¨è¨ã«ã¤ãã¦è¦ã¦ããã¾ãã ä¾ãã°ãè¤æ°ããã¤ã¹ã®å ¥åã«å¯¾å¿ããåãæ¿ããè¡ãå ´åãã©ããã¦ãã³ã¼ãããã¡ãã£ã¨ãããããããã°ã大å¤ã ã£ãããã¾ãã
â ããã§Interfaceçã使ã£ã¦ççµåã«ãã¦ããï¼ï¼
ä¾ã¨ãã¦VRã¨ãã¼ãã¼ãã§å ¥ååãæ¿ãããã¨ãèãã¦è¨è¨ãã¦ããã¾ãããã
ã¾ããããå¯çµåã®ç¶æ ã§ãã
ããã§ã¯æä½æ¹æ³ãå¢ãããã³ã«PlayerMove
ãæ¸ãæããå¿
è¦ãåºã¦ãã¦ãã¾ãã¾ããããã§ã¯æéã§ããããã¼ã éçºã«ããã¦ã¯æ§ã
ãªåé¡ã«ç¹ããããã¾ããã
ãããinterfaceã使ããã¨ã§ãççµåã«ãã¦ããã¾ãã
ããã«ããPlayerMove
ã¯æ¸ãæãããã¨ãªããæ§ã
ãªæä½æ¹æ³ã«å¯¾å¿ã§ãã¾ãã
å ·ä½çãªã¹ã¯ãªãã
ãã®ä¾ã§ã¯ã¹ã«ã¤ãã¤ãã³ã°ã²ã¼ã ãæ³å®ãã¦æ¸ãã¾ããã
PlayerMove
Playerã®åãç¨ã¹ã¯ãªãã
public class PlayerMove : MonoBehaviour { [SerializeField] private float moveSpeed = 2; [SerializeField] private float fallSpeedMagnification = 3; private IInputProvider _inputProvider; private Rigidbody _rigidbody; private void Start() { _rigidbody = GetComponent<Rigidbody>(); } private void Update() { _rigidbody.AddRelativeForce(_inputProvider.GetMove() * moveSpeed); _rigidbody.drag = _inputProvider.FallSpeed() * fallSpeedMagnification; } }
ãã¾ã¼ããã¨ççµåã«ãã¦ããã¨Inputãå¢ãã¦ããããæ¸ãæããå¿ è¦ã¯ãªãã
IInputProvider
ä»åä½ã£ãInputç¨ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã使ç¨ããåããå®ç¾©ã
public interface IInputProvider { Vector2 GetMove(); float FallSpeed(); }
VRInputProvider
IInputProviderãå®è£ ãã¦ãã¾ãã
public class VRInputProvider : IInputProvider { public Vector2 GetMove() { å¦ç } public float FallSpeed() { å¦ç } }
KeyboardInputProvider
ãVRInputProvider
åæ§ã«å®è£
ããã
Extenjectã®å©ç¨
ãã ãççµåã«ããã¨ã¯ã©ã¹ã®å©ç¨è çã«ã¯ä¸ä¾¿ãªå ´é¢ãå¤ãã§ãã
ä¾ãã° ãå©ç¨ããã¤ã³ã¹ã¿ã³ã¹ã®æ±ºå®ãã©ããã£ã¦è¡ãã®ãï¼ã ãã©ã®ã¿ã¤ãã³ã°ã§å¼ã³åºãã®ãï¼ã ã¨ãã£ãåé¡ãçºçãã¾ãã
ããã§Extenjectã§ä¾åæ§ãæ³¨å ¥ããï¼ï¼
å°å ¥æ¹æ³
å¾æ¥ã®ã¢ã»ããéããAssetStoreããã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
ä»åã®å®è£
主ãªæ§æã¯ãããªæãã
1. Installerãæ¸ãã¦ãã
DI Container1ã«å¯¾ãã¦ãIInputProvider
ãè¦æ±ãããããVRInputProvider
ã注å
¥ãããã¨ãããã¨ãè¨å®ããVRInputInstaller
ãæ¸ãã
VRInputInstaller
public class VRInputInstaller : Installer<VRInputInstaller> { public override void InstallBindings() { Container .Bind<IInputProvider>() .To<VRInputProvider>() .AsCached(); } }
KeyboardInputInstaller
åæ§ã«ãã¦KeyboardInputInstaller
ãè¨è¿°ã
public class KeyboardInputInstaller : Installer<KeyboardInputInstaller> { public override void InstallBindings() { Container .Bind<IInputProvider>() .To<KeyboardInputProvider>() .AsCached(); } }
InputManager
ããã«VRããã¼ãã¼ãããå¤å®ããInputManager
ãæ¸ãã
public class InputManager : MonoInstaller { ~~ä¸ç¥~~ public override void InstallBindings() { if (XRGeneralSettings.Instance && XRGeneralSettings.Instance.Manager.activeLoader != null) { Debug.Log("VR Mode"); VRInputInstaller.Install(Container); } else { Debug.Log("Keyboard Mode"); KeyboardInputInstaller.Install(Container); } } }
2. Contextãè¨å®ãã
é
ç½®ãããã·ã¼ã³ã«ã®ã¿Installerã®å½±é¿ç¯å²ãè¨å®ããScene Context
ãã·ã¼ã³ã«é
ç½®ããã
Scene Contextã«InputManagerãç»é²ãã¾ãã
3. DI Containerããªãã¸ã§ã¯ããæ³¨å ¥ããå ãè¨å®
å¤æ°ã«[Inject]
å±æ§ãã¤ãããã¨ã§Containerããªãã¸ã§ã¯ãã注å
¥ãã¦ããã¾ãã
ããã¼ç°¡åã§ãã...
ä»åã§ããã°ãPlayerMove
ã®IInputProvider
ã®å®£è¨ã®é¨åã«[Inject]
å±æ§ã追è¨ã
[Inject]
private IInputProvider _inputProvider;
ããã§å®è¡æã«IInputProvider
ã«VR or ãã¼ãã¼ãã®InputProviderã注å
¥ããã¾ãã
ã¾ã¨ã
ä»ã¾ã§ã¾ã¨ãã«è¨è¨ãããã¨ããªãã£ãã®ã§ãã(è¨è¨ãã¦ãçµå±ãã¾ããããªãã£ã)ãExtenjectãå¦ã¶ã«ãããã¤ã³ã¿ã¼ãã§ã¤ã¹å¨ãã®åå¼·ã«ããªãã¾ããã
UniRx/UniTaskçã¨çµã¿åããããã¨ã§ãããã«ãã¼ã éçºã«ããã¦ããªãå¹åãçºæ®ãããã§ããã¼ãä»åº¦ã¯ããã辺åãçµãã§è¨äºã«ãããããªã¨æãã¾ãã
åè
以ä¸ã®è¨äºãåèã«ããã¦ããã ãã¾ããï¼ãããã¨ããããã¾ããï¼
Zenject入門その1 疎結合とDI Container - Qiita
【Unity(C#)】Extenject(Zenject)使って入力機能を切り離したものを管理してみた - Qiita
-
DI Containerã¯ç°¡åã«è¨ãã°ãç°å¢ã«ãããããä¾åé¢ä¿ãè¨å®ãã¦ããã¨ãããã«å¿ãã¦èªåçã«ã¤ã³ã¹ã¿ã³ã¹ãè¨å®ãã¦ã¾ãã£ã¦ããã便å©ãªåå¨ãã§ããZenject入門その1 疎結合とDI Container - Qiitaããå¼ç¨↩