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
1
Question by LeetRooster · Aug 24, 2013 at 08:25 PM · instantiatemonobehaviourdesign-patternsstrategy

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.

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 Jamora · Aug 24, 2013 at 10:35 PM 0
Share

Can you show some code of what you're trying to do?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · 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
avatar image LeetRooster · Aug 27, 2013 at 05:21 AM 0
Share

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.

avatar image Jamora · Aug 27, 2013 at 12:12 PM 1
Share

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.

avatar image
1

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

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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