Types and Conversors
The NodeGraph features a conversion system that allows you to convert objects from one type to another.
How It Works
When starting the application, NodeGraph scans for all existing converters in the project. For each converter found, it creates an instance of the corresponding class, which will be used whenever a conversion between the defined types is needed.
Examples
Here are two examples of converters already implemented in the project:
Convert a Component to Transform:
[Description("component.transform")]
Transform ITypeConverter<Transform, Component>.Convert(Component source)
{
return source.transform;
}
Convert a Component to GameObject:
[Description("component.gameObject")]
GameObject ITypeConverter<GameObject, Component>.Convert(Component source)
{
return source.gameObject;
}
Check the CustomConverters
class to see all the existing implementations. This will provide a solid foundation for understanding how new converters can be created and applied in your project.
Creating Custom Converters
The existing converters handle most common scenarios, but you can create custom converters to meet specific needs. To add a new converter:
- Create a New Class: Implement the
ITypeConverter<TOut, TIn>
interface in your new class. - Define the Convert Method: Implement the Convert method to specify how the conversion should be performed.
To add a description for your converter, use the System.ComponentModel.DescriptionAttribute
in your Method. This Attribute will display a tooltip with a description when you hover over the TypeConverter label in the Inspector.