Skip to content
Nathan Gill edited this page Sep 4, 2020 · 6 revisions

ComponentProxies are an easy way of quickly proxying MonoBehaviours from the game's assemblies for use in Unity Editor. Since we can't directly use the game's scripts these will act as a way of storing the editor data and then initializing the proper component and copying that data over on run-time.
You do not need to use a ComponentProxy for your own custom scripts, however they provide some cool features that could come in handy regardless.

Here's an example of a simple proxy and we'll go over it line by line:

// Your class extends ComponentProxy (which itself extends MonoBehaviour)
public class MyProxy : ComponentProxy
{
    // Add your serialized fields. These are what will show up in the Unity Editor
    public int MyInt;
    public string MyString;

    // The ResolveOrder tells WurstMod which order custom proxies should be initialized in.
    // Higher values resolve before lower ones, with the default being 1
    public virtual int ResolveOrder => 1;

    // This method is called while the scene is loading in-game.
    // It is similar to Awake(), but called directly by the loader.
    public override void InitializeComponent()
    {
        // Your code to create and initialize the proxied elements go here.
        // In this case, we're going to proxy a fictional component then copy the serialized data from the editor over to it.
        var proxied = gameObject.AddComponent<MyFictionalComponent>();
        proxied.MyInt = MyInt;
        proxied.MyString = MyString;

        // And finally, if you no longer need this component to be on the object we can destroy it.
        Destroy(this);
    }

    // This method is called while a scene is being exported in the Unity editor.
    // You can use it to validate the state of your component and provide feedback to the user
    public override void OnExport(ExportErrors err)
    {
        // Create an error if MyInt is negative.
        // Errors will prevent the export from completing, so only use these in critical situations
        if (MyInt < 0) err.AddError("MyInt cannot be a negative number!", this);

        // Create a warning if MyString is empty
        // Warnings will pause the export after validation completes prompting the user to cancel and fix them,
        // But still allowing them to continue.
        if (MyString == "") err.AddWarning("MyString is empty. Consider setting it.", this);

        // Debug is just debug. Gets printed to console, nothing special happens
        err.AddDebug("Hello! :-)", this);

        // You can also use this method to update some fields if necessary.
        if (MyString == "foo") MyString = "bar";
    }
}

Remarks:

  • Your proxy class can use types from the game's assemblies inside a method, but it cannot have methods, fields, or properties that return or have parameters of types from the game's assemblies. (There are ways around this, See this page for more info)
Clone this wiki locally