TypeTree
Builds and caches type inheritance hierarchies using Unity's TypeCache. Each node in the tree represents a type, with children being its direct subclasses. Supports enumeration over the full subtree and implicit conversion to Type.
Definition
Namespace: Paragon.Editor Assembly: Paragon.Editor.dll
public class TypeTree : IEnumerable<Type>Implements: IEnumerable<Type>
Remarks
TypeTree provides a structured view of type inheritance hierarchies, built on top of Unity's TypeCache.GetTypesDerivedFrom(). Unlike TypeCache, which returns a flat list of derived types, TypeTree organizes them into a parent-child tree that mirrors the actual inheritance chain.
Caching
All TypeTree instances are cached in a static Dictionary<Type, TypeTree>. Calling GetTypeTree() with the same root type returns the cached instance. Subtrees are also cached — if you build a tree for BaseClass, then later request DerivedClass, the existing node is reused.
Generic Type Handling
When resolving the parent type, generic types are normalized to their generic type definition. For example, MyClass<int> maps to MyClass<> as its parent, ensuring the tree structure is consistent regardless of generic type arguments.
Enumeration
TypeTree implements IEnumerable<Type> with a depth-first pre-order traversal. Iterating yields the root type first, then recursively yields all descendants:
Quick Lookup
Build type hierarchy
TypeTree tree = TypeTree.GetTypeTree(typeof(MyBase))
Get the type
tree.Type or implicit Type t = tree
Get parent node
tree.Parent
Get direct children
tree.Children
Iterate all descendants
foreach (Type t in tree) { ... }
Use with LINQ
tree.Where(t => !t.IsAbstract)
Properties
Type
Type
public
The System.Type this node represents
Parent
TypeTree
public
The parent node in the tree (looked up from the static cache)
Children
IEnumerable<TypeTree>
public
Direct child nodes (immediate subclasses)
Fields
typeTrees
Dictionary<Type, TypeTree>
private static readonly
Global cache of all TypeTree nodes
type
Type
private readonly
The type this node represents
children
HashSet<TypeTree>
private readonly
Direct child nodes
parent
Type
private readonly
The parent type (not the node — resolved via cache)
Constructors
TypeTree(Type) — private
Creates a new node, resolves the parent type, registers itself in the global cache, and initializes an empty children set.
Methods
GetTypeTree
Builds (or retrieves from cache) a full type hierarchy rooted at the specified type.
root
Type
The root type to build the hierarchy from
Returns: The TypeTree node for the root type, with all descendants populated.
Algorithm:
Checks the cache — returns immediately if found
Creates a new
TypeTreenode for the rootGets all derived types via
TypeCache.GetTypesDerivedFrom(root)For each derived type, gets or creates a
TypeTreenodeLinks each child node to its parent node's
childrenset
GetParentType (private static)
Resolves the parent type, normalizing generic types to their generic type definition.
GetEnumerator
Depth-first pre-order traversal: yields the current node's type, then recursively yields all children.
Operators
Implicitly converts a TypeTree to its underlying Type.
Common Pitfalls
Static cache persists across calls The typeTrees dictionary is static and never cleared. In long editor sessions, this cache grows as new root types are queried. This is by design for performance, but be aware that the cache reflects the type state at the time of first construction.
Parent property requires parent type to be cached Parent does a dictionary lookup via typeTrees[parent]. If the parent type was not part of any previously built tree, this will throw KeyNotFoundException. This is generally safe because GetTypeTree populates the cache for all types in the hierarchy, but accessing Parent on a manually constructed node could fail.
Editor-only — uses TypeCache TypeTree depends on UnityEditor.TypeCache, which is only available in the Unity Editor. Do not reference this class from runtime code.
Examples
Building a Type Hierarchy
Iterating All Descendants
Implicit Type Conversion
Navigating the Tree
See Also
ParagonMenuItems — uses editor utilities
Paragon Editor Overview — system overview
Last updated