1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

[RELEASED] IoC+, Inversion of Control for Unity

Discussion in 'Assets and Asset Store' started by softlion, Oct 2, 2016.

  1. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    76
    Hi again mrcoffee666!

    IoC+ does not work with constructor/method injection. IoC+ injects into fields of Commands and Mediators with the [Inject] attribute directly using Reflection. Only Commands and Mediators have injectable fields to prevent cross dependency in services and models. Your example could work like this:
    Code (CSharp):
    1. public class GameView : View { }
    2. public class GameMediator : Mediator<GameView> {
    3.  
    4.     [Inject] private IUserService userSettings;
    5.  
    6.     public override void Initialize() {
    7.         Debug.Log(userSettings.GetName());
    8.     }
    9.  
    10. }
    11.  
    12. public interface IUserService {
    13.     string GetName();
    14. }
    15.  
    16. public class LocalUserService : IUserService {
    17.     public string GetName() {
    18.         return "xzy";
    19.     }
    20. }
    21.  
    22. public class DatabaseUserService : IUserService {
    23.     public string GetName() {
    24.         return "sql";
    25.     }
    26. }
    27.  
    28. public class GameContext {
    29.  
    30.     protected override void SetBindings(){
    31.         base.SetBindings();
    32.      
    33.         Bind<IUserService, LocalUserService>();
    34.      
    35.         BindMediator<GameMediator, GameView>();
    36.      
    37.         On<EnterContextSignal>()
    38.             .InstantiateView<GameView>();
    39.      
    40.     }
    41.  
    42. }
    Let me know if this helps. The get a more basic understanding of IoC+, don't forget to go through the tutorials on the IoC+ website!
     
  2. mrcoffee666

    mrcoffee666

    Joined:
    Jun 21, 2013
    Posts:
    4
    Ok now I understand the reason of this limitation much better thanks. Before evaluating your framework I tested another IOC container and there it was also possible to inject constructor parameters of any class. I have to think a little different now to use your framework correctly.
     
  3. B4blue

    B4blue

    Joined:
    Oct 2, 2013
    Posts:
    1
    Any other way of buying this? I can't pay with paypal as I currently don't have any credit card, just paypal money. Stupid paypal...
     
  4. TokyoDan

    TokyoDan

    Joined:
    Jun 16, 2012
    Posts:
    999
    Hi softlion. How are things going?

    I've been away from Unity and game dev for a while but now I am about to start work on a new game idea and was just reviewing IoC+. And even my simplified example that we previously discussed (without Mediators) was hard to wrap my mind around after my months away.

    But I guess being away gave me more clarity and I found a way to simplify it even more:

    I got rid of Commands completely by changing the injected JumpCommandSignal and MoveCommandSignal in the PlayerInputView (that Dispatch the JumpSignal and the MoveSignal to the PlayerView) to injected JumpSignal and MoveSignal. And then in the Update() method of PlayerInputView where it reads the keyboard arrow keys to perform jumps and moves, I changed the jumpCommandSignal.Dispatch() and moveCommandSignal.Dispatch() calls respectively to corresponding jumpSignal.Dispatch() and moveSignal.Dispatch() calls. This works perfectly and made things 100% more clear.

    Why are intermediary Commands necessary?
     
  5. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    76
    Hi again!

    Commands are there so the functionality of your game can be configured via the Contexts. When mediator/view signals are decoupled correctly, then the Contexts allow you to wire the events (signals) with actions (commands).
     
  6. shaunmitchell84

    shaunmitchell84

    Joined:
    Dec 17, 2013
    Posts:
    3
    Hi,

    What would be the best way to handle signals that more then one object could listen to? For example a switch that sends a signal to a door? Every door will probably be listening for a signal but I need to only do something if it's the correct one.

    I thought about ids that could be sent along with the signal but that might lead to performance issues instead of a direct reference.

    Any thoughts would be great.

    Great tool btw

    Thanks,
    Shaun
     
  7. softlion

    softlion

    Joined:
    Sep 6, 2013
    Posts:
    76
    Hi Shaun!

    That would depend on how a door should know that the switch is connected to that specific door. In some of the projects that use IoC+ at work we also use a trigger/actor system. We also use strings to connect an actor to a trigger. So for example a SwitchTriggerView, that has the "FrontDoorButton" id and a DoorActorView with the same id. The ActorView can then figure out whether it should respond to the TriggerSignal. You'd need a ridiculous amount of actors before this becomes a performance issue.

    I hope that helps!