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 if not already present:

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 update our ComponentField function to also account for this parameter:

override public void ShowGUI (List<ActionParameter> parameters)
{
	ComponentField ("Light:", ref light, ref constantID, parameters, ref parameterID);
	light.intensity = intensity;
}

Finally, we'll update the AssignValues function to also account for this parameter:

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

Now, if we declare a GameObject parameter in our ActionList's properties panel, we can use it override our "Light" field:

In a similar way, the intensty float can also be overridden with a Float parameter:

using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

	[System.Serializable]
	public class ActionSetLightIntensity : Action
	{
		
		public override ActionCategory Category { get { return ActionCategory.Object; }}
		public override string Title { get { return "Light intensity"; }}
		public override string Description { get { return "Sets a Light's intensity value."; }}

		public Light light;
		public int constantID;
		public int parameterID = -1;

		public float intensity;
		public int intensityParameterID = -1;


		override public void AssignValues (List parameters)
		{
			light = AssignFile (parameters, parameterID, constantID, light);
			intensity = AssignFloat (parameters, intensityParameterID, intensity);
		}


		public override float Run ()
		{
			if (light)
			{
				light.intensity = intensity;
			}
			return 0f;
		}

		
		#if UNITY_EDITOR

		public override void ShowGUI (List parameters)
		{
			ComponentField ("Light:", ref light, ref constantID, parameters, ref parameterID);
			FloatField ("Intensity:", ref intensity, parameters, ref intensityParameterID);
		}

		#endif
		
	}

}