Search Unity

[RELEASED] RapidIoC for Unity

Discussion in 'Assets and Asset Store' started by chillpillgames, May 24, 2019.

  1. chillpillgames

    chillpillgames

    Joined:
    May 24, 2019
    Posts:
    3


    RapidIoC for Unity is released (FREE) in the asset store.
    What is it
    RapidIoC is a lightweight Inversion of Control/Dependency Injection framework. It helps improve code quality, simplify code maintenance and code extendibility. Additionally, its provides a solid foundation for architecting complex projects.

    How it works

    RapidIoC encapsulates your data into contexts, and lets you access it via property injection within Views. Additionally it provides Signal-Command communication protocol between views.

    To put it simply, it removes the need to keep track of object instances and having to manually assign data to them (and if that data changes dynamically, manually having to update these instances). It keeps your code clean, loosely-coupled, and well encapsulated.

    For example, imagine you need to implement a score GUI. Whenever you kill an enemy, you will earn 10 points. Here is one way you could do it without RapidIoC:

    Code (CSharp):
    1. class ScoreTable : MonoBehaviour
    2. {
    3.     int score = 0; // current score
    4.     public Text scoreText; // UI text element that displays your score
    5.  
    6.     // Call this to update your score
    7.     public void AddScore(int newScore)
    8.     {
    9.         score += newScore;
    10.         scoreText.text = score.ToString();
    11.     }
    12. }
    13.  
    14. public class GameWorld : MonoBehaviour
    15. {
    16.     public ScoreTable scoreTable; // you need to assign this object manually
    17.  
    18.     void KillEnemy()
    19.     {
    20.         scoreTable.AddScore(10);
    21.     }
    22. }
    23.  
    Now this is ok, but what if our score table lifespan is not permanent (e.g. it can be destroyed and re-created throughout a game)? What if we can have more than 1 score tables located randomly within game object hierarchy? What if we have other things that also have to keep track of score as well? For example we may also have a GameState object:

    Code (CSharp):
    1. class GameState : MonoBehaviour
    2. {
    3.     int score = 0;
    4.  
    5.     public void AddScore(int newScore)
    6.     {
    7.         score += newScore;
    8.         if (score >= 100)
    9.             Win();
    10.     }
    11. }
    12.  
    Now we have to manually update references to everything... not fun! :(

    How can we do it with RapidIoC:

    Code (CSharp):
    1. class ScoreTable : ComponentView
    2. {
    3.     int score = 0;
    4.     public Text scoreText;
    5.  
    6.     // we can bind AddScoreSignal to either local or root context
    7.     [Inject]
    8.     public Signal<int> AddScoreSignal { get; set; }
    9.  
    10.     // 'On' prefix is important to automatically connect this function to AddScoreSignal
    11.     public void OnAddScore(int newScore)
    12.     {
    13.         score += newScore;
    14.         scoreText.text = score.ToString();
    15.     }
    16. }
    17.  
    18. class GameState : ComponentView
    19. {
    20.     int score = 0;
    21.  
    22.     [Inject]
    23.     public Signal<int> AddScoreSignal { get; set; }
    24.  
    25.     public void OnAddScore(int newScore)
    26.     {
    27.         score += newScore;
    28.         if (score >= 100)
    29.             Win();
    30.     }
    31. }
    32.  
    33. public class GameWorld : ComponentView
    34. {
    35.  
    36.     [Inject]
    37.     public Signal<int> AddScoreSignal { get; set; }
    38.  
    39.     void KillEnemy()
    40.     {
    41.         AddScoreSignal.Dispatch(10); // we don't need to know who listens to this signal!
    42.     }
    43. }
    44.  
    Much easier, right?! :D This is just one example of how RapidIoC can make your programmer life easier. Check out the examples within the asset (it's free after all) for list of all the cool things it can do!

    RapidIoC comes with 3 well-documented examples (including Unity's Space Shooter tutorial adapted to the framework), and full api documentation here. If you have any issues, suggestions, or questions - feel free to either post in this thread or visit our forums.

    Hopefully you guys find it useful in your next game project! :)
     
    Last edited: May 24, 2019