Service Locator: Inversion of Control in Unity C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 มิ.ย. 2024
  • Often considered an anti-pattern, the Service Locator offers a unique approach to managing dependencies in software design, particularly within the context of game development in Unity. This video dives into the construction of a Service Locator, demonstrating how it can operate efficiently across different scopes - at the level of individual game objects, entire scenes, and globally across the entire application. We will explore its relationship with principles like Dependency Inversion / Inversion of Control.
    By the end of this tutorial, you’ll understand how to implement a Service Locator that efficiently manages service lifecycles and dependencies, providing a centralized point for service lookup without tightly coupling objects to their dependencies. Whether you're building a small indie game or a large-scale project, this guide will illuminate how a Service Locator can provide a flexible alternative to other dependency management strategies.
    🔔 Subscribe for more Unity Tutorials / @git-amend
    #unity3d #gamedev #indiedev
    ▬ Contents of this video ▬▬▬▬▬▬▬▬▬▬
    0:00 Overview
    02:40 Service Manager
    04:36 Service Locator
    13:25 Using a Service Locator
    Source code: github.com/adammyhre/Unity-Se...
    Assets Shown In This Video (Affiliate Links)
    Dungeon Mason Tiny Hero Duo: (FREE): assetstore.unity.com/packages...
    Chromisu: Handpainted Forest MEGA Pack assetstore.unity.com/packages...
    SineVFX: Better Crystals assetstore.unity.com/packages...
    VFX Trees: assetstore.unity.com/packages...
    Kronnect Beautify: assetstore.unity.com/packages...
    Dice Creator Pack: assetstore.unity.com/packages...
    Toony Colors Pro 2: assetstore.unity.com/packages...
    Gabriel Aguiar VFX: assetstore.unity.com/packages...
    REXARD SpellBook Icons Megapack: assetstore.unity.com/packages...
    ARCEY Vampire Skill Icons: assetstore.unity.com/packages...
    Follow me!
    linktr.ee/gitamend
  • เกม

ความคิดเห็น • 81

  • @git-amend
    @git-amend  7 หลายเดือนก่อน +4

    Hi everyone! I hope you find this dive into the Service Locator pattern interesting! There will be a follow up video to this one to explore Dependency Injection and further explore the topic of Inversion of Control in a few weeks - suggestions, comments and questions welcome!
    I plan to add a bit more functionality to this Service Locator implementation later this week, which might not make it into a video - keep an eye on the repository if you are interested in that!

    • @stevehamilton321
      @stevehamilton321 7 หลายเดือนก่อน +1

      Man.... it's like you're reading my mind with the timing of this one : )

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +1

      @@stevehamilton321 Haha! Awesome!

  • @guyginat
    @guyginat 7 หลายเดือนก่อน +18

    Man you are pumping out quality Unity videos like crazy!
    Thanks for all your hard work 🙏🏽

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +5

      Thank you! Glad you like the vids! Cheers!

  • @cheesecakenl1980
    @cheesecakenl1980 7 หลายเดือนก่อน +9

    Hey great video! I love this kind of more "complex" game development content. Looking forward to the DI and frameworks video.

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +2

      Right on, thanks!

  • @mhouney
    @mhouney 24 วันที่ผ่านมา

    Thanks for all these videos. I studied software engineering and its refreshing to see some unity videos, that are on a industry standard level! :)

    • @git-amend
      @git-amend  24 วันที่ผ่านมา +1

      You're very welcome! Glad to hear that!

    • @mhouney
      @mhouney 24 วันที่ผ่านมา

      ​@@git-amendWow, that was a fast reply xD

  • @HunterSchramm
    @HunterSchramm 7 หลายเดือนก่อน +2

    Just found your channel this past week and it's quickly becoming my favorite for game dev content. Thank you!

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +1

      Right on, glad you like it! Cheers!

  • @adamprokop
    @adamprokop 5 หลายเดือนก่อน

    I appreciate the last comment on Service Locator might be considered as an antipattern because of some of its caveats. It is still very valuable to try this approach and see for yourself. Very nice tutorial!

    • @git-amend
      @git-amend  5 หลายเดือนก่อน

      Thanks!

  • @user-cw7sk8ih4v
    @user-cw7sk8ih4v 6 หลายเดือนก่อน +1

    Found your channel recently and i love your videos. Helped me alot in understanding patterns and using them in my games. Keep up the great work

    • @git-amend
      @git-amend  6 หลายเดือนก่อน

      Great to hear! Thanks!

  • @Cloud-Yo
    @Cloud-Yo 3 หลายเดือนก่อน

    Learned a lot here, and this is my second time watching this after following the previous videos in the channel, its starting to make more sense. Ill end up watching this a couple of more times to walk through it until it clicks completely...awesome content! Can't say that enough.

    • @git-amend
      @git-amend  3 หลายเดือนก่อน +1

      Thanks again!

  • @PrzygodazUnity
    @PrzygodazUnity 7 หลายเดือนก่อน +2

    You are doing great stuff and very advenced C#. Thanks a lot for your content! :)

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +1

      My pleasure! Glad you enjoy it!

  • @xlilcasper
    @xlilcasper 7 หลายเดือนก่อน +3

    Outstanding content. I am loving these videos.

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +2

      Glad to hear it! Thanks!

  • @antonkorshunov4979
    @antonkorshunov4979 3 หลายเดือนก่อน

    I'm happy to join your fans croud. Great content with precise explanation.

    • @git-amend
      @git-amend  3 หลายเดือนก่อน

      Awesome, thank you!

  • @ekekw930
    @ekekw930 6 หลายเดือนก่อน +1

    Although I really enjoy these complex topics with great examples, I'd like to see some more projects being build using all these best practices. Something like the 3D platformer, but more extended. I totally understand if this is a big task/project and you would rather focus on these kind of videos, it's just a suggestion :) Great work!

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +4

      Thanks for the comment - this is something I've been thinking about more now that the channel has grown to be much than when I did the 3D platformer series. Extended projects appeal to less viewers, but have the benefit that you just described in your comment. Stay tuned!

  • @PaulRobello
    @PaulRobello 6 หลายเดือนก่อน +2

    I would love to see you do a series on DOTS / ECS / Job System

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +2

      Thanks for the suggestion - I've received a few similar comments lately, and I'll definitely be considering that as a future topic that would likely span several videos, or possibly some kind of project. Cheers!

  • @termite1737
    @termite1737 6 หลายเดือนก่อน +2

    You are my best teacher :))), i like your content

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +1

      Thank you so much!

  • @devphil0
    @devphil0 6 หลายเดือนก่อน +1

    Thanks a lot for this video. I think this'll solve my dependency issues for the most part, especially configuring locators for scenes or even just gameobjects sounds exactly what I've been looking for.
    You did mention that it's not "feature rich" yet, so I wonder what else you would add to it?

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +2

      You're welcome! Probably the next feature I would add would be lifecycle management, in case you registered some services and wanted them to automatically do something every Update / FixedUpdate etc. Maybe we can cover a few things like that in a future video, but I might add to the repository before that as time permits.

  • @Broudy001
    @Broudy001 6 หลายเดือนก่อน

    Another fantastic video, though its going to take me some time to figure out how I use this one in my game

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +3

      Great! My advice would be to start small with just one Service or Resource that you reference from multiple places. One thing I didn't mention in the video is that Factory or Abstract Factory classes are often used together with Inversion of Control paradigms. So, for example, if you use a unique ParticleEffectFactory for each scene, your various components that use the factory never change - they just retrieve the registered IParticleEffectFactory for that scene from the ServiceLocator.

  • @TheArghnono
    @TheArghnono 7 หลายเดือนก่อน

    Very interesting, and excellent code as always. Did I notice some norske words in that list? ;-)

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +2

      Takk! Og ja, du har et skarpt øye - Copilot foreslo tysk, men vi vet jo alle at norsk er veien å gå! 😉

  • @uhhmir
    @uhhmir 7 หลายเดือนก่อน

    thank you for covering these topics! i'd love it if you would spend a bit more time on the overview, as i often get lost, not sure what class you're writing in and what the variables or functions do. still, awesome content!

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +1

      Thanks, and I'll keep that in mind! I will probably add some comments to the code in the repository this week as well.

  • @TheKr0ckeR
    @TheKr0ckeR 6 หลายเดือนก่อน

    Great guide! Do you use custom theme for Unity? I see black dropdown menu's and etc. Is that an extension or smth? :)

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +1

      Thanks! I think the black menus were introduced in one of the recent beta versions of Unity 2023. I’m using 2023.2.0b13 right now. Took a while to get used to them tbh.

  • @hainkiwanki
    @hainkiwanki 6 หลายเดือนก่อน

    Didn't know Matthew Mcconaughey did programming tutorials. Love you game programming pattern videos! They are very helpful and well explained!

    • @git-amend
      @git-amend  6 หลายเดือนก่อน

      Haha, do I sound like an actor from Texas? Thanks for the comment!

  • @ribicb
    @ribicb 7 หลายเดือนก่อน

    I had my own clumsy implementation of this, now stealing yours :)

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +1

      Awesome!

  • @Yogs01180
    @Yogs01180 2 หลายเดือนก่อน

    Thanks for this, great implementation, however,I have a question : I'm using your implementation here, I want to reference 2 objects with the same type inside an object from Scene1 into another object in Scene2. How to do this with service locator ?
    am I supposed to create a c# script that I name ServiceGlobalObjectContainer and add them to the service global and get the service in the other side on a script and affect them ?

    • @git-amend
      @git-amend  2 หลายเดือนก่อน +1

      Interesting question. The first and probably easiest thing that comes to mind to to create a Factory that creates, caches and supplies the 2 services you want that are the same type. Then you can supply the factory class instead of the services and ask the factory for the one you want. This way the ServiceLocator knows about the Factory and not the 2 services that are the same type.

  • @nguyenhoangminhtrung2779
    @nguyenhoangminhtrung2779 6 หลายเดือนก่อน +1

    Now I can wait for dependency injection magic, would you use framework such as VContainer or Exenject or writing your own?

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +1

      I was thinking I'd make 2 videos - in the first one we can make a small DI system from scratch since it's a good learning exercise, and the second one could be a compare / contrast / explore video about several different frameworks.

    • @TheKr0ckeR
      @TheKr0ckeR 6 หลายเดือนก่อน +1

      I dont prefer to use a 3rd party asset to put it a top of my code structure. One small update or a bug on the asset would cause a lot of problem. Even though i like DI, i dont like using it with Unity that makes me feel i am fightining with the engine itself.

    • @WalterGordyCanada
      @WalterGordyCanada 3 หลายเดือนก่อน

      @@TheKr0ckeR C# natively contains a dependency injection framework. Most people have no idea but it's been in there since version 1.0.

  • @omeryilmaz1021
    @omeryilmaz1021 หลายเดือนก่อน

    ForSceneOf method looked dangerous a little bit because of many loops, shall we consider of doing that method async so it may not cause a stutter.

    • @git-amend
      @git-amend  หลายเดือนก่อน

      Mostly use that when the scene is loading, in the Start method. Then you can cache the reference and only need to call for it one time right at the beginning.

  • @OdysseyHome-Gaming
    @OdysseyHome-Gaming 2 หลายเดือนก่อน

    I downloaded the zip of the files. Got a missing script reference error when creating the Service Locators through the context menu. Moving the ServiceLocatorGlobal and ServiceLocatorScene classes to their own files fixed the error.
    Is their a project setting in Unity that allows for creating components in files that don't have the same name?

    • @git-amend
      @git-amend  2 หลายเดือนก่อน

      That's interesting, I've not had that problem or heard of anyone who did. But this could happen for any number of reasons. There isn't a special setting to enable this. At any rate, I'm glad you were able to resolve the issue, maybe I should update the repository.

  • @gregbradburn
    @gregbradburn 23 วันที่ผ่านมา

    By registering the services inside the Hero classes, aren't you still tightly coupling the Hero to those services? Doesn't that defeat the purpose of the service locator? I'd have expected the services themselves and consumers to just request them.

    • @git-amend
      @git-amend  23 วันที่ผ่านมา +1

      It's just an example for the video. Of course in a real scenario you would not register a service and then request it in the same class.

  • @endihidir2087
    @endihidir2087 5 หลายเดือนก่อน

    Do you think Service Locator pattern and Dependency Injection can be used together? The two seem like opposites of each other to me. I even read somewhere that Service Locator was called anti-pattern, just like Singleton. What do you think?

    • @git-amend
      @git-amend  5 หลายเดือนก่อน

      I do think you could use them together, but I don't know that I would choose to do that - the reason being that the DI solution shown on this channel only runs at startup, but the Service Locator could be used even for new Game Objects created after the game starts. For that reason I would either continue to develop the DI solution to be able to handle more situations, or just use the Service Locator. It really depends on what you need to accomplish.

    • @endihidir2087
      @endihidir2087 5 หลายเดือนก่อน

      @@git-amend As I understand it, Dependency Injection has a more open structure, because the dependencies are explicitly specified in the code and these dependencies are provided by the injection container. During the addition phase, dependencies are usually defined through a configuration file or records specified during program startup. In Service Locator, the resolving has a more centralized structure in that the components get the dependencies they need through the Service Locator. During the runtime phase, Service Locator usually register records and resolves them dynamically. This provides more centralized control and a flexible structure, but can reduce the transparency of the code about where dependencies come from.

  • @martin.m.kloeckener
    @martin.m.kloeckener 4 หลายเดือนก่อน

    Which AI-voice tool do you use to create your videos? Or is this your actual voice?

    • @git-amend
      @git-amend  4 หลายเดือนก่อน

      That is my real voice

    • @martin.m.kloeckener
      @martin.m.kloeckener 4 หลายเดือนก่อน

      Crazy, I would have lost a bet on that. :D
      Anyways, thank you for your very informative videos!

  • @MarioSantoso
    @MarioSantoso 5 หลายเดือนก่อน

    I really want to understand this advanced work. But my level is not there yet, I am having a hard time understanding this. 😢 Where should I start to learn advance coding?

    • @git-amend
      @git-amend  5 หลายเดือนก่อน +1

      Little bit at a time I suppose! Keep watching, there will be more intermediate content this year as well.

  • @damonfedorick
    @damonfedorick 6 หลายเดือนก่อน +1

    please create us a inventory system that is modular and robust!

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +2

      Great suggestion! I'm actually planning something along those lines in the new year. Stay tuned!

    • @damonfedorick
      @damonfedorick 6 หลายเดือนก่อน +1

      @@git-amend Lets face it, almost every game needs a inventory system. It would be super awesome to have someone walk through a good system that we can apply to almost any game.

  • @5thBabbitt
    @5thBabbitt หลายเดือนก่อน

    Cool video but a bit advanced for my mind at the moment

    • @git-amend
      @git-amend  หลายเดือนก่อน

      There's always tomorrow 😊

  • @Normie4lyf
    @Normie4lyf 6 หลายเดือนก่อน

    Big Bro I want to become a game developer and develop games like gta , rdr , Resident evil , uncharted etc etc which language hsould I start with I am in 10 grade I mastered MIT 6.042J and CS50 should I learn DSA or should do something else

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +1

      Your background in MIT 6.042J and CS50 provides a strong foundation in computational thinking, which is great. Focusing on DSA will significantly enhance your problem-solving skills, crucial for complex game development tasks like those in GTA or Uncharted. The key to success in engineering or game dev is your ability to solve REAL problems. Start small - build a lot of small games that you can go start to finish in one day, build them and then do a retrospective - ask yourself: How can I improve? What am I getting good at? What are my weaknesses? Then repeat, maybe trying something a little different, a little bigger each time. You'll be amazed how fast your skills grow when you do this on a daily basis.

    • @Normie4lyf
      @Normie4lyf 6 หลายเดือนก่อน

      @@git-amend Thank you bro for your advice , definitely follow your words👍👍

  • @manofapocalypse
    @manofapocalypse 6 หลายเดือนก่อน

    will you be making a Discord server?

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +2

      Thanks for asking about a Discord server! It's something I'm considering. To better understand everyone's interest, I might set up a poll or some kind of survey soon to find out if/how people would use that. Stay tuned; your input will be invaluable in making a decision!

    • @manofapocalypse
      @manofapocalypse 6 หลายเดือนก่อน

      Have my comment been deleted? 🤔

  • @omeryilmaz1021
    @omeryilmaz1021 หลายเดือนก่อน

    its the only tutorials can not be watched at 2x

    • @git-amend
      @git-amend  หลายเดือนก่อน

      I'll take that as a compliment!

    • @omeryilmaz1021
      @omeryilmaz1021 หลายเดือนก่อน

      ​@@git-amend Exactly

    • @omeryilmaz1021
      @omeryilmaz1021 หลายเดือนก่อน

      @@git-amend Could we do our Bootstrapper abstract class in Generic T where T:Monobehaviour so the base class will be reusable even more or is it ridicilous?