- Home /
What are good strategies for allowing users to extend behaviour of your scripts?
This is a problem that I have frequently building tools for the editor:
I have a script, say MakeCube that makes a cube. I want to make this script so that it can somehow take custom behaviour. For example, a script that will place the cube - a script with a function PlaceCube that returns a Vector3. Ideally, my code should look something like this:
public class MakeCube: SomeClass //More about this later
{
public CustomPlacer placer;
public void Make()
{
var cube = ... //make the cube
if(placer != null)
cube.transform.position = placer.PlaceCube();
...//Do other stuff
}
}
And ideally, I want:
The user to specify the script file
A GUI to render showing all the custom properties of the user's script
The method to work whether SomeClass is a ScriptableObject, Monobehaviour, or ScriptableWizard
It would be ideal if the user does not have to make any object with the script attached and drop that into the placer field. (This is especially desirable for things such as scriptable wizards, where the idea is to just quickly fill in the properties, and not to have to make a temporary object this for running the wizard with custom behaviours).
The best solutions I found so far is to look for components attached to the same object as MakeCube, but this does not work for scriptable objects.
I welcome any ideas or approximate solutions!