- Home /
Strategy Pattern with Monobehaviours
Is there a trick to using the strategy pattern with Monobehaviours? Say you have an interface the Monobehaviour uses. Can you pick an implementation of that interface at run-time and use that?
The best I could come up with was to create a non-MonoBehaviour interface and set of classes which implement the strategy pattern and pass those into the MonoBehaviour when I "construct it" by calling an Initialize method in a Factory class after my Instantiate calls to instantiate the Prefab. This works kind of well, but inside the class you don't have access to the gameObject's variables the the same way a Monobehaviour can just grab them as instance variables.
Answer by Jamora · Aug 26, 2013 at 07:09 PM
If you had shown some code I would have had a lot more to go on, but I would think you currently have the interface as a class variable in a MonoBehaviour, yet you need access to e.g. transform
or rigidbody
from within that implementation of the interface.
The way I would implement the Strategy pattern in Unity is to create a MonoBehaviour of each implementation, then add those to the GameObject after you've decided on which strategy needs to be used. Depending on your use case, the strategy can then be removed after it having served its purpose and a new one added as need be.
GetComponent doesn't really play nice with interfaces, so I use this piece of code to determine if any Component in a GameObject implements an interface:
public static void GetInterfaces<T>(out List<T> resultList, GameObject objectToSearch) where T: class {
MonoBehaviour[] list = objectToSearch.GetComponents<MonoBehaviour>();
resultList = new List<T>();
foreach(MonoBehaviour mb in list){
if(mb is T){
//found one
resultList.Add((T)((System.Object)mb));
}
}
}
Though it is rather cubersome so I usually try to avoid using interfaces when dealing with MonoBehaviours. I use interface-like abstract classes with deep inheritance so I can use GetComponent. Haven't run into maintenance problems... yet.
That's what I do with a lot of stuff in my game. I'll have an interface which the $$anonymous$$onobehaviour implements. But this isn't the complete Strategy pattern. Part of the Strategy pattern is to be able to choose which implementation of the interface you use at runtime. This solution allows you to attach the $$anonymous$$onobehaviour to a Prefab at configuration/compile time.
So you can't choose which $$anonymous$$onobehaviour to use at runtime.
You can have your choosing logic, then use AddComponent to add the correct $$anonymous$$onoBehaviour. So you could, in your factory for instance, add whichever implementation suits your needs. All in one huge switch-case (because I haven't figured out a better way yet...)
When you need to get a reference to the interface(s), you can get the right classes from the GameObject using the code I posted.
Answer by taylank · Mar 05, 2014 at 02:27 AM
I have to admit I'm not sure I quite understand the problem, but maybe you would find this useful: http://www.gamasutra.com/blogs/VictorBarcelo/20131217/207204/Using_abstractions_and_interfaces_with_Unity3D.php
It is what I'm using atm to switch implementations at runtime.
You may also want to invest in something like the Full Inspector from the Asset Store, which gives you editor support for interfaces, something that you don't get from vanilla Unity: https://www.assetstore.unity3d.com/#/content/14913
Your answer

Follow this Question
Related Questions
Associate objects to a prefab 1 Answer
Duplicating a prefab 1 Answer
Civ Like Game Handling Tile Interactions 1 Answer
Need help with strategy terrain. 1 Answer
Instantiating Too Many Objects 1 Answer