Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 srknvrnbskrt · Jul 17, 2017 at 10:14 PM · c#interfacedesignabstractfindobjectsoftype

problem with interface and abstract class in unity

Hello, There is a problem I always face. If I use interface I cannot use drag/drop in editor. Also, FindObjectsOfType doesn't work because it is not an object. There is the same problem when I use abstract class. Is there any solution?

Comment
Add comment · Show 3
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 Cornelis-de-Jager · Jul 17, 2017 at 10:24 PM 0
Share

Can you show us your code for the FindObjectsOfType

avatar image srknvrnbskrt Cornelis-de-Jager · Jul 17, 2017 at 11:15 PM 0
Share

Let me write an example like that:

public interface ITryInterface { void $$anonymous$$y$$anonymous$$ethod(); }

public class TryClass1: $$anonymous$$onoBehaviour, ITryInterface{ void $$anonymous$$ethod(){ //Do Something cool } }

public class TryClass2: $$anonymous$$onoBehaviour, ITryInterface{ void $$anonymous$$ethod(){ //Do Something cooler } }

public class TryClass3: $$anonymous$$onoBehaviour, ITryInterface{ void $$anonymous$$ethod(){ //Do the coolest! } }

public class $$anonymous$$anager:$$anonymous$$onoBehaviour{ public ITryInterface[] try; /cannot drag, drop. Can't see in the editor and con't find them with findobjectoftype/

 void Start(){
     foreach(ITryInterface item in try){
         item.$$anonymous$$ethod();
     }
 }

}


public abstract class TryClass: $$anonymous$$onoBehaviour{ abstract public void $$anonymous$$ethod(); }

public class TryClass1: TryClass { override public void $$anonymous$$ethod(){ //Do Something cool } }

public class TryClass2: TryClass { override public void $$anonymous$$ethod(){ //Do Something cooler } }

public class TryClass3: TryClass { override public void $$anonymous$$ethod(){ //Do the coolest! } }

public class $$anonymous$$anager:$$anonymous$$onoBehaviour{ public TryClass[] try; /cannot drag, drop. Can't see in the editor and can't find them with findobjectoftype/

 void Start(){
     foreach(TryClass item in try){
         item.$$anonymous$$ethod();
     }
 }

}

avatar image srknvrnbskrt Cornelis-de-Jager · Jul 17, 2017 at 11:18 PM 0
Share

I don't know how to share as a script. Sorry

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 18, 2017 at 12:36 AM

Of course you can't use an interface in this case since Unity can only serialize references of types which are derived from UnityEngine.Object. However an abstract class which is derived from MonoBehaviour works just fine. I just created an example in my test project just like yours and it works just fine. I created 3 files TryClass, TryClass1 and Manager

 // TryClass.cs
 public abstract class TryClass : MonoBehaviour
 {
     public abstract void MyMethod();
 }
 
 // TryClass1.cs
 public class TryClass1 : TryClass
 {
     public override void MyMethod()
     {
         Debug.Log("TryClass1");
     }
 }
 
 // Manager.cs
 public class Manager : MonoBehaviour
 {
     public TryClass[] list;
     
     void Start()
     {
         foreach (var c in list)
             c.MyMethod();
     }
 }

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
avatar image
0

Answer by ZeN12 · Jul 18, 2017 at 07:15 AM

First option, class should inhered from MonoBehavior (or Component, not sure about this).

Second option, class (or struct) should be serializable.
https://docs.unity3d.com/ScriptReference/Serializable.html

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
avatar image
0

Answer by UnityCoach · Jul 20, 2017 at 03:39 PM

This is a topic I touched on in my SOLID design training course. FindObjectOfType doesn't work with interfaces, though GetComponent does.

So, here's something you can do is :

1) find all root objects, using System.Linq to filter all transform with no parent

 List<Transform> rootTransforms = (from t in FindObjectsOfType<Transform>()
     where t.parent == null
     select t).ToList();

2) then use GetComponentsInChildren<Interface>() on them.

 List<ISpecialEventHandler> _specialEventHandlers = new List<ISpecialEventHandler>();
        
 
 foreach (Transform t in rootTransforms)
    
     _specialEventHandlers.AddRange (t.GetComponentsInChildren<ISpecialEventHandler>());
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 Bunny83 · Jul 20, 2017 at 05:28 PM 0
Share

It's true that unfortunately FindObjectsOfType doesn't accept an interface however it does work just fine with an abstract base class that is derived either from $$anonymous$$onoBehaviour or ScriptableObject.

Ps: Since in your solution FindObjectsOfType<Transform>() already returns an array with all Transform components, it doesn't make much sense to throw most of them away and then use GetComponentsInChildren. GetComponentsInChildren has to iterate through the whole hierarchy of the given object. Since you already have all Transform components you can just use GetComponents on each Transform.

However a better approach would be to use FindObjectsOfType<$$anonymous$$onoBehaviour>() and just as-cast all of them to your interface. That way you can avoid all the additional GetComponents / GetComponentsInChildren. There are most likely much more Transform components out there than $$anonymous$$onoBehaviour derived Components.

With Linq you could simply do:

 var list = FindObjectsOfType<$$anonymous$$onoBehaviour>().OfType<ISpecialEventHandler>().ToList();

This does only allocate memory for the initial "$$anonymous$$onoBehaviour[]" array and the final List (and of course the temporary linq IEnumerables / IEnumerators).

"OfType" basically does two things in one: .Select(c=>c as ISpecialEventHandler).Where(c=>c != null)

avatar image UnityCoach Bunny83 · Jul 20, 2017 at 05:43 PM 0
Share

Interesting. I figured GetComponentsInChildren would probably be "optimised" and provide better results than casting every single $$anonymous$$onoBehaviour. I guess we can predict which of FindObjectsOfType<$$anonymous$$onoBehaviour>() and FindObjectsOfType<Transform>() will be the smaller, depending on the scene configuration. I'll do some testing on my scene. Thanks for the tip!

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

356 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

How to Store and Reference Skills Flexibly? 0 Answers

Class derivation problem 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

An OS design issue: File types associated with their appropriate programs 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