Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Meem0 · Sep 13, 2014 at 08:42 PM · interfacedependenciesdependency injection

Best way to resolve dependencies

Let's say some component called A needs to use a function Foo in some other component, B.

More specifically, say A is used in a number of different objects, and Foo can be implemented differently in the other component. In other words, multiple objects use A, but the component that implements Foo is different across those objects.

I've read that the best way to deal with this dependency is essentially this:

 // A.cs
 
 public class A : MonoBehaviour
 {
     public ISomeInterface SomeInterface { get; set; }
 
     void Bar()
     {
         SomeInterface.Foo();
     }

     // other stuff A does
 }


 // ISomeInterface.cs

 public interface ISomeInterface
 {
     void Foo();
 }


 // B.cs

 public class B : MonoBehaviour, ISomeInterface
 {
     public void Foo()
     {
         // do something
     }

     // other stuff B does
 }

But that's where all the tutorials end. I don't understand where you're supposed to assign B as A's SomeInterface.

Do you make a "setup" script for each object and resolve the dependencies in the Start() function?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image glantucan · Sep 17, 2014 at 05:38 PM 0
Share

mmmm, "best way" to solve anything is very context, and opinion, sensitive. But I have an aswer for you, anyway ;)

See below

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by glantucan · Sep 17, 2014 at 04:36 PM

That's a good question, specially in Unity, where the entry point of your application is not clearly identified (it's the scene itself BTW).

I would create a GameObject with a, let's call it, GameManager script attached to it as a component. This GameManager instance is the one responsible for all the wiring you are talking about. You can make it singleton if you want, to avoid having more than one instance of it. I don't consider that a good practice, though. If you want only one instance, be careful and create just one.

Then in the Awake() callback of GameManager I would either:

  1. Find the game objects containing the A instances components (You can use GameObject.Find() for it or, if they are more than one, asign a tag to them in the inspector and use GameObject.FindGameObjectsWithTag())

  2. Instantiate GameObjects from prefabs containing the A class and the class implementing ISomeInterface.

Either way, you get references to the participating objects and now you can asign things properly. Here is an example of the GameManager class:

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour 
 {
     void Awake () 
     {
         // Asume the GO containing the instance implementing ISomeInterface is named 'FooContainer'
         GameObject fooContainer = GameObject.Find("FooContainer");

         // This part is a little tricky, the generics version of GetComponent() doesn't work here (see later)
         ISomeInterface fooFighter = fooContainer.GetComponent(typeof(ISomeInterface)) as ISomeInterface;

         // Asume GOs containing the A script have 'Atag' asigned in the inspector 
         GameObject[] AContainers = GameObject.FindGameObjectsWithTag ("Atag");
 
         foreach(GameObject AContainer in AContainers)
         {
             A Ainstance = AContainer.GetComponent<A>();
             if (Ainstance != null)
             {
                 Ainstance.SomeInterface = fooFighter;
             }
         }
 
     }
 }

Unity doesn't make easy to work with MonoBehaviours implementing your own interfaces, but this works.

The only concern is that the code used to get the instance of the class implementing the interface feels like a hack to me.

I did post a question about it here which hopefully will get answered by someone more experienced than myself.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Zenject Issue: Declaring Factory for Object which doesn't have all its dependencies yet 1 Answer

Getting on drag functions into other classes 1 Answer

Editor doesn't load (some) dependencies with the scene, but standalone does? O_o 1 Answer

How can I select asset dependencies when using AssetDatabase.ExportPackage 0 Answers

Can not resolve dependencies for Android 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges