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 ypsilon · Jun 25, 2014 at 09:04 PM · inheritanceabstractrequirecomponent

"Injecting" an interface into an object

I have a class for my enemy which extends from MonoBehaviour. This class has inside a reference to another class that I have made:

 [RequireComponent (typeof (Action))]
 public class EnemyController : MonoBehaviour {
     public Action action;      // Action is an interface!
 }

Now, I would like to "inject" this dependency using the Unity GUI, just dragging and dropping the desired object into the EnemyController. The problem is that Action is an interface, so Unity tells me that the class can't be abstract.

My idea was to be able to use different types of Actions... Is it possible to do this?

Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rutter · Jun 25, 2014 at 09:10 PM

Bad news: Unity's built-in serialization isn't polymorphic. Your Action reference can point at any given subclass during runtime, of course, but when Unity writes the data to disk, it will serialize only an Action and its associated fields.

You can find some discussion about this, online, but this thread is a pretty good overview.

Some people work around this by integrating other serialization libraries, such as JsonFX, MiniJSON, Protobufs, or so on. Others use Unity to serialize primitive data containers, which they use to reconstruct necessary objects at runtime.

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 ypsilon · Jun 25, 2014 at 09:21 PM 1
Share

Thanks for the fast answer, rutter. This is definetely bad news, since I had been planning this very nice object-oriented solution for my problem for some time, and it relies on interfaces and the possibility of switching implementations interchangeably (actually, it's AI related - just like the question in the post that you sent me).

This sucks. I'm depressed now. I guess I will have to make some very ugly and very big class with enums to handle this.

avatar image ypsilon · Jun 25, 2014 at 09:54 PM 0
Share

For those with the same kind of problem, I've made a quite simple workaround:

 public enum ActionType {
     ACTION1, ACTION2;
 }
 
 [RequireComponent(typeof (ActionType))]
 public class EnemyController : $$anonymous$$onoBehaviour {
     private Action action;
     public ActionType actionType;
 
     void Start () {
         switch (actionType) {
             case (ACTION1): action = new ProwlAction(); break;
             case (ACTION2): action = new AttackAction(); break;
             default: break;
         }
     }
 }

Something like that should work. It is not as nice and elegant as the pure OO solution would be, but it's something.

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

Is it possible to use || (or) with RequireComponent? 5 Answers

Wierd problem with abstract classes and import statements in Javascript 2 Answers

Abstract class question 1 Answer

Abstract Class acting like a static Class when inherited 1 Answer

How should I structure my unit class relationships? 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