Writing a custom Action 3

Actions can be extended to make use of ActionList parameters, so that field values can be set at runtime. In this tutorial, we'll give our custom Action the ability to accept a GameObject parameter in place of the field we have in the GUI.

Parameters involve the use of Lists, which means we must first include a new library. In our script's header, add the following:

using System.Collections.Generic;

We'll store the parameter as an ID number, much the same way as we record a Constant ID. However, we must ensure it's set to -1 by default. Add to the list of declared variables:

public int parameterID = -1;

Our ShowGUI function's header must be amended to accept a list of parameters.

override public void ShowGUI (List<ActionParameter> parameters)

We will then call a function that will display a parameter selector, if any are available. If one is chosen, we will reset the other variables - otherwise, we'll display the GUI as normal:

override public void ShowGUI (List<ActionParameter> parameters)
{
	parameterID = Action.ChooseParameterGUI
       ("GameObject to affect:", parameters, parameterID, ParameterType.GameObject);
	if (parameterID >= 0)
	{
		constantID = 0;
		objectToAffect = null;
	}
	else
	{
	objectToAffect = (GameObject) EditorGUILayout.ObjectField
       ("GameObject to affect:", objectToAffect, typeof (GameObject), true);
				
		constantID = FieldToID (objectToAffect, constantID);
		objectToAffect = IDToField (objectToAffect, constantID, true);
	}
}

Finally, we'll update the AssignValues function to also accept parameters, and re-assign the GameObject if appropriate:

override public void AssignValues (List<ActionParameter> parameters)
{
  objectToAffect = AssignFile (parameters, parameterID, constantID, objectToAffect);
}

Our Action is now complete - it will work with GameObjects, prefabs and parameters.