Using the Editor to make life easier
I like to build systems and components that other designers can use to speed up their workflow, and/or get specific functionality without having to write custom scripts.
The TriggerableZone
script is a simple but powerful component that enables you to call any public method on any component/monobehaviour in the scene. An example of the GUI can be seen in the banner image above (right).
In that example, this script along with a BoxCollider
set to trigger enabled one of my team to start a conversation (attached to another gameobject) when the player passes by.
It functions using the UnityEvent
class.
At runtime, I copy over the events from the an Editor visible event to a private UnityEvent
and allow subscriptions via code to the private event. This allows me to clear all events easily if required by bool TriggerEnterOnceOnly
and not impact the editor in any way. A section of the TriggerEnter code is provided below, this is echoed for all other trigger events.
...
public UnityEvent OnTriggerEnterAction;
...
private void Start()
{
...
_onTriggerEnterAction = OnTriggerEnterAction;
...
}
private void OnTriggerEnter()
{
if (!isActiveAndEnabled)
{
return;
}
if (_onTriggerEnterAction != null)
{
// Run all stored actions
_onTriggerEnterAction.Invoke();
if (TriggerEnterOnceOnly)
{
// Remove listners
_onTriggerEnterAction.RemoveAllListeners();
_onTriggerEnterAction = new UnityEvent();
}
}
}
...
Editing the Editor
To make the GUI a little more useful, I added bezier curves between each TriggerableZone
and the objects that are assigned in the editor. Using colour codes for Enter/Exit/Stay I coloured the lines as well as the GUI elements to make it a bit more intuitive.