Skip to content

Creating plugins

dsisco11 edited this page Apr 15, 2016 · 22 revisions

Introduction

This guide will walk you through the process of creating a new plugin and specifying it's data such as it's name, description, version, icon, and thumbnail to be used with the plugin loader.
This guide assumes you have are comfortable using Visual Studio. This guide will NOT be teaching you how to use Visual Studio" as that topic is far too broad for the scope of this guide.

Alternatively, if you are using VisualStudio then you can just:

  • Download the plugin project template here
  • Double click it to install the template.
  • Create a new project using the custom template.
  • Fill out your plugins information in the PLUGIN_INFO declaration.
  • And skip to this guide!

Creating a plugin

To create your own plugin you first need to include a few key assemblies from your SlimeRancher's "Managed" folder located under it's data folder.
A picture is worth a thousand words so here's a picture of the assemblies you'll need, and look they're highlighted!

So you referenced all the assemblies you need and now you're feelin' really tough huh?
Well calm down now because here's where it gets hard.
You see the plugin loader requires all plugins to have a special class by the name of "SR_Plugin" and it has to be public static
So make a new class in your project and name it SR_Plugin, then make it public and static.
public static SR_Plugin

Now include the plugin loader in a using statement at the top of the class file.
using SR_PluginLoader;

At this point let's go ahead and side-track to setup the other class that will handle your plugins actual logic. Create a new class and name it whatever you want, but it needs to inherit from MonoBehaviour.
public class My_Sick_Plugin_2016 : MonoBehaviour
Good. Beautiful. Fantastic.

Now you need to reference the assemblies we imported earlier but I'm not going to baby you so all I'll say is this. At the top of the file add a using statement for each of those assemblies.

Well it looks like somebody screencapped it _and_ highlighted it FOR you, How will you ever learn at this rate?

Anyhow let's head back to the SR_Plugin class.

All plugins MUST specify data about themselves, their name at the very least.
This is done by creating a static instance of the Plugin_Data class with the variable named PLUGIN_INFO.

Notice how the description is specified as a literal string with the @ symbol
Doing this allows you to more easily specify exactly how your plugins description text will appear on the menu ingame.
Since any tabs or line-breaks are rendered displayed correctly ingame you don't want to tab those lines over just to get them to line up with your variable declaration as doing so would make the text ingame horribly misaligned!

In addition to a name, author, version, and description it is possible to give your plugin an icon and thumbnail/banner.
Simply add an image named icon.png or thumb.png as a resource to your plugin. The plugin loader will load the first resource named thumb.png and icon.png as the thumbnail and icon for a plugin.

Every plugin must also include an entry and exit point.
You do this by declaring two functions named Load and Unload.
Keep in mind that these functions have to be both public and static as well.
public static void Load(Plugin plugin, GameObject gmObj)
public static void Unload(GameObject gmObj)
Both of these functions receive a Unity GameObject instance as a parameter with the former Load function also receiving a reference to it's own Plugin object.
No two plugins will be given the same GameObject instance in their Load or Unload functions.

These functions allow your plugin to be loaded and unloaded.
In the Load function what you want to do is attach a your custom MonoBehaviour class to the GameObject passed to it.
In this case the custom class name is ExtraControls, so be sure to replace that with the name of your own custom class.
public static void Load(Plugin plugin, GameObject gmObj)
{
gmObj.AddComponent<ToughLife>();
}

Now we can't forget to allow the plugin to be unloaded, the plugin loader system will handle cleaning up your plugins presence in the game world but it's your job to unload any unmanaged items you might have created.
Do so in the Unload function.

That's it for the SR_Plugin part, now you put your plugins logic in that custom MonoBehaviour class you made earlier to handle your plugins actual logic.

If you are unfamiliar with how unity behaviour classes work then I recommend reading this.
Additional information about MonoBehaviour type classes can be found on the unity wiki.

Thanks for choosing this as your plugins loader and good luck with all of your future plugins!

Clone this wiki locally