Learn how to use NodeCanvas

Download PDF Version

1.Creating Custom Object Drawers

Custom Object and Attribute Drawers work very similar to Unity’s Property Drawers feature and with it, it is possible to customize how a certain object (class/structure) appears in all NodeCanvas inspectors, as well as create GUI for custom attributes.

Object Drawers

Let’s suppose we have this example struct (it could also be a class of course).

public struct MyExampleStruct{
	public float number;
	public bool trigger;

To create a custom drawer for this object, you simply need to create a class and derive from the generic version of ObjectDrawer, the implement the OnGUI method. For example:

public class MyExampleClassDrawer : ObjectDrawer<MyExampleStruct>{

	public override MyExampleStruct OnGUI(GUIContent content, MyExampleStruct instance){
		instance.number = EditorGUILayout.FloatField("Number", instance.number);
		instance.trigger = EditorGUILayout.ToggleLeft("Trigger", instance.trigger);
		return instance;

The instance parameter of the OnGUI method above, is always of type T. By the end of the OnGUI method you must return the modified instance.

That’s it. The drawer will work exactly the same for field of BBParameter<MyExampleStruct> as well. Both are shown bellow.

public class MyTask : ActionTask{

	public MyExampleStruct example;
	public BBParameter<MyExampleStruct> parameterExample;


Attribute Drawers

For this example, we are going to re-create a Range(min, max) attribute. So, let’s first create the attribute, with the only requirement being that it has to derive from CustomDrawerAttribute.

public class MyRangeAttribute : DrawerAttribute{
	public float min = 0;
	public float max = 1;
	public MyRangeAttribute(float min, float max){
		this.min = min;
		this.max = max;

Then to create the actual GUI drawer for this attribute, in short, you need to create a class and derive from the generic version of AttributeDrawer. Much similar on how it was done before for ObjectDrawer. In this case, the T argument, represents the type of the attribute we need to create the drawer for.

public class MyRangeDrawer : AttributeDrawer<MyRangeAttribute>{

	public override object OnGUI(GUIContent content, object instance){
		instance = EditorGUILayout.Slider(content, (float)instance, attribute.min, attribute.max );
		return instance;

The instance here is always of type object. The inherited ‘attribute’ property returns our attribute which we can use to fetch it’s meta data. No casting required here since out attribute is of type T.

Finally, we simply add this new attribute above a float, or even a BBParameter<float>. It will work exactly the same. Both are shown bellow.

public class MyTask : ActionTask{

	public float myFloat;

	public BBParameter<float> myParameterFloat;


Was this helpful? Yes No Suggest edit
Suggest Edit