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
2
Question by CelloCoder · Jun 09, 2015 at 04:38 AM · c#reflectioncustom-editor

C# Using Reflection to Automate finding classes

This may be multiple things I am asking for, but I think they all relate to reflection in some way.

I am making a visual scripting tool/node editor. I have a system setup already in code that upon opening an editor window saves a node script, then it can be loaded by another script on a gameobject and used. What I want to do is to automate future use when I start doing the GUI; as in, I don't want to have to have a huge switch case or list that I have to add to every time I add a new node type.

I want to use reflection, or something, that will look for every class I make that extends from the abstract class 'Node' and can tell all of their properties and give me some way to make an instance of it on request. This way creating new node types is as simple as duplicating a basic existing one, changing the name and data, and the editor I coded does it for me.

How should I go about doing this? Google has not seemed to help me much XD

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 CelloCoder · Jun 09, 2015 at 04:40 AM 0
Share

$$anonymous$$ainly I want to automatically find classes extended from an abstract Node that extends from a ScriptableObject

1 Reply

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

Answer by Bunny83 · Jun 09, 2015 at 04:59 AM

Just use this extension method:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class ReflectionHelpers
 {
     public static System.Type[] GetAllDerivedTypes(this System.AppDomain aAppDomain, System.Type aType)
     {
         var result = new List<System.Type>();
         var assemblies = aAppDomain.GetAssemblies();
         foreach (var assembly in assemblies)
         {
             var types = assembly.GetTypes();
             foreach (var type in types)
             {
                 if (type.IsSubclassOf(aType))
                     result.Add(type);
             }
         }
         return result.ToArray();
     }
     public static System.Type[] GetAllDerivedTypes<T>(this System.AppDomain aAppDomain)
     {
         return GerAllDerivedTypes(aAppDomain, typeof(T));
     }
     public static System.Type[] GetTypesWithInterface(this System.AppDomain aAppDomain, System.Type aInterfaceType)
     {
         var result = new List<System.Type>();
         var assemblies = aAppDomain.GetAssemblies();
         foreach (var assembly in assemblies)
         {
             var types = assembly.GetTypes();
             foreach (var type in types)
             {
                 if (aInterfaceType.IsAssignableFrom(type))
                     result.Add(type);
             }
         }
         return result.ToArray();
     }
     public static System.Type[] GetTypesWithInterface<T>(this System.AppDomain aAppDomain)
     {
         return GetTypesWithInterface(aAppDomain, typeof(T));
     }
 }

Just place this class somewhere in your project. Then just use:

 var types = System.AppDomain.CurrentDomain.GetAllDerivedTypes(typeof(YourBaseClass));


edit

I've just added the method "GetTypesWithInterface" which also works with interface types. So you can find all classes which implement a given interface.

 var types = System.AppDomain.CurrentDomain.GetTypesWithInterface(typeof(IMyInterface));
Comment
Add comment · Show 3 · 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 CelloCoder · Jun 09, 2015 at 04:51 PM 0
Share

Thanks! Now I need to post one more question for another roadblock lol

avatar image bibbis · Oct 13, 2020 at 05:22 PM 0
Share

What can an array of types be used for? I can't figure out how to actually do anything with this array.

avatar image Bunny83 bibbis · Oct 13, 2020 at 06:27 PM 0
Share

Well, depending on the nature of your class you can either attach them as components to a gameobject (in case the types are actually $$anonymous$$onoBehaviours) or if they are ordinary classes you can create an instance of those classes by using the Activator class.


Since we're only interested in classes derived from our base class we can safely cast the created objects to our base class. So we can work normally with polymorphism.


I just realised that the "GetAllDerivedTypes" method only checks for classes that are derived from a base class. I'll add another method that works with interfaces as well which is probably more useful.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

invoke method with reflection C# 3 Answers

Editor script private fields and method best practices 0 Answers

Custom Editor Properties Revert to Defaults on Play 4 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